From d70200eaf7fba93f709f6e0a3f1978b9fdb160c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Wed, 3 Dec 2025 11:30:10 +0100 Subject: [PATCH] Updated PhpUnit & Fixed all deprecations --- composer.json | 2 +- phpunit.xml | 13 ++++---- tests/Command/ArchiveTokenCommandTest.php | 5 ++-- .../DependencyInjectionTest.php | 13 ++++---- tests/Entity/TokenTest.php | 4 +-- tests/Factory/TokenFactoryTest.php | 30 +++++++++++++------ tests/Generator/OpenSslTokenGeneratorTest.php | 2 +- .../InformationGuesserTest.php | 4 +-- tests/Manager/ChainUserManagerTest.php | 26 +++++++++------- tests/Manager/DoctrineUserManagerTest.php | 10 +++---- tests/Manager/TokenManagerTest.php | 14 ++++----- .../DoctrineORMTokenRepositoryTest.php | 14 ++++----- 12 files changed, 78 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index b3df4af..3332066 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "yokai/dependency-injection": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^10.0|12.0", "symfony/yaml": "^6.4|^7.4|^8.0", "phpstan/phpstan": "^2.1", "symplify/easy-coding-standard": "^13.0" diff --git a/phpunit.xml b/phpunit.xml index 0c6888c..44153ed 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,13 +2,14 @@ @@ -18,9 +19,9 @@ ./tests - + ./src - + diff --git a/tests/Command/ArchiveTokenCommandTest.php b/tests/Command/ArchiveTokenCommandTest.php index cac4188..25df0a3 100644 --- a/tests/Command/ArchiveTokenCommandTest.php +++ b/tests/Command/ArchiveTokenCommandTest.php @@ -41,6 +41,7 @@ protected function setUp(): void protected function tearDown(): void { parent::tearDown(); + \restore_exception_handler(); unset( $this->archivist, @@ -65,7 +66,7 @@ protected function runCommand(Command $command, array $options = []): string return $tester->getDisplay(); } - public function testIt_archive_every_token_when_run_without_options_with_confirmation(): void + public function test_it_archive_every_token_when_run_without_options_with_confirmation(): void { $command = $this->command(); @@ -79,7 +80,7 @@ public function testIt_archive_every_token_when_run_without_options_with_confirm self::assertStringContainsString('Successfully archived 10 security token(s).', $output); } - public function testIt_archive_partial_tokens_when_run_with_options(): void + public function test_it_archive_partial_tokens_when_run_with_options(): void { $command = $this->command(); diff --git a/tests/DependencyInjection/DependencyInjectionTest.php b/tests/DependencyInjection/DependencyInjectionTest.php index 4a3e93a..e2b53da 100644 --- a/tests/DependencyInjection/DependencyInjectionTest.php +++ b/tests/DependencyInjection/DependencyInjectionTest.php @@ -9,6 +9,7 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Generator; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Symfony\Component\Config\FileLocator; @@ -85,10 +86,8 @@ protected function setUp(): void $bundle->build($this->container); } - /** - * @dataProvider configurationProvider - */ - public function testIt_parse_configuration_as_expected(string $resource, array $tokens, array $aliases): void + #[DataProvider('configurationProvider')] + public function test_it_parse_configuration_as_expected(string $resource, array $tokens, array $aliases): void { // for test purpose, all services are switched to public $this->container->addCompilerPass(new class implements CompilerPassInterface { @@ -144,7 +143,7 @@ protected function loadConfiguration(string $resource): void $loader->load($resource); } - public function configurationProvider(): Generator + public static function configurationProvider(): Generator { $defaultAliases = [ 'yokai_security_token.information_guesser' => 'yokai_security_token.default_information_guesser', @@ -154,7 +153,7 @@ public function configurationProvider(): Generator 'yokai_security_token.archivist' => 'yokai_security_token.delete_archivist', ]; - foreach ($this->formatProvider() as $format) { + foreach (self::formatProvider() as $format) { $format = $format[0]; yield $format . ' - none' => [ @@ -205,7 +204,7 @@ public function configurationProvider(): Generator } } - public function formatProvider(): Generator + public static function formatProvider(): Generator { yield ['yml']; } diff --git a/tests/Entity/TokenTest.php b/tests/Entity/TokenTest.php index 62401fd..2c04e9f 100644 --- a/tests/Entity/TokenTest.php +++ b/tests/Entity/TokenTest.php @@ -14,7 +14,7 @@ */ final class TokenTest extends TestCase { - public function testIt_allow_limited_usage_token(): void + public function test_it_allow_limited_usage_token(): void { $token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month', 2); self::assertFalse($token->isConsumed()); @@ -29,7 +29,7 @@ public function testIt_allow_limited_usage_token(): void self::assertSame([2], $token->getLastUsage()->getInformation()); } - public function testIt_allow_unlimited_usage_token(): void + public function test_it_allow_unlimited_usage_token(): void { $token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month', 0); self::assertFalse($token->isConsumed()); diff --git a/tests/Factory/TokenFactoryTest.php b/tests/Factory/TokenFactoryTest.php index 8da21cd..da9d677 100644 --- a/tests/Factory/TokenFactoryTest.php +++ b/tests/Factory/TokenFactoryTest.php @@ -64,21 +64,33 @@ protected function factory(array $configuration): TokenFactory ); } - public function testIt_create_token_according_to_configuration(): void + public function test_it_create_token_according_to_configuration(): void { $generator1 = $this->createMock(TokenGeneratorInterface::class); - $generator1->method('generate') - ->will($this->onConsecutiveCalls('existtoken-1', 'uniquetoken-1')); + $generator1->expects($matcher = $this->atMost(2)) + ->method('generate') + ->willReturnCallback(fn() => match ($matcher->numberOfInvocations()) { + 1 => 'existtoken-1', + 2 => 'uniquetoken-1', + }); $user1 = 'user-1'; $generator2 = $this->createMock(TokenGeneratorInterface::class); - $generator2->method('generate') - ->will($this->onConsecutiveCalls('existtoken-2', 'uniquetoken-2')); + $generator2->expects($matcher = $this->atMost(2)) + ->method('generate') + ->willReturnCallback(fn() => match ($matcher->numberOfInvocations()) { + 1 => 'existtoken-2', + 2 => 'uniquetoken-2', + }); $user2 = 'user-2'; $generator3 = $this->createMock(TokenGeneratorInterface::class); - $generator3->method('generate') - ->will($this->onConsecutiveCalls('existtoken-3', 'uniquetoken-3')); + $generator3->expects($matcher = $this->atMost(2)) + ->method('generate') + ->willReturnCallback(fn() => match ($matcher->numberOfInvocations()) { + 1 => 'existtoken-3', + 2 => 'uniquetoken-3', + }); $user3 = 'user-3'; $token3FromRepository = new Token( 'string', @@ -111,7 +123,7 @@ public function testIt_create_token_according_to_configuration(): void $this->userManager->expects(self::exactly(3)) ->method('getClass') - ->with(self::isType('string')) + ->with(\method_exists(self::class, 'isString') ? self::isString() : self::isType('string')) ->willReturnMap([ [$user1, 'string'], [$user2, 'string'], @@ -120,7 +132,7 @@ public function testIt_create_token_according_to_configuration(): void $this->userManager->expects(self::exactly(3)) ->method('getId') - ->with(self::isType('string')) + ->with(\method_exists(self::class, 'isString') ? self::isString() : self::isType('string')) ->willReturnMap([ [$user1, 'u1'], [$user2, 'u2'], diff --git a/tests/Generator/OpenSslTokenGeneratorTest.php b/tests/Generator/OpenSslTokenGeneratorTest.php index efe1a72..a82e967 100644 --- a/tests/Generator/OpenSslTokenGeneratorTest.php +++ b/tests/Generator/OpenSslTokenGeneratorTest.php @@ -14,7 +14,7 @@ */ final class OpenSslTokenGeneratorTest extends TestCase { - public function testIt_generate_unique_token(): void + public function test_it_generate_unique_token(): void { $generator = new OpenSslTokenGenerator(); diff --git a/tests/InformationGuesser/InformationGuesserTest.php b/tests/InformationGuesser/InformationGuesserTest.php index 4010aed..120458b 100644 --- a/tests/InformationGuesser/InformationGuesserTest.php +++ b/tests/InformationGuesser/InformationGuesserTest.php @@ -21,7 +21,7 @@ protected function guesser(RequestStack $requestStack): InformationGuesser return new InformationGuesser($requestStack); } - public function testIt_return_empty_array_if_no_master_request(): void + public function test_it_return_empty_array_if_no_master_request(): void { $requestStack = new RequestStack(); @@ -30,7 +30,7 @@ public function testIt_return_empty_array_if_no_master_request(): void self::assertSame([], $info); } - public function testIt_return_array_with_ip_from_master_request(): void + public function test_it_return_array_with_ip_from_master_request(): void { $requestStack = new RequestStack(); $requestStack->push(new Request([], [], [], [], [], ['REMOTE_ADDR' => '88.88.88.88'])); diff --git a/tests/Manager/ChainUserManagerTest.php b/tests/Manager/ChainUserManagerTest.php index 3fae055..6a24e08 100644 --- a/tests/Manager/ChainUserManagerTest.php +++ b/tests/Manager/ChainUserManagerTest.php @@ -42,7 +42,10 @@ private function entityManager(): UserManagerInterface $manager->method('getId') ->willReturn('increment'); $manager->method('get') - ->with(UserEntity::class, self::isType('string')) + ->with( + UserEntity::class, + \method_exists(self::class, 'isString') ? self::isString() : self::isType('string'), + ) ->willReturn(new UserEntity()); return $manager; @@ -66,13 +69,16 @@ private function documentManager(): UserManagerInterface $manager->method('getId') ->willReturn('uuid'); $manager->method('get') - ->with(UserDocument::class, self::isType('string')) + ->with( + UserDocument::class, + \method_exists(self::class, 'isString') ? self::isString() : self::isType('string'), + ) ->willReturn(new UserDocument()); return $manager; } - public function testIt_supports_same_classes_as_managers(): void + public function test_it_supports_same_classes_as_managers(): void { $entityManager = $this->entityManager(); $documentManager = $this->documentManager(); @@ -97,7 +103,7 @@ public function testIt_supports_same_classes_as_managers(): void self::assertFalse($userEmptyManager->supportsClass($document)); } - public function testIt_supports_same_users_as_managers(): void + public function test_it_supports_same_users_as_managers(): void { $entityManager = $this->entityManager(); $documentManager = $this->documentManager(); @@ -122,7 +128,7 @@ public function testIt_supports_same_users_as_managers(): void self::assertFalse($userEmptyManager->supportsUser($document)); } - public function testIt_get_user_class_from_appropriate_manager(): void + public function test_it_get_user_class_from_appropriate_manager(): void { $entity = new UserEntity(); $document = new UserDocument(); @@ -132,7 +138,7 @@ public function testIt_get_user_class_from_appropriate_manager(): void self::assertSame(UserDocument::class, $userCompleteManager->getClass($document)); } - public function testIt_get_user_id_from_appropriate_manager() + public function test_it_get_user_id_from_appropriate_manager() { $entity = new UserEntity(); $document = new UserDocument(); @@ -142,7 +148,7 @@ public function testIt_get_user_id_from_appropriate_manager() self::assertSame('uuid', $userCompleteManager->getId($document)); } - public function testIt_get_user_from_appropriate_manager(): void + public function test_it_get_user_from_appropriate_manager(): void { $userCompleteManager = $this->manager([$this->entityManager(), $this->documentManager()]); self::assertInstanceOf(UserEntity::class, $userCompleteManager->get(UserEntity::class, '9999')); @@ -152,7 +158,7 @@ public function testIt_get_user_from_appropriate_manager(): void ); } - public function testIt_throw_exception_on_get_user_class_without_appropriate_manager(): void + public function test_it_throw_exception_on_get_user_class_without_appropriate_manager(): void { $this->expectException(InvalidArgumentException::class); @@ -160,7 +166,7 @@ public function testIt_throw_exception_on_get_user_class_without_appropriate_man $userCompleteManager->getClass(new \stdClass()); } - public function testIt_throw_exception_on_get_user_id_without_appropriate_manager(): void + public function test_it_throw_exception_on_get_user_id_without_appropriate_manager(): void { $this->expectException(InvalidArgumentException::class); @@ -168,7 +174,7 @@ public function testIt_throw_exception_on_get_user_id_without_appropriate_manage $userCompleteManager->getId(new \stdClass()); } - public function testIt_throw_exception_on_get_user_without_appropriate_manager(): void + public function test_it_throw_exception_on_get_user_without_appropriate_manager(): void { $this->expectException(InvalidArgumentException::class); diff --git a/tests/Manager/DoctrineUserManagerTest.php b/tests/Manager/DoctrineUserManagerTest.php index 196042f..9db7a20 100644 --- a/tests/Manager/DoctrineUserManagerTest.php +++ b/tests/Manager/DoctrineUserManagerTest.php @@ -72,7 +72,7 @@ public function getId() }; } - public function testIt_supports_doctrine_entities(): void + public function test_it_supports_doctrine_entities(): void { $user = $this->user('jdoe'); @@ -85,7 +85,7 @@ public function testIt_supports_doctrine_entities(): void self::assertTrue($manager->supportsUser($user)); } - public function testIt_do_not_supports_objects_out_of_doctrine(): void + public function test_it_do_not_supports_objects_out_of_doctrine(): void { $user = $this->user('jdoe'); @@ -98,7 +98,7 @@ public function testIt_do_not_supports_objects_out_of_doctrine(): void self::assertFalse($manager->supportsUser($user)); } - public function testIt_get_user(): void + public function test_it_get_user(): void { $expected = $this->user('jdoe'); @@ -117,7 +117,7 @@ public function testIt_get_user(): void self::assertSame($expected, $user); } - public function testIt_get_user_class(): void + public function test_it_get_user_class(): void { $expected = $this->user('jdoe'); @@ -126,7 +126,7 @@ public function testIt_get_user_class(): void self::assertSame(\get_class($expected), $class); } - public function testIt_get_user_id(): void + public function test_it_get_user_id(): void { $expected = $this->user('jdoe'); diff --git a/tests/Manager/TokenManagerTest.php b/tests/Manager/TokenManagerTest.php index ee12fa5..1d0be00 100644 --- a/tests/Manager/TokenManagerTest.php +++ b/tests/Manager/TokenManagerTest.php @@ -90,7 +90,7 @@ protected function manager(): TokenManager ); } - public function testIt_dispatch_not_found_exceptions_on_get_token_from_repository(): void + public function test_it_dispatch_not_found_exceptions_on_get_token_from_repository(): void { $this->expectException(TokenNotFoundException::class); @@ -111,7 +111,7 @@ public function testIt_dispatch_not_found_exceptions_on_get_token_from_repositor $this->manager()->get('forgot_password', 'unique-token'); } - public function testIt_dispatch_expired_exceptions_on_get_token_from_repository(): void + public function test_it_dispatch_expired_exceptions_on_get_token_from_repository(): void { $this->expectException(TokenExpiredException::class); @@ -132,7 +132,7 @@ public function testIt_dispatch_expired_exceptions_on_get_token_from_repository( $this->manager()->get('forgot_password', 'unique-token'); } - public function testIt_dispatch_used_exceptions_on_get_token_from_repository(): void + public function test_it_dispatch_used_exceptions_on_get_token_from_repository(): void { $this->expectException(TokenConsumedException::class); @@ -153,7 +153,7 @@ public function testIt_dispatch_used_exceptions_on_get_token_from_repository(): $this->manager()->get('forgot_password', 'unique-token'); } - public function testIt_get_token_from_repository(): void + public function test_it_get_token_from_repository(): void { $this->repository->expects(self::once()) ->method('get') @@ -173,7 +173,7 @@ public function testIt_get_token_from_repository(): void self::assertSame($expected, $token); } - public function testIt_create_unique_token(): void + public function test_it_create_unique_token(): void { $expectedToken = new Token( 'string', @@ -215,7 +215,7 @@ public function testIt_create_unique_token(): void self::assertSame($expectedToken, $token); } - public function testIt_consume_token(): void + public function test_it_consume_token(): void { $token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month'); @@ -252,7 +252,7 @@ public function testIt_consume_token(): void self::assertInstanceOf(\DateTime::class, $usage->getCreatedAt()); } - public function testIt_extract_user_from_token(): void + public function test_it_extract_user_from_token(): void { $token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month', 1, []); diff --git a/tests/Repository/DoctrineORMTokenRepositoryTest.php b/tests/Repository/DoctrineORMTokenRepositoryTest.php index 66cb302..206e2e5 100644 --- a/tests/Repository/DoctrineORMTokenRepositoryTest.php +++ b/tests/Repository/DoctrineORMTokenRepositoryTest.php @@ -50,7 +50,7 @@ protected function repository(): DoctrineORMTokenRepository return new DoctrineORMTokenRepository($this->manager, $this->repository); } - public function testIt_throw_exception_if_token_not_found(): void + public function test_it_throw_exception_if_token_not_found(): void { $this->expectException(TokenNotFoundException::class); @@ -62,7 +62,7 @@ public function testIt_throw_exception_if_token_not_found(): void $this->repository()->get('unique', 'init_password'); } - public function testIt_throw_exception_if_token_expired(): void + public function test_it_throw_exception_if_token_expired(): void { $this->expectException(TokenExpiredException::class); @@ -76,7 +76,7 @@ public function testIt_throw_exception_if_token_expired(): void $this->repository()->get('unique', 'init_password'); } - public function testIt_throw_exception_if_token_used_single_time(): void + public function test_it_throw_exception_if_token_used_single_time(): void { $this->expectException(TokenConsumedException::class); @@ -91,7 +91,7 @@ public function testIt_throw_exception_if_token_used_single_time(): void $this->repository()->get('unique', 'init_password'); } - public function testIt_throw_exception_if_token_used_multiple_times(): void + public function test_it_throw_exception_if_token_used_multiple_times(): void { $this->expectException(TokenConsumedException::class); @@ -107,7 +107,7 @@ public function testIt_throw_exception_if_token_used_multiple_times(): void $this->repository()->get('unique', 'init_password'); } - public function testIt_get_valid_token(): void + public function test_it_get_valid_token(): void { $token = new Token('string', 'jdoe', 'unique', 'init_password', '+1 day', '+1 month', 1, []); @@ -121,7 +121,7 @@ public function testIt_get_valid_token(): void self::assertSame($token, $got); } - public function testIt_create_token(): void + public function test_it_create_token(): void { $token = new Token('string', 'jdoe', 'unique', 'init_password', '+1 day', '+1 month', 1, []); @@ -140,7 +140,7 @@ public function testIt_create_token(): void $this->repository()->create($token); } - public function testIt_update_token(): void + public function test_it_update_token(): void { $token = new Token('string', 'jdoe', 'unique', 'init_password', '+1 day', '+1 month', 1, []);