From faedf2ebab6a4bd9869372771b7e19e92a5c1d17 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Fri, 19 Jul 2024 20:46:14 +0330 Subject: [PATCH] fix tests --- tests/Feature/AccessTokenControllerTest.php | 2 +- tests/Feature/HasApiTokensTest.php | 2 +- .../PersonalAccessTokenFactoryTest.php | 28 +++++++++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tests/Feature/AccessTokenControllerTest.php b/tests/Feature/AccessTokenControllerTest.php index 4e519bce..7f96ade3 100644 --- a/tests/Feature/AccessTokenControllerTest.php +++ b/tests/Feature/AccessTokenControllerTest.php @@ -145,7 +145,7 @@ public function testGettingAccessTokenWithPasswordGrant() $token = $this->app->make(PersonalAccessTokenFactory::class)->findAccessToken($decodedResponse); $this->assertInstanceOf(Token::class, $token); $this->assertFalse($token->revoked); - $this->assertTrue($token->user->is($user)); + $this->assertSame($user->getAuthIdentifier(), $token->user_id); $this->assertTrue($token->client->is($client)); $this->assertNull($token->name); $this->assertLessThanOrEqual(5, CarbonImmutable::now()->addSeconds($expiresInSeconds)->diffInSeconds($token->expires_at)); diff --git a/tests/Feature/HasApiTokensTest.php b/tests/Feature/HasApiTokensTest.php index b3b1b215..94d5e18a 100644 --- a/tests/Feature/HasApiTokensTest.php +++ b/tests/Feature/HasApiTokensTest.php @@ -16,7 +16,7 @@ public function testGetProvider() config([ 'auth.providers.admins' => ['driver' => 'eloquent', 'model' => AdminHasApiTokensStub::class], 'auth.guards.api-admins' => ['driver' => 'passport', 'provider' => 'admins'], - 'auth.providers.customers' => ['driver' => 'database', 'table' => 'customer_has_api_tokens_stubs'], + 'auth.providers.customers' => ['driver' => 'eloquent', 'model' => CustomerHasApiTokensStub::class], 'auth.guards.api-customers' => ['driver' => 'passport', 'provider' => 'customers'], ]); diff --git a/tests/Feature/PersonalAccessTokenFactoryTest.php b/tests/Feature/PersonalAccessTokenFactoryTest.php index 97d6997d..b23dfc2c 100644 --- a/tests/Feature/PersonalAccessTokenFactoryTest.php +++ b/tests/Feature/PersonalAccessTokenFactoryTest.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Support\Facades\DB; use Laravel\Passport\Client; use Laravel\Passport\Database\Factories\ClientFactory; use Laravel\Passport\HasApiTokens; @@ -53,7 +54,7 @@ public function testIssueTokenWithDifferentProviders() config([ 'auth.providers.admins' => ['driver' => 'eloquent', 'model' => AdminProviderStub::class], 'auth.guards.api-admins' => ['driver' => 'passport', 'provider' => 'admins'], - 'auth.providers.customers' => ['driver' => 'database', 'table' => 'customer_provider_stubs'], + 'auth.providers.customers' => ['driver' => 'eloquent', 'model' => CustomerProviderStub::class], 'auth.guards.api-customers' => ['driver' => 'passport', 'provider' => 'customers'], 'passport.personal_access_client' => ['id' => $client->getKey(), 'secret' => $client->plainSecret], 'passport.personal_access_client.admins' => ['id' => $adminClient->getKey(), 'secret' => $adminClient->plainSecret], @@ -80,6 +81,21 @@ public function testIssueTokenWithDifferentProviders() $this->assertInstanceOf(PersonalAccessTokenResult::class, $customerToken); $this->assertSame($customerClient->getKey(), $customerToken->token->client_id); $this->assertSame($customer->getAuthIdentifier(), $customerToken->token->user_id); + + DB::enableQueryLog(); + $userTokens = $user->tokens()->pluck('id')->all(); + $adminTokens = $admin->tokens()->pluck('id')->all(); + $customerTokens = $customer->tokens()->pluck('id')->all(); + DB::disableQueryLog(); + + $queries = DB::getRawQueryLog(); + $this->assertStringContainsString('and ("provider" is null or "provider" = \'users\')', $queries[0]['raw_query']); + $this->assertStringContainsString('and ("provider" = \'admins\')', $queries[1]['raw_query']); + $this->assertStringContainsString('and ("provider" = \'customers\')', $queries[2]['raw_query']); + + $this->assertEquals([$userToken->token->id], $userTokens); + $this->assertEquals([$adminToken->token->id], $adminTokens); + $this->assertEquals([$customerToken->token->id], $customerTokens); } } @@ -87,18 +103,12 @@ class AdminProviderStub extends Authenticatable { use HasApiTokens; - public function getAuthIdentifier() - { - return 'foo'; - } + protected $attributes = ['id' => 1]; } class CustomerProviderStub extends Authenticatable { use HasApiTokens; - public function getAuthIdentifier() - { - return 3; - } + protected $attributes = ['id' => 3]; }