Что Laravel джет + команды живчика + проблемы FeatureTest для проверки подлинности


Что Laravel джет + команды живчика + проблемы FeatureTest для проверки подлинности

29.09.2020 02:42:23 Просмотров 33 Источник

Я использую свежую установку laravel Jetstream с командами и Livewire. Все работало как и ожидалось но когда я пытаюсь сделать некоторые тесты с livewire я получаю эту ошибку

1) Tests\Feature\ContactFormTest::contact_form_page_contains_contact_form_livewire
ErrorException: Trying to get property 'id' of non-object (View: /var/www/livewire/resources/views/navigation-dropdown.blade.php) (View: /var/www/livewire/resources/views/navigation-dropdown.blade.php) (View: /var/www/livewire/resources/views/navigation-dropdown.blade.php)
/var/www/livewire/vendor/laravel/jetstream/src/HasTeams.php:28
/var/www/livewire/vendor/livewire/livewire/src/CompilerEngine.php:38
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:60
/var/www/livewire/vendor/livewire/livewire/src/LivewireViewCompilerEngine.php:32
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php:61
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/View.php:139
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/View.php:122
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/View.php:91
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Http/Response.php:62
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Http/Response.php:34
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:765
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:737
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:691
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:128
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php:29
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:44
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:77
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php:58
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:116
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:62
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:67
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:103
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:693
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:668
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:634
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Routing/Router.php:623
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:166
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:128
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php:87
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/fruitcake/laravel-cors/src/HandleCors.php:37
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/fideloper/proxy/src/TrustProxies.php:57
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:167
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:103
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:141
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:110
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:508
/var/www/livewire/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:292
/var/www/livewire/tests/Feature/ContactFormTest.php:20

Похоже, это что-то связанное с командами. Кто-нибудь знает, как обойти эту проблему? заранее спасибо

У вопроса есть решение - Посмотреть?

Ответы - Что Laravel джет + команды живчика + проблемы FeatureTest для проверки подлинности / Laravel Jetstream + Livewire + Teams FeatureTest problems when authenticate

Является ответом!
JMoura

29.09.2020 02:48:09

У меня просто была эта проблема некоторое время назад. Это вызвано тем, что у заводских пользователей нет созданной личной команды.

$user = App\Models\User::factory()->create()
dd($user->personalTeam()); // null

Вы можете принудительно создать личную команду, используя тот же код внутри действия fortify:

App\Actions\Fortify\CreateNewUser@createTeam

Тогда ваши тесты должны выглядеть так:

/** @test */
public function test_livewire_component_is_present()
{
    $user = User::factory()->create();
    //CreateNewUser@createTeam
    $user->ownedTeams()->save(Team::forceCreate([
        'user_id' => $user->id,
        'name' => explode(' ', $user->name, 2)[0]."'s Team",
        'personal_team' => true,
    ]));
    // ... The rest of your test. Here an example:
    $this
        ->get('/') //the route where your livewire component is declared
        ->assertSeeLivewire(''); // your livewire-component name
}

И чтобы избежать использования этого гигантского блока кода в каждой аутентификации, вы можете создать вспомогательную функцию signIn() внутри

Тест\TestCase

Что-то вроде:

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    public function signIn($user=false)
    {
        if(!$user) {
            $user = User::factory()->create();
        }

        $user->ownedTeams()->save(Team::forceCreate([
            'user_id' => $user->id,
            'name' => explode(' ', $user->name, 2)[0]."'s Team",
            'personal_team' => true,
        ]));

        $this->be($user);
    
        return $user;
    }
    // ...
}

Затем вы можете использовать его в своих тестах

/** @test */
public function test_foo_bar()
{
    $user = $this->signIn();
    ...
}
Помочь в развитии проекта:
Закрыть X