Skip to content

Commit

Permalink
Merge pull request #3 from wimski/test-suite-setup
Browse files Browse the repository at this point in the history
Setup test suite with tests for Logout mutation
  • Loading branch information
daniel-de-wit committed Apr 16, 2021
2 parents 654910d + 7ea596f commit ccdb397
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 25 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"nuwave/lighthouse": "^5.0"
},
"require-dev": {
"mockery/mockery": "^1.3.3",
"nunomaduro/larastan": "^0.7.2",
"orchestra/testbench": "^6.0",
"orchestra/testbench": "^6.5",
"phpstan/phpstan-mockery": "^0.12.13",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpunit/phpunit": "^9.5",
Expand All @@ -43,7 +44,8 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit",
"phpstan": "php ./vendor/phpstan/phpstan/phpstan analyse --memory-limit 1G"
},
"config": {
"sort-packages": true
Expand Down
20 changes: 14 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
Expand All @@ -10,20 +10,28 @@
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
verbose="true"
>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
processUncoveredFiles="true"
>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
9 changes: 6 additions & 3 deletions src/GraphQL/Mutations/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
use DanielDeWit\LighthouseSanctum\Enums\LogoutStatus;
use Exception;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Translation\Translator;
use Laravel\Sanctum\PersonalAccessToken;
use RuntimeException;

class Logout
{
protected AuthFactory $authFactory;
protected Translator $translator;

public function __construct(AuthFactory $authFactory)
public function __construct(AuthFactory $authFactory, Translator $translator)
{
$this->authFactory = $authFactory;
$this->translator = $translator;
}

/**
Expand Down Expand Up @@ -42,8 +45,8 @@ public function __invoke($_, array $args): array
$personalAccessToken->delete();

return [
'status' => LogoutStatus::TOKEN_REVOKED,
'message' => __('Your session has been terminated'),
'status' => LogoutStatus::TOKEN_REVOKED(),
'message' => $this->translator->get('Your session has been terminated'),
];
}
}
51 changes: 51 additions & 0 deletions tests/Integration/AbstractIntegrationTest.php
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;
}
}
51 changes: 51 additions & 0 deletions tests/Integration/GraphQL/Mutations/LogoutTest.php
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',
],
],
]);
}
}
13 changes: 13 additions & 0 deletions tests/Unit/AbstractUnitTest.php
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;
}
14 changes: 0 additions & 14 deletions tests/Unit/ExampleTest.php

This file was deleted.

75 changes: 75 additions & 0 deletions tests/Unit/GraphQL/Mutations/LogoutTest.php
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;
}
}
23 changes: 23 additions & 0 deletions tests/stubs/Users/UserHasApiTokens.php
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();
}
}
12 changes: 12 additions & 0 deletions tests/stubs/Users/UserHasApiTokensFactory.php
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;
}
16 changes: 16 additions & 0 deletions tests/stubs/Users/UserMustVerifyEmail.php
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();
}
}
12 changes: 12 additions & 0 deletions tests/stubs/Users/UserMustVerifyEmailFactory.php
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;
}
5 changes: 5 additions & 0 deletions tests/stubs/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Query

type Mutation

#import ../../graphql/sanctum.graphql

0 comments on commit ccdb397

Please sign in to comment.