diff --git a/features/app/src/EventListener/ForgotPasswordEventListener.php b/features/app/src/EventListener/ForgotPasswordEventListener.php index 7f61b36..d21cca2 100644 --- a/features/app/src/EventListener/ForgotPasswordEventListener.php +++ b/features/app/src/EventListener/ForgotPasswordEventListener.php @@ -30,25 +30,13 @@ */ final class ForgotPasswordEventListener implements EventSubscriberInterface { - /** - * @var MailerInterface - */ - private $mailer; - - /** - * @var EngineInterface|Environment - */ - private $twig; - /** * @var EntityManagerInterface */ private $entityManager; - public function __construct(MailerInterface $mailer, $twig, Registry $doctrine) + public function __construct(private readonly MailerInterface $mailer, private readonly EngineInterface|Environment $twig, Registry $doctrine) { - $this->mailer = $mailer; - $this->twig = $twig; $this->entityManager = $doctrine->getManager(); } diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 03fe84c..6f376e0 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -35,21 +35,6 @@ */ final class FeatureContext implements Context { - /** - * @var Registry - */ - private $doctrine; - - /** - * @var Client|KernelBrowser - */ - private $client; - - /** - * @var PasswordTokenManager - */ - private $passwordTokenManager; - /** * @var Application */ @@ -60,19 +45,10 @@ final class FeatureContext implements Context */ private $output; - /** - * @var ProviderChainInterface - */ - private $providerChain; - - public function __construct($client, Registry $doctrine, PasswordTokenManager $passwordTokenManager, ProviderChainInterface $providerChain, KernelInterface $kernel) + public function __construct(private readonly Client|KernelBrowser $client, private readonly Registry $doctrine, private readonly PasswordTokenManager $passwordTokenManager, private readonly ProviderChainInterface $providerChain, KernelInterface $kernel) { - $this->client = $client; - $this->doctrine = $doctrine; - $this->passwordTokenManager = $passwordTokenManager; $this->application = new Application($kernel); $this->output = new BufferedOutput(); - $this->providerChain = $providerChain; } /** @@ -84,7 +60,7 @@ public function resetDatabase(): void $purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); try { $purger->purge(); - } catch (Exception $e) { + } catch (Exception) { $schemaTool = new SchemaTool($this->doctrine->getManager()); $schemaTool->createSchema($this->doctrine->getManager()->getMetadataFactory()->getAllMetadata()); } @@ -408,7 +384,7 @@ public function iShouldGetAnOpenApiDocumentationUpdated(): void { $output = $this->output->fetch(); Assert::assertJson($output); - $openApi = json_decode($output, true); + $openApi = json_decode((string) $output, true); Assert::assertEquals($this->getOpenApiPaths(), $openApi['paths']); Assert::assertEquals([ 'schemas' => [ diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..2d7b4e2 --- /dev/null +++ b/rector.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use Rector\Config\RectorConfig; +use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; + +return RectorConfig::configure() + ->withPaths([ + __DIR__.'/features', + __DIR__.'/src', + __DIR__.'/tests', + ]) + // uncomment to reach your current PHP version + // ->withPhpSets() + ->withRules([ + AddVoidReturnTypeWhereNoReturnRector::class, + ]) + ->withPhpSets(php81: true); diff --git a/src/Bridge/ApiPlatform/OpenApi/AbstractOpenApiFactory.php b/src/Bridge/ApiPlatform/OpenApi/AbstractOpenApiFactory.php index 71dc0a8..15d130f 100644 --- a/src/Bridge/ApiPlatform/OpenApi/AbstractOpenApiFactory.php +++ b/src/Bridge/ApiPlatform/OpenApi/AbstractOpenApiFactory.php @@ -29,18 +29,8 @@ */ abstract class AbstractOpenApiFactory { - protected $decorated; - protected $router; - protected $providerChain; - - /** - * @param LegacyOpenApiFactoryInterface|OpenApiFactoryInterface $decorated - */ - public function __construct($decorated, RouterInterface $router, ProviderChainInterface $providerChain) + public function __construct(protected readonly LegacyOpenApiFactoryInterface|OpenApiFactoryInterface $decorated, protected readonly RouterInterface $router, protected readonly ProviderChainInterface $providerChain) { - $this->providerChain = $providerChain; - $this->decorated = $decorated; - $this->router = $router; } public function __invoke(array $context = []) diff --git a/src/Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php b/src/Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php index 5315410..a0268ef 100644 --- a/src/Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php +++ b/src/Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php @@ -23,15 +23,8 @@ */ final class DocumentationNormalizer implements NormalizerInterface { - private $decorated; - private $router; - private $providerChain; - - public function __construct(NormalizerInterface $decorated, RouterInterface $router, ProviderChainInterface $providerChain) + public function __construct(private readonly NormalizerInterface $decorated, private readonly RouterInterface $router, private readonly ProviderChainInterface $providerChain) { - $this->decorated = $decorated; - $this->router = $router; - $this->providerChain = $providerChain; } public function normalize($object, $format = null, array $context = []): array diff --git a/src/Controller/ForgotPasswordController.php b/src/Controller/ForgotPasswordController.php index 4bd2448..cd2d156 100644 --- a/src/Controller/ForgotPasswordController.php +++ b/src/Controller/ForgotPasswordController.php @@ -25,15 +25,8 @@ */ final class ForgotPasswordController { - private $getToken; - private $updatePassword; - private $resetPassword; - - public function __construct(GetToken $getToken, UpdatePassword $updatePassword, ResetPassword $resetPassword) + public function __construct(private readonly GetToken $getToken, private readonly UpdatePassword $updatePassword, private readonly ResetPassword $resetPassword) { - $this->getToken = $getToken; - $this->updatePassword = $updatePassword; - $this->resetPassword = $resetPassword; } /** diff --git a/src/Controller/GetToken.php b/src/Controller/GetToken.php index ae8d351..b5abb2b 100644 --- a/src/Controller/GetToken.php +++ b/src/Controller/GetToken.php @@ -23,11 +23,8 @@ */ final class GetToken { - private $normalizer; - - public function __construct(NormalizerInterface $normalizer) + public function __construct(private readonly NormalizerInterface $normalizer) { - $this->normalizer = $normalizer; } /** diff --git a/src/Controller/ResetPassword.php b/src/Controller/ResetPassword.php index b0356fd..7c9c461 100644 --- a/src/Controller/ResetPassword.php +++ b/src/Controller/ResetPassword.php @@ -22,11 +22,8 @@ */ final class ResetPassword { - private $forgotPasswordManager; - - public function __construct(ForgotPasswordManager $forgotPasswordManager) + public function __construct(private readonly ForgotPasswordManager $forgotPasswordManager) { - $this->forgotPasswordManager = $forgotPasswordManager; } /** diff --git a/src/Controller/UpdatePassword.php b/src/Controller/UpdatePassword.php index 000f781..6d71245 100644 --- a/src/Controller/UpdatePassword.php +++ b/src/Controller/UpdatePassword.php @@ -23,11 +23,8 @@ */ final class UpdatePassword { - private $forgotPasswordManager; - - public function __construct(ForgotPasswordManager $forgotPasswordManager) + public function __construct(private readonly ForgotPasswordManager $forgotPasswordManager) { - $this->forgotPasswordManager = $forgotPasswordManager; } /** diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 7d218e1..c67e5cf 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -33,9 +33,7 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->beforeNormalization() - ->ifTrue(function ($config) { - return \array_key_exists('password_token_class', $config) || \array_key_exists('user_class', $config); - }) + ->ifTrue(fn ($config) => \array_key_exists('password_token_class', $config) || \array_key_exists('user_class', $config)) ->then(function ($config) { if (\array_key_exists('password_token_class', $config)) { if (!isset($config['password_token'])) { @@ -54,9 +52,7 @@ public function getConfigTreeBuilder(): TreeBuilder return $config; }) - ->ifTrue(function ($config) { - return !\array_key_exists('providers', $config); - }) + ->ifTrue(fn ($config) => !\array_key_exists('providers', $config)) ->then(function ($config) { $config['providers']['default']['default'] = true; $config['providers']['default']['password_token'] = $config['password_token']; diff --git a/src/Event/ForgotPasswordEvent.php b/src/Event/ForgotPasswordEvent.php index e44bf0d..c3e182a 100644 --- a/src/Event/ForgotPasswordEvent.php +++ b/src/Event/ForgotPasswordEvent.php @@ -25,16 +25,8 @@ class ForgotPasswordEvent extends PolyfillEvent public const CREATE_TOKEN = 'coop_tilleuls_forgot_password.create_token'; public const UPDATE_PASSWORD = 'coop_tilleuls_forgot_password.update_password'; - protected $passwordToken; - protected $password; - - /** - * @param string $password - */ - public function __construct(AbstractPasswordToken $passwordToken, $password = null) + public function __construct(protected readonly AbstractPasswordToken $passwordToken, protected readonly ?string $password = null) { - $this->passwordToken = $passwordToken; - $this->password = $password; } /** diff --git a/src/Event/UserNotFoundEvent.php b/src/Event/UserNotFoundEvent.php index d329f56..a24c8e0 100644 --- a/src/Event/UserNotFoundEvent.php +++ b/src/Event/UserNotFoundEvent.php @@ -17,11 +17,8 @@ final class UserNotFoundEvent extends PolyfillEvent { public const USER_NOT_FOUND = 'coop_tilleuls_forgot_password.user_not_found'; - private array $context; - - public function __construct(array $context = []) + public function __construct(private readonly array $context = []) { - $this->context = $context; } /** diff --git a/src/EventListener/RequestEventListener.php b/src/EventListener/RequestEventListener.php index 9e25197..104a554 100755 --- a/src/EventListener/RequestEventListener.php +++ b/src/EventListener/RequestEventListener.php @@ -29,15 +29,8 @@ final class RequestEventListener { use MainRequestTrait; - private $passwordTokenManager; - private ProviderChainInterface $providerChain; - - public function __construct( - PasswordTokenManager $passwordTokenManager, - ProviderChainInterface $providerChain - ) { - $this->passwordTokenManager = $passwordTokenManager; - $this->providerChain = $providerChain; + public function __construct(private readonly PasswordTokenManager $passwordTokenManager, private readonly ProviderChainInterface $providerChain) + { } public function decodeRequest(KernelEvent $event): void diff --git a/src/Manager/Bridge/DoctrineManager.php b/src/Manager/Bridge/DoctrineManager.php index db419ec..db7e63c 100644 --- a/src/Manager/Bridge/DoctrineManager.php +++ b/src/Manager/Bridge/DoctrineManager.php @@ -13,19 +13,16 @@ namespace CoopTilleuls\ForgotPasswordBundle\Manager\Bridge; +use Doctrine\Common\Persistence\ManagerRegistry as LegacyManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; + /** * @author Vincent CHALAMON */ final class DoctrineManager implements ManagerInterface { - private $registry; - - /** - * @var \Doctrine\Common\Persistence\ManagerRegistry|\Doctrine\Persistence\ManagerRegistry - */ - public function __construct($registry) + public function __construct(private readonly LegacyManagerRegistry|ManagerRegistry $registry) { - $this->registry = $registry; } public function findOneBy($class, array $criteria) diff --git a/src/Manager/ForgotPasswordManager.php b/src/Manager/ForgotPasswordManager.php index f5dd60e..96437db 100644 --- a/src/Manager/ForgotPasswordManager.php +++ b/src/Manager/ForgotPasswordManager.php @@ -29,18 +29,8 @@ */ class ForgotPasswordManager { - private $passwordTokenManager; - private $dispatcher; - private $providerChain; - - public function __construct( - PasswordTokenManager $passwordTokenManager, - EventDispatcherInterface $dispatcher, - ProviderChainInterface $providerChain - ) { - $this->passwordTokenManager = $passwordTokenManager; - $this->dispatcher = $dispatcher; - $this->providerChain = $providerChain; + public function __construct(private readonly PasswordTokenManager $passwordTokenManager, private readonly EventDispatcherInterface $dispatcher, private readonly ProviderChainInterface $providerChain) + { } public function resetPassword($propertyName, $value, ?ProviderInterface $provider = null): void diff --git a/src/Manager/PasswordTokenManager.php b/src/Manager/PasswordTokenManager.php index 8e23293..c10a14a 100644 --- a/src/Manager/PasswordTokenManager.php +++ b/src/Manager/PasswordTokenManager.php @@ -25,11 +25,8 @@ */ class PasswordTokenManager { - private $providerChain; - - public function __construct(ProviderChainInterface $providerChain) + public function __construct(private readonly ProviderChainInterface $providerChain) { - $this->providerChain = $providerChain; } /** diff --git a/src/Normalizer/JMSNormalizer.php b/src/Normalizer/JMSNormalizer.php index 60b29c0..d2d0b48 100644 --- a/src/Normalizer/JMSNormalizer.php +++ b/src/Normalizer/JMSNormalizer.php @@ -21,14 +21,8 @@ */ final class JMSNormalizer implements NormalizerInterface { - /** - * @var ArrayTransformerInterface - */ - private $normalizer; - - public function __construct(ArrayTransformerInterface $normalizer) + public function __construct(private readonly ArrayTransformerInterface $normalizer) { - $this->normalizer = $normalizer; } /** diff --git a/src/Normalizer/SymfonyNormalizer.php b/src/Normalizer/SymfonyNormalizer.php index 1ad2d79..ea90ad6 100644 --- a/src/Normalizer/SymfonyNormalizer.php +++ b/src/Normalizer/SymfonyNormalizer.php @@ -21,14 +21,8 @@ */ final class SymfonyNormalizer implements NormalizerInterface { - /** - * @var SymfonyNormalizerInterface - */ - private $normalizer; - - public function __construct(SymfonyNormalizerInterface $normalizer) + public function __construct(private readonly SymfonyNormalizerInterface $normalizer) { - $this->normalizer = $normalizer; } /** diff --git a/src/Provider/Provider.php b/src/Provider/Provider.php index e16b343..a95e7bb 100644 --- a/src/Provider/Provider.php +++ b/src/Provider/Provider.php @@ -17,42 +17,8 @@ final class Provider implements ProviderInterface { - private ManagerInterface $manager; - private string $passwordTokenClass; - private string $passwordTokenExpiredIn; - private string $passwordTokenUserField; - private array $passwordTokenSerializationGroups; - private string $userClass; - private string $userEmailField; - private string $userPasswordField; - private array $userAuthorizedFields; - private bool $isDefault; - private string $name; - - public function __construct( - ManagerInterface $manager, - string $name, - string $passwordTokenClass, - string $passwordTokenExpiredIn, - string $passwordTokenUserField, - string $userClass, - array $passwordTokenSerializationGroups = [], - string $userEmailField = 'email', - string $userPasswordField = 'password', - array $userAuthorizedFields = [], - bool $isDefault = false - ) { - $this->manager = $manager; - $this->name = $name; - $this->passwordTokenClass = $passwordTokenClass; - $this->passwordTokenExpiredIn = $passwordTokenExpiredIn; - $this->passwordTokenUserField = $passwordTokenUserField; - $this->passwordTokenSerializationGroups = $passwordTokenSerializationGroups; - $this->userClass = $userClass; - $this->userEmailField = $userEmailField; - $this->userPasswordField = $userPasswordField; - $this->userAuthorizedFields = $userAuthorizedFields; - $this->isDefault = $isDefault; + public function __construct(private readonly ManagerInterface $manager, private readonly string $name, private readonly string $passwordTokenClass, private readonly string $passwordTokenExpiredIn, private readonly string $passwordTokenUserField, private readonly string $userClass, private readonly array $passwordTokenSerializationGroups = [], private readonly string $userEmailField = 'email', private readonly string $userPasswordField = 'password', private readonly array $userAuthorizedFields = [], private readonly bool $isDefault = false) + { } public function getManager(): ManagerInterface diff --git a/tests/EventListener/ExceptionEventListenerTest.php b/tests/EventListener/ExceptionEventListenerTest.php index 0dba8b9..d9614ea 100755 --- a/tests/EventListener/ExceptionEventListenerTest.php +++ b/tests/EventListener/ExceptionEventListenerTest.php @@ -85,14 +85,12 @@ public function testOnKernelException(): void } else { $eventMock->expects($this->once())->method('isMasterRequest')->willReturn(true); } - $eventMock->expects($this->once())->method('setResponse')->with($this->callback(function ($response) { - return $response instanceof JsonResponse - && json_encode( - ['message' => 'Parameter "foo" is missing.'], - 15 - ) === $response->getContent() - && 400 === $response->getStatusCode(); - })); + $eventMock->expects($this->once())->method('setResponse')->with($this->callback(fn ($response) => $response instanceof JsonResponse + && json_encode( + ['message' => 'Parameter "foo" is missing.'], + 15 + ) === $response->getContent() + && 400 === $response->getStatusCode())); $listener = new ExceptionEventListener(); $listener->onKernelException($eventMock); diff --git a/tests/Manager/Bridge/DoctrineManagerTest.php b/tests/Manager/Bridge/DoctrineManagerTest.php index 10c5863..ef9aea7 100755 --- a/tests/Manager/Bridge/DoctrineManagerTest.php +++ b/tests/Manager/Bridge/DoctrineManagerTest.php @@ -54,7 +54,7 @@ public function testFindOneBy(): void public function testPersist(): void { - $this->registryMock->expects($this->once())->method('getManagerForClass')->with(\get_class($this->objectMock))->willReturn($this->managerMock); + $this->registryMock->expects($this->once())->method('getManagerForClass')->with($this->objectMock::class)->willReturn($this->managerMock); $this->managerMock->expects($this->once())->method('persist')->with($this->objectMock); $this->managerMock->expects($this->once())->method('flush'); @@ -63,7 +63,7 @@ public function testPersist(): void public function testRemove(): void { - $this->registryMock->expects($this->once())->method('getManagerForClass')->with(\get_class($this->objectMock))->willReturn($this->managerMock); + $this->registryMock->expects($this->once())->method('getManagerForClass')->with($this->objectMock::class)->willReturn($this->managerMock); $this->managerMock->expects($this->once())->method('remove')->with($this->objectMock); $this->managerMock->expects($this->once())->method('flush'); diff --git a/tests/Manager/ForgotPasswordManagerTest.php b/tests/Manager/ForgotPasswordManagerTest.php index ce90536..a162a00 100755 --- a/tests/Manager/ForgotPasswordManagerTest.php +++ b/tests/Manager/ForgotPasswordManagerTest.php @@ -72,13 +72,9 @@ public function testResetPasswordNotUser(): void $this->providerMock->expects($this->once())->method('getUserClass')->willReturn(User::class); $this->managerMock->expects($this->once())->method('findOneBy')->with(User::class, ['email' => 'foo@example.com']); if ($this->eventDispatcherMock instanceof ContractsEventDispatcherInterface) { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(function ($event) { - return $event instanceof UserNotFoundEvent && ['email' => 'foo@example.com'] === $event->getContext(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(fn ($event) => $event instanceof UserNotFoundEvent && ['email' => 'foo@example.com'] === $event->getContext())); } else { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(UserNotFoundEvent::USER_NOT_FOUND, $this->callback(function ($event) { - return $event instanceof UserNotFoundEvent && ['email' => 'foo@example.com'] === $event->getContext(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(UserNotFoundEvent::USER_NOT_FOUND, $this->callback(fn ($event) => $event instanceof UserNotFoundEvent && ['email' => 'foo@example.com'] === $event->getContext())); } $this->passwordManagerMock->expects($this->never())->method('findOneByUser')->with(self::any(), $this->providerMock); @@ -97,13 +93,9 @@ public function testResetPasswordWithNoPreviousToken(): void $this->passwordManagerMock->expects($this->once())->method('createPasswordToken')->with($this->userMock, $this->isInstanceOf(\DateTimeInterface::class), $this->providerMock)->willReturn($this->tokenMock); if ($this->eventDispatcherMock instanceof ContractsEventDispatcherInterface) { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } else { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } $this->manager->resetPassword('email', 'foo@example.com'); @@ -121,13 +113,9 @@ public function testResetPasswordWithExpiredPreviousToken(): void $this->passwordManagerMock->expects($this->once())->method('createPasswordToken')->with($this->userMock, $this->isInstanceOf(\DateTimeInterface::class), $this->providerMock)->willReturn($this->tokenMock); if ($this->eventDispatcherMock instanceof ContractsEventDispatcherInterface) { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } else { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } $this->manager->resetPassword('email', 'foo@example.com'); @@ -147,13 +135,9 @@ public function testResetPasswordWithUnexpiredTokenHttp(): void $this->passwordManagerMock->expects($this->once())->method('findOneByUser')->with($this->userMock, $this->providerMock)->willReturn($this->tokenMock); if ($this->eventDispatcherMock instanceof ContractsEventDispatcherInterface) { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } else { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(function ($event) { - return $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::CREATE_TOKEN, $this->callback(fn ($event) => $event instanceof CreateTokenEvent && null === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } $this->manager->resetPassword('email', 'foo@example.com'); @@ -165,13 +149,9 @@ public function testUpdatePassword(): void $this->providerMock->expects($this->once())->method('getManager')->willReturn($this->managerMock); if ($this->eventDispatcherMock instanceof ContractsEventDispatcherInterface) { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(function ($event) { - return $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with($this->callback(fn ($event) => $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } else { - $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::UPDATE_PASSWORD, $this->callback(function ($event) { - return $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $this->tokenMock === $event->getPasswordToken(); - })); + $this->eventDispatcherMock->expects($this->once())->method('dispatch')->with(ForgotPasswordEvent::UPDATE_PASSWORD, $this->callback(fn ($event) => $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $this->tokenMock === $event->getPasswordToken())); } $this->managerMock->expects($this->once())->method('remove')->with($this->tokenMock); diff --git a/tests/Manager/PasswordTokenManagerTest.php b/tests/Manager/PasswordTokenManagerTest.php index 4196e5b..afa3386 100755 --- a/tests/Manager/PasswordTokenManagerTest.php +++ b/tests/Manager/PasswordTokenManagerTest.php @@ -49,12 +49,10 @@ protected function setUp(): void public function testCreatePasswordToken(): void { - $this->managerMock->expects($this->once())->method('persist')->with($this->callback(function ($object) { - return $object instanceof AbstractPasswordToken - && '2016-10-11 10:00:00' === $object->getExpiresAt()->format('Y-m-d H:i:s') - && preg_match('/^[A-z\d]{50}$/', $object->getToken()) - && $this->userMock === $object->getUser(); - })); + $this->managerMock->expects($this->once())->method('persist')->with($this->callback(fn ($object) => $object instanceof AbstractPasswordToken + && '2016-10-11 10:00:00' === $object->getExpiresAt()->format('Y-m-d H:i:s') + && preg_match('/^[A-z\d]{50}$/', $object->getToken()) + && $this->userMock === $object->getUser())); $this->providerChainMock->expects($this->once())->method('get')->willReturn($this->providerMock); $this->providerMock->expects($this->once())->method('getPasswordTokenClass')->willReturn(PasswordToken::class); @@ -65,11 +63,9 @@ public function testCreatePasswordToken(): void public function testCreatePasswordTokenWithoutExpirationDate(): void { - $this->managerMock->expects($this->once())->method('persist')->with($this->callback(function ($object) { - return $object instanceof AbstractPasswordToken - && preg_match('/^[A-z\d]{50}$/', $object->getToken()) - && $this->userMock === $object->getUser(); - })); + $this->managerMock->expects($this->once())->method('persist')->with($this->callback(fn ($object) => $object instanceof AbstractPasswordToken + && preg_match('/^[A-z\d]{50}$/', $object->getToken()) + && $this->userMock === $object->getUser())); $this->providerChainMock->expects($this->once())->method('get')->willReturn($this->providerMock); $this->providerMock->expects($this->once())->method('getPasswordTokenClass')->willReturn(PasswordToken::class);