I have a lumen microservice on lumen 10 using https://github.com/wadaahmad/lumen-passport. I have installed passport and configured it with a custom logic and everything is working as expected but my phpunit tests are failing on an unusual error. It fails on test environment (phpunit) but it works when I make direct http calls to the login API route.
Here is my PassportTestCase.php file content:
<?php
namespace Tests;
use App\Models\User;
use Laravel\Passport\ClientRepository;
use Tests\TestCase;
use Laravel\Lumen\Testing\DatabaseMigrations;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
abstract class PassportTestCase extends TestCase
{
use DatabaseMigrations;
protected $headers = [];
protected $scopes = [];
protected $user;
public function setUp(): void
{
parent::setUp();
// $this->artisan('passport:install');
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null,
'Test Personal Access Client',
"http://localhost"
);
DB::table('oauth_personal_access_clients')->insert([
'client_id' => $client->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
$this->user = User::factory()->create();
$this->user->phone_verified_at = Carbon::now();
$this->user->email_verified_at = Carbon::now();
$this->user->login_token = "abcdef";
$this->user->login_token_expires_at = Carbon::now()->addMinutes(30);
$this->user->save();
$token = $this->user->createToken('TestToken', $this->scopes)->accessToken;
$this->headers['Accept'] = 'application/json';
$this->headers['Authorization'] = 'Bearer ' . $token;
}
protected function guestApi($method, $uri, array $data = [], array $headers = [])
{
return $this->json($method, $uri, $data, $headers);
}
protected function api($method, $uri, array $data = [], array $headers = [])
{
return $this->json($method, $uri, $data, array_merge($this->headers, $headers));
}
}
And here is my PassportTest.php:
<?php
use Tests\TestCase;
use Laravel\Lumen\Testing\DatabaseMigrations;
use App\Models\User;
use App\Models\Country;
use App\Models\Verification;
use Tests\PassportTestCase;
use Carbon\Carbon;
class PassportTest extends PassportTestCase
{
use DatabaseMigrations;
public function test_it_can_login_if_phone_and_email_verified()
{
$this->guestApi("post", "auth/login", ["email" => $this->user->email, "password" => "abcdef"])
->seeJson([
"token_type" => "bearer"
])
->assertResponseStatus(200);
}
}
Here is the error when I run phpunit:
1) PassportTest::test_it_can_login_if_phone_and_email_verified
Error: Call to a member function getKey() on null
C:\Users\Kamasah Dickson\Jean\projects\satsatai-workspace\apps\ms-user\vendor\laravel\passport\src\PersonalAccessTokenFactory.php:101
C:\Users\Kamasah Dickson\Jean\projects\satsatai-workspace\apps\ms-user\vendor\laravel\passport\src\PersonalAccessTokenFactory.php:72
C:\Users\Kamasah Dickson\Jean\projects\satsatai-workspace\apps\ms-user\vendor\laravel\passport\src\HasApiTokens.php:66
C:\Users\Kamasah Dickson\Jean\projects\satsatai-workspace\apps\ms-user\tests\PassportTestCase.php:44
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Tracing the error: this is PassportTestCase.php:44
$token = $this->user->createToken('TestToken', $this->scopes)->accessToken;
This is HasApiTokens.php:66:
public function createToken($name, array $scopes = [])
{
return Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
$this->getKey(),
$name,
$scopes
);
}
so the Trait HasApiTokens which I have on my user model to use passport is not getting the current user object when $this->getKey() is called in first argument to make() function.
I have been scratching my head over this because it works over the API but phpunit tests are failing so it has got something to do with test environment.
Thanks in advance.