ahoi's avatar
Level 5

Test passport with uuids

Hello everybody,

I'd like to create some tests including passport-based auth functions.

I am using passport with uuids, I want to pre-run the passport-installation in my PHPUnit-test:

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Set up the tests
     */
    public function setUp(): void
    {
        parent::setUp();

        Artisan::call('migrate', ['-vvv' => true]);
        Artisan::call('passport:install', ['-vvv' => true, '--uuids' => true]);
        $this->seed();
    }
}

Unfortunately this returns:

  Received Mockery_1_Illuminate_Console_OutputStyle::askQuestion(), but no expectations were specified

  at vendor/symfony/console/Style/SymfonyStyle.php:293
    289▕      * {@inheritdoc}
    290▕      */
    291▕     public function confirm(string $question, bool $default = true)
    292▕     {
  ➜ 293▕         return $this->askQuestion(new ConfirmationQuestion($question, $default));
    294▕     }
    295▕ 
    296▕     /**
    297▕      * {@inheritdoc}

      +18 vendor frames 
  19  tests/TestCase.php:25

If I extend this to --no-interaction I am getting the very same error.

Line 25 of TestCase.php contains

Artisan::call('passport:install', ['-vvv' => true, '--uuids' => true, '-n' => true]);
0 likes
1 reply
ahoi's avatar
Level 5

One thing I mentioned: The error occurs once a test is running:

  • Tests\Feature\AuthTest > user can log in via api
   Mockery\Exception\BadMethodCallException 

This is the test

<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class AuthTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test if the user can log in via HTTP API.
     *
     * @return void
     */
    public function test_user_can_log_in_via_api()
    {
        $user = User::where('email', '[email protected]')->first();

        $body = [
            'email'  => $user->email,
            'secret' => 'secret',
        ];

        $this->json('POST', '/api/auth/login', $body, [
            'Accept' => 'application/json',
        ])->assertStatus(200)->assertJsonStructure([
            'token_type',
            'expires_at',
            'access_token',
        ]);
    }
}

It seems that use RefreshDatabase is causing this issue.

Please or to participate in this conversation.