-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve how InjectUser handles unauthenticated user (#575)
- Loading branch information
1 parent
f5bed7c
commit b5af49e
Showing
7 changed files
with
161 additions
and
4 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
61 changes: 61 additions & 0 deletions
61
tests/Mappers/Parameters/InjectUserParameterHandlerTest.php
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,61 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Parameters; | ||
|
||
use Generator; | ||
use phpDocumentor\Reflection\DocBlock; | ||
use ReflectionMethod; | ||
use stdClass; | ||
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest; | ||
use TheCodingMachine\GraphQLite\Annotations\InjectUser; | ||
use TheCodingMachine\GraphQLite\Parameters\InjectUserParameter; | ||
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface; | ||
|
||
class InjectUserParameterHandlerTest extends AbstractQueryProviderTest | ||
{ | ||
/** | ||
* @dataProvider mapParameterProvider | ||
*/ | ||
public function testMapParameter(bool $optional, string $method): void | ||
{ | ||
$authenticationService = $this->createMock(AuthenticationServiceInterface::class); | ||
|
||
$refMethod = new ReflectionMethod(__CLASS__, $method); | ||
$parameter = $refMethod->getParameters()[0]; | ||
|
||
$mapped = (new InjectUserParameterHandler($authenticationService))->mapParameter( | ||
$parameter, | ||
new DocBlock(), | ||
null, | ||
$this->getAnnotationReader()->getParameterAnnotationsPerParameter([$parameter])['user'], | ||
$this->createMock(ParameterHandlerInterface::class), | ||
); | ||
|
||
self::assertEquals( | ||
new InjectUserParameter($authenticationService, $optional), | ||
$mapped | ||
); | ||
} | ||
|
||
public function mapParameterProvider(): Generator | ||
{ | ||
yield 'required user' => [false, 'requiredUser']; | ||
yield 'optional user' => [true, 'optionalUser']; | ||
yield 'missing type' => [true, 'missingType']; | ||
} | ||
|
||
private function requiredUser( | ||
#[InjectUser] stdClass $user, | ||
) { | ||
} | ||
|
||
private function optionalUser( | ||
#[InjectUser] stdClass|null $user, | ||
) { | ||
} | ||
|
||
private function missingType( | ||
#[InjectUser] $user, | ||
) { | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\GraphQLite\Parameters; | ||
|
||
use Generator; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use TheCodingMachine\GraphQLite\Middlewares\MissingAuthorizationException; | ||
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface; | ||
|
||
class InjectUserParameterTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider resolveReturnsUserProvider | ||
*/ | ||
public function testResolveReturnsUser(stdClass|null $user, bool $optional): void | ||
{ | ||
$authenticationService = $this->createMock(AuthenticationServiceInterface::class); | ||
$authenticationService->method('getUser') | ||
->willReturn($user); | ||
|
||
$resolved = (new InjectUserParameter($authenticationService, $optional))->resolve( | ||
null, | ||
[], | ||
null, | ||
$this->createStub(ResolveInfo::class) | ||
); | ||
|
||
self::assertSame($user, $resolved); | ||
} | ||
|
||
public function resolveReturnsUserProvider(): Generator | ||
{ | ||
yield 'non optional and has user' => [new stdClass(), false]; | ||
yield 'optional and has user' => [new stdClass(), true]; | ||
yield 'optional and doesnt have user' => [null, true]; | ||
} | ||
|
||
public function testThrowsMissingAuthorization(): void | ||
{ | ||
$authenticationService = $this->createMock(AuthenticationServiceInterface::class); | ||
$authenticationService->method('getUser') | ||
->willReturn(null); | ||
|
||
$this->expectExceptionObject(MissingAuthorizationException::unauthorized()); | ||
|
||
(new InjectUserParameter($authenticationService, false))->resolve( | ||
null, | ||
[], | ||
null, | ||
$this->createStub(ResolveInfo::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
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