-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from wimski/test-suite-setup
Setup test suite with tests for Logout mutation
- Loading branch information
Showing
13 changed files
with
282 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Integration; | ||
|
||
use DanielDeWit\LighthouseSanctum\Providers\LighthouseSanctumServiceProvider; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Laravel\Sanctum\SanctumServiceProvider; | ||
use Nuwave\Lighthouse\LighthouseServiceProvider; | ||
use Nuwave\Lighthouse\Testing\MakesGraphQLRequests; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
abstract class AbstractIntegrationTest extends TestCase | ||
{ | ||
use MakesGraphQLRequests; | ||
use RefreshDatabase; | ||
|
||
/** | ||
* @param Application $app | ||
* @return string[] | ||
*/ | ||
protected function getPackageProviders($app): array | ||
{ | ||
return [ | ||
LighthouseSanctumServiceProvider::class, | ||
LighthouseServiceProvider::class, | ||
SanctumServiceProvider::class, | ||
]; | ||
} | ||
|
||
protected function defineDatabaseMigrations(): void | ||
{ | ||
$this->loadLaravelMigrations(); | ||
} | ||
|
||
/** | ||
* @param Application $app | ||
*/ | ||
protected function defineEnvironment($app): void | ||
{ | ||
$app['config']->set('lighthouse.schema.register', $this->getStubsPath('schema.graphql')); | ||
$app['config']->set('lighthouse.guard', 'sanctum'); | ||
} | ||
|
||
protected function getStubsPath(string $path): string | ||
{ | ||
return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . $path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Integration\GraphQL\Mutations; | ||
|
||
use DanielDeWit\LighthouseSanctum\GraphQL\Mutations\Logout; | ||
use DanielDeWit\LighthouseSanctum\Tests\Integration\AbstractIntegrationTest; | ||
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
use Illuminate\Contracts\Translation\Translator; | ||
use Laravel\Sanctum\Sanctum; | ||
|
||
class LogoutTest extends AbstractIntegrationTest | ||
{ | ||
protected Logout $mutation; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mutation = new Logout( | ||
$this->app->make(AuthFactory::class), | ||
$this->app->make(Translator::class), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_logs_a_user_out(): void | ||
{ | ||
Sanctum::actingAs(UserHasApiTokens::factory()->create()); | ||
|
||
$this->graphQL(/** @lang GraphQL */' | ||
mutation { | ||
logout { | ||
status | ||
message | ||
} | ||
} | ||
')->assertJson([ | ||
'data' => [ | ||
'logout' => [ | ||
'status' => 'TOKEN_REVOKED', | ||
'message' => 'Your session has been terminated', | ||
], | ||
], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Unit; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class AbstractUnitTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Unit\GraphQL\Mutations; | ||
|
||
use DanielDeWit\LighthouseSanctum\Enums\LogoutStatus; | ||
use DanielDeWit\LighthouseSanctum\GraphQL\Mutations\Logout; | ||
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens; | ||
use DanielDeWit\LighthouseSanctum\Tests\Unit\AbstractUnitTest; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
use Illuminate\Contracts\Auth\Guard; | ||
use Illuminate\Contracts\Translation\Translator; | ||
use Laravel\Sanctum\PersonalAccessToken; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
|
||
class LogoutTest extends AbstractUnitTest | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_log_a_user_out(): void | ||
{ | ||
/** @var PersonalAccessToken|MockInterface $token */ | ||
$token = Mockery::mock(PersonalAccessToken::class) | ||
->shouldReceive('delete') | ||
->getMock(); | ||
|
||
/** @var UserHasApiTokens|MockInterface $user */ | ||
$user = Mockery::mock(UserHasApiTokens::class) | ||
->shouldReceive('currentAccessToken') | ||
->andReturn($token) | ||
->getMock(); | ||
|
||
/** @var Translator|MockInterface $translator */ | ||
$translator = Mockery::mock(Translator::class) | ||
->shouldReceive('get') | ||
->with('Your session has been terminated') | ||
->andReturn('Translated string!') | ||
->getMock(); | ||
|
||
$mutation = new Logout($this->mockAuthFactory($user), $translator); | ||
|
||
$result = $mutation(null, []); | ||
|
||
static::assertIsArray($result); | ||
static::assertCount(2, $result); | ||
static::assertTrue(LogoutStatus::TOKEN_REVOKED()->is($result['status'])); | ||
static::assertSame('Translated string!', $result['message']); | ||
} | ||
|
||
/** | ||
* @param Authenticatable|MockInterface|null $user | ||
* @return AuthFactory|MockInterface | ||
*/ | ||
protected function mockAuthFactory($user = null) | ||
{ | ||
/** @var Guard|MockInterface $guard */ | ||
$guard = Mockery::mock(Guard::class) | ||
->shouldReceive('user') | ||
->andReturn($user) | ||
->getMock(); | ||
|
||
/** @var AuthFactory|MockInterface $authFactory */ | ||
$authFactory = Mockery::mock(AuthFactory::class) | ||
->shouldReceive('guard') | ||
->with('sanctum') | ||
->andReturn($guard) | ||
->getMock(); | ||
|
||
return $authFactory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\stubs\Users; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Foundation\Auth\User; | ||
use Laravel\Sanctum\HasApiTokens; | ||
|
||
class UserHasApiTokens extends User | ||
{ | ||
use HasApiTokens; | ||
use HasFactory; | ||
|
||
protected $table = 'users'; | ||
|
||
protected static function newFactory(): Factory | ||
{ | ||
return new UserHasApiTokensFactory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\stubs\Users; | ||
|
||
use Orchestra\Testbench\Factories\UserFactory; | ||
|
||
class UserHasApiTokensFactory extends UserFactory | ||
{ | ||
protected $model = UserHasApiTokens::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\stubs\Users; | ||
|
||
use Illuminate\Contracts\Auth\MustVerifyEmail; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class UserMustVerifyEmail extends UserHasApiTokens implements MustVerifyEmail | ||
{ | ||
protected static function newFactory(): Factory | ||
{ | ||
return new UserMustVerifyEmailFactory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\stubs\Users; | ||
|
||
use Orchestra\Testbench\Factories\UserFactory; | ||
|
||
class UserMustVerifyEmailFactory extends UserFactory | ||
{ | ||
protected $model = UserMustVerifyEmail::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type Query | ||
|
||
type Mutation | ||
|
||
#import ../../graphql/sanctum.graphql |