|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flownative\OAuth2\Client; |
| 5 | + |
| 6 | +require_once('Fixtures/OAuthTestClient.php'); |
| 7 | + |
| 8 | +/* |
| 9 | + * This file is part of the Flownative.OAuth2.Client package. |
| 10 | + * |
| 11 | + * (c) Robert Lemke, Flownative GmbH - www.flownative.com |
| 12 | + * |
| 13 | + * This package is Open Source Software. For the full copyright and license |
| 14 | + * information, please view the LICENSE file which was distributed with this |
| 15 | + * source code. |
| 16 | + */ |
| 17 | + |
| 18 | +use Doctrine\ORM\EntityManagerInterface; |
| 19 | +use Doctrine\Persistence\ObjectRepository; |
| 20 | +use Flownative\OAuth2\Client\Tests\Unit\Fixtures\OAuthTestClient; |
| 21 | +use Neos\Flow\ObjectManagement\ObjectManagerInterface; |
| 22 | +use Neos\Flow\Persistence\Repository; |
| 23 | +use Neos\Flow\Tests\UnitTestCase; |
| 24 | + |
| 25 | +class OAuthClientTest extends UnitTestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @test |
| 29 | + */ |
| 30 | + public function constructorSetsServiceName(): void |
| 31 | + { |
| 32 | + $client = new OAuthTestClient('my-service-name'); |
| 33 | + self::assertSame('my-service-name', $client->getServiceName()); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @test |
| 38 | + */ |
| 39 | + public function baseUriIsUsedForConstructingEndpointUris(): void |
| 40 | + { |
| 41 | + $client = new OAuthTestClient('my-service-name'); |
| 42 | + |
| 43 | + $actualUri = $client->getAccessTokenUri(); |
| 44 | + $expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token'; |
| 45 | + self::assertSame($expectedUri, $actualUri); |
| 46 | + |
| 47 | + $actualUri = $client->getAuthorizeTokenUri(); |
| 48 | + $expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token/authorize'; |
| 49 | + self::assertSame($expectedUri, $actualUri); |
| 50 | + |
| 51 | + $actualUri = $client->getResourceOwnerUri(); |
| 52 | + $expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token/resource'; |
| 53 | + self::assertSame($expectedUri, $actualUri); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @test |
| 58 | + */ |
| 59 | + public function generateAuthorizationIdQueryParameterName(): void |
| 60 | + { |
| 61 | + self::assertSame('flownative_oauth2_authorization_id_test-service-type', OAuthTestClient::generateAuthorizationIdQueryParameterName('test-service-type')); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @test |
| 66 | + */ |
| 67 | + public function getAuthorizationFetchesAuthorizationFromRepository(): void |
| 68 | + { |
| 69 | + $authorizationId = '3d47f0eafd6a8b49e32b55103d817b6e4ef489e7'; |
| 70 | + $expectedAuthorization = new Authorization($authorizationId, 'service', 'clientId',Authorization::GRANT_AUTHORIZATION_CODE, 'profile'); |
| 71 | + |
| 72 | + $mockRepository = $this->createMock(ObjectRepository::class); |
| 73 | + $mockRepository->method('find')->with(['authorizationId' => $authorizationId])->willReturn($expectedAuthorization); |
| 74 | + |
| 75 | + $mockEntityManager = $this->createMock(EntityManagerInterface::class); |
| 76 | + $mockEntityManager->method('getRepository')->with(Authorization::class)->willReturn($mockRepository); |
| 77 | + |
| 78 | + $client = new OAuthTestClient('my-service-name'); |
| 79 | + $client->injectEntityManager($mockEntityManager); |
| 80 | + |
| 81 | + $actualAuthorization = $client->getAuthorization($authorizationId); |
| 82 | + self::assertSame($expectedAuthorization, $actualAuthorization); |
| 83 | + } |
| 84 | +} |
0 commit comments