1

I`m new to pest PHP and trying to use a trait within an pest PHP file. Within an trait I have an abstract method where I have to enter the route name. But I dont know how to call the abstract function correctly within pest?

This is the Trait

<?php

namespace Tests\Traits;

use Illuminate\Support\Facades\Route;

trait UserScopeEndpointTest
{
    /** @test */
    public function ep_contains_auth_middleware()
    {
        $this->assertTrue(in_array('auth',
            Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
    }

    /** @test */
    public function ep_contains_apply_user_scopes_middleware()
    {
        $this->assertTrue(in_array('applyUserScopes',
            Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
    }

    private abstract function routeName();
}

This is the Pest file

<?php

use App\Models\Task;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Traits\UserScopeEndpointTest;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;

uses(RefreshDatabase::class);
uses(UserScopeEndpointTest::class);

beforeEach(function () {
   $task = Task::factory()->create();
   Task::factory()->create();
   actingAs($task->user);
});


test('user can see all his tasks', function () {
    get(route(routeName()))->assertStatus(200);
});

function routeName() {
    return 'tasks.index';
}

This is the error I'm getting

Class P\Tests\Feature\TaskIndexTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (P\Tests\Feature\TaskIndexTest::routeName)

  at vendor/pestphp/pest/src/Factories/TestCaseFactory.php:222
    218▕
    219▕                 final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
    220▕                     $traitsCode
    221▕
  ➜ 222▕                     private static \$__filename = '$filename';
    223▕                 }
    224▕             ");
    225▕         } catch (ParseError $caught) {
    226▕             throw new RuntimeException(sprintf('Unable to create test case for test file at %s', $filename), 1, $caught);

      +1 vendor frames
  2   [internal]:0
      Whoops\Run::handleShutdown()

Can someone tell me how to use the trait correctly within pest php?

Lx45
  • 95
  • 2
  • 10

0 Answers0