From 4da494debc4ff8602b91703df81333a43512f3f8 Mon Sep 17 00:00:00 2001 From: Adrian Dumitrache Date: Wed, 25 Sep 2024 18:06:34 +0300 Subject: [PATCH] Deprecate HttpTester; refactor remaining tests --- .../Authenticator/JwtAuthenticatorTest.php | 35 ++++++------------ .../Tests/Mock/MockableUserInterface.php | 12 ++++++ packages/tester/.phpstorm.meta.php | 7 +--- packages/tester/HttpTesterTrait.php | 3 ++ packages/tester/MockTrait.php | 19 ---------- .../tester/Tests/Http/ClientObserverTest.php | 37 ------------------- phpstan.dist.neon | 3 +- phpstan.neon | 3 +- 8 files changed, 30 insertions(+), 89 deletions(-) create mode 100644 packages/security/Tests/Mock/MockableUserInterface.php delete mode 100644 packages/tester/Tests/Http/ClientObserverTest.php diff --git a/packages/security/Tests/Http/Authenticator/JwtAuthenticatorTest.php b/packages/security/Tests/Http/Authenticator/JwtAuthenticatorTest.php index 44ae1e854..9b9ecf749 100644 --- a/packages/security/Tests/Http/Authenticator/JwtAuthenticatorTest.php +++ b/packages/security/Tests/Http/Authenticator/JwtAuthenticatorTest.php @@ -6,6 +6,7 @@ use Draw\Component\Security\Http\Authenticator\JwtAuthenticator; use Draw\Component\Security\Http\Authenticator\Passport\Badge\JwtPayloadBadge; use Draw\Component\Security\Jwt\JwtEncoder; +use Draw\Component\Security\Tests\Mock\MockableUserInterface; use Draw\Component\Tester\MockTrait; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\MockObject; @@ -39,10 +40,10 @@ class JwtAuthenticatorTest extends TestCase private string $userIdentifierPayloadKey; - private string $userIdentifierGetter; - private TranslatorInterface&MockObject $translator; + private static string $userIdentifierGetter = 'getIdentifierForJwtAuthenticatorTest'; + protected function setUp(): void { $this->userProvider = $this->createMock(UserProviderInterface::class); @@ -51,7 +52,7 @@ protected function setUp(): void $this->jwtEncoder = $this->createMock(JwtEncoder::class), $this->userProvider, $this->userIdentifierPayloadKey = uniqid('key'), - $this->userIdentifierGetter = uniqid('get'), + self::$userIdentifierGetter, $this->translator = $this->createMock(TranslatorInterface::class) ); } @@ -92,14 +93,11 @@ public function testSupportsNoToken(): void public function testGenerateToken(): void { - $user = $this->createMockWithExtraMethods( - UserInterface::class, - [$this->userIdentifierGetter] - ); + $user = $this->createMock(MockableUserInterface::class); $user ->expects(static::once()) - ->method($this->userIdentifierGetter) + ->method(self::$userIdentifierGetter) ->willReturn($userId = uniqid('id')); $this->jwtEncoder @@ -125,14 +123,11 @@ public function testGenerateTokenDefaultNull(): void null ); - $user = $this->createMockWithExtraMethods( - UserInterface::class, - [$this->userIdentifierGetter] - ); + $user = $this->createMock(MockableUserInterface::class); $user ->expects(static::once()) - ->method($this->userIdentifierGetter) + ->method(self::$userIdentifierGetter) ->willReturn($userId = uniqid('id')); $this->jwtEncoder @@ -151,14 +146,11 @@ public function testGenerateTokenDefaultNull(): void public function testGenerateTokenWithExpiration(): void { - $user = $this->createMockWithExtraMethods( - UserInterface::class, - [$this->userIdentifierGetter] - ); + $user = $this->createMock(MockableUserInterface::class); $user ->expects(static::once()) - ->method($this->userIdentifierGetter) + ->method(self::$userIdentifierGetter) ->willReturn($userId = uniqid('id')); $this->jwtEncoder @@ -180,14 +172,11 @@ public function testGenerateTokenWithExpiration(): void public function testGenerateTokenWithExtraPayload(): void { - $user = $this->createMockWithExtraMethods( - UserInterface::class, - [$this->userIdentifierGetter] - ); + $user = $this->createMock(MockableUserInterface::class); $user ->expects(static::once()) - ->method($this->userIdentifierGetter) + ->method(self::$userIdentifierGetter) ->willReturn($userId = uniqid('id')); $extraPayload = [ diff --git a/packages/security/Tests/Mock/MockableUserInterface.php b/packages/security/Tests/Mock/MockableUserInterface.php new file mode 100644 index 000000000..39bea0063 --- /dev/null +++ b/packages/security/Tests/Mock/MockableUserInterface.php @@ -0,0 +1,12 @@ + "@"]) -); - override( \Draw\Component\Tester\MockTrait::mockProperty(2), map([""=>"$2"]) -); \ No newline at end of file +); diff --git a/packages/tester/HttpTesterTrait.php b/packages/tester/HttpTesterTrait.php index 16496dd2b..d55126f0c 100644 --- a/packages/tester/HttpTesterTrait.php +++ b/packages/tester/HttpTesterTrait.php @@ -5,6 +5,9 @@ use Draw\Component\Tester\Http\ClientInterface; use PHPUnit\Framework\Attributes\BeforeClass; +/** + * @deprecated + */ trait HttpTesterTrait { protected static ?ClientInterface $httpTesterClient = null; diff --git a/packages/tester/MockTrait.php b/packages/tester/MockTrait.php index 9b37af0c5..05ec5f8df 100644 --- a/packages/tester/MockTrait.php +++ b/packages/tester/MockTrait.php @@ -14,25 +14,6 @@ abstract public function getMockBuilder(string $className): MockBuilder; abstract protected function createMock(string $originalClassName): MockObject; - /** - * @template T of object - * - * @phpstan-param class-string $originalClassName - * - * @return MockObject&T - */ - protected function createMockWithExtraMethods(string $originalClassName, array $methods): MockObject - { - return $this->getMockBuilder($originalClassName) - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->onlyMethods(get_class_methods($originalClassName)) - ->addMethods($methods) - ->getMock(); - } - /** * @template T of object * diff --git a/packages/tester/Tests/Http/ClientObserverTest.php b/packages/tester/Tests/Http/ClientObserverTest.php deleted file mode 100644 index 080c540ae..000000000 --- a/packages/tester/Tests/Http/ClientObserverTest.php +++ /dev/null @@ -1,37 +0,0 @@ -getMockForAbstractClass(ClientObserver::class); - - $request = new Request('GET', '/test'); - - /* @var ClientObserver $clientObserver */ - static::assertSame( - $request, - $clientObserver->preSendRequest($request) - ); - } - - public function testPostSendRequest(): void - { - $clientObserver = $this->getMockForAbstractClass(ClientObserver::class); - - $response = new Response(200); - - /* @var ClientObserver $clientObserver */ - static::assertSame( - $response, - $clientObserver->postSendRequest(new Request('GET', '/test'), $response) - ); - } -} diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 4b604b9ee..79260eec4 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -11,11 +11,10 @@ parameters: - packages/fixer/Tests/fixtures/* ignoreErrors: - '#Call to an undefined method Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag\(\).#' - - '#Trying to mock an undefined method [a-zA-Z0-9\\_]#' #- '#provideTestArgument\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#provideTestOption\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#getHandledMessages\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#Test::getDefaultConfiguration\(\) return type has no value type specified in iterable type array.#' #- '#Test::getConfiguration\(\) return type has no value type specified in iterable type array.#' #- '#Test::provide[a-zA-Z0-9]*\(\) return type has no value type specified in iterable type (iterable|array).#' - #- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#' \ No newline at end of file + #- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#' diff --git a/phpstan.neon b/phpstan.neon index 0a8fecc07..e52692867 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,11 +13,10 @@ parameters: - packages/fixer/Tests/fixtures/* ignoreErrors: - '#Call to an undefined method Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag\(\).#' - - '#Trying to mock an undefined method [a-zA-Z0-9\\_]#' #- '#provideTestArgument\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#provideTestOption\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#getHandledMessages\(\) return type has no value type specified in iterable type (iterable|array).#' #- '#Test::getDefaultConfiguration\(\) return type has no value type specified in iterable type array.#' #- '#Test::getConfiguration\(\) return type has no value type specified in iterable type array.#' #- '#Test::provide[a-zA-Z0-9]*\(\) return type has no value type specified in iterable type (iterable|array).#' - #- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#' \ No newline at end of file + #- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#'