害虫php使用抽象功能的特征

发布于 2025-02-08 13:42:20 字数 2192 浏览 0 评论 0原文

我是害虫PHP的新手,并尝试在害虫PHP文件中使用特质。在一个特征中,我有一种抽象方法,必须输入路由名称。但是我不知道如何在害虫中正确调用抽象功能?

特征

<?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();
}

这是这是害虫文件的

<?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';
}

,这是我遇到的错误

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()

,有人可以告诉我如何在PEST PHP中正确使用该特质吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文