From 196a602808b14ed22141f0746f9e74f785a0be51 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 26 Aug 2024 14:41:32 +0200 Subject: [PATCH] Fix --- src/Action/CheckEmailAction.php | 35 ++++++++++----------------- tests/Action/CheckEmailActionTest.php | 31 ++---------------------- tests/Action/RequestActionTest.php | 2 +- 3 files changed, 16 insertions(+), 52 deletions(-) diff --git a/src/Action/CheckEmailAction.php b/src/Action/CheckEmailAction.php index 9fe82ebaf..af18af914 100644 --- a/src/Action/CheckEmailAction.php +++ b/src/Action/CheckEmailAction.php @@ -18,23 +18,15 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Twig\Environment; -use TypeError; final class CheckEmailAction { - private Pool $adminPool; private TemplateRegistryInterface $templateRegistry; private int $tokenTtl; /** - * NEXT_MAJOR: Remove `$tokenTtlDeprecated` argument and only allow first types of arguments. - * - * @param Environment $twig - * @param Pool $adminPool - * @param TemplateRegistryInterface $templateRegistry - * @param int $tokenTtl - * @param null $tokenTtlDeprecated + * NEXT_MAJOR: Remove `$tokenTtlDeprecated` argument and only allow first types of arguments. */ public function __construct( private Environment $twig, @@ -46,27 +38,26 @@ public function __construct( // NEXT_MAJOR: Remove all checks and use constructor property promotion instead if ($adminPool instanceof UrlGeneratorInterface) { if (!$templateRegistry instanceof Pool) { - throw new \TypeError(sprintf( + throw new \TypeError(\sprintf( 'Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, Pool::class, - \get_class($templateRegistry) + $templateRegistry::class )); } $this->adminPool = $templateRegistry; if (!$tokenTtl instanceof TemplateRegistryInterface) { - throw new \TypeError(sprintf( - 'Argument 4 passed to %s() must be an instance of %s, %s given.', + throw new \TypeError(\sprintf( + 'Argument 4 passed to %s() must be an instance of %s, int given.', __METHOD__, TemplateRegistryInterface::class, - \get_class($tokenTtl) )); } $this->templateRegistry = $tokenTtl; - if (!is_int($tokenTtlDeprecated)) { - throw new \TypeError(sprintf( + if (!\is_int($tokenTtlDeprecated)) { + throw new \TypeError(\sprintf( 'Argument 5 passed to %s() must be type of %s, %s given.', __METHOD__, 'integer', @@ -75,7 +66,7 @@ public function __construct( } $this->tokenTtl = $tokenTtlDeprecated; - @trigger_error(sprintf( + @trigger_error(\sprintf( 'Passing an instance of %s as argument 2 to "%s()" is deprecated since sonata-project/user-bundle 5.x and will only accept an instance of %s in version 6.0.', UrlGeneratorInterface::class, __METHOD__, @@ -84,17 +75,17 @@ public function __construct( } else { $this->adminPool = $adminPool; if (!$templateRegistry instanceof TemplateRegistryInterface) { - throw new \TypeError(sprintf( + throw new \TypeError(\sprintf( 'Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, TemplateRegistryInterface::class, - \get_class($templateRegistry) + $templateRegistry::class )); } $this->templateRegistry = $templateRegistry; - if (!is_int($tokenTtl)) { - throw new \TypeError(sprintf( + if (!\is_int($tokenTtl)) { + throw new \TypeError(\sprintf( 'Argument 4 passed to %s() must be type of %s, %s given.', __METHOD__, 'integer', @@ -104,7 +95,7 @@ public function __construct( $this->tokenTtl = $tokenTtl; if (null !== $tokenTtlDeprecated) { - throw new \TypeError(sprintf( + throw new \TypeError(\sprintf( 'Argument 5 passed to %s() must be %s, %s given.', __METHOD__, 'NULL', diff --git a/tests/Action/CheckEmailActionTest.php b/tests/Action/CheckEmailActionTest.php index 12ef4a09e..2c56169c4 100644 --- a/tests/Action/CheckEmailActionTest.php +++ b/tests/Action/CheckEmailActionTest.php @@ -19,9 +19,6 @@ use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Sonata\UserBundle\Action\CheckEmailAction; use Symfony\Component\DependencyInjection\Container; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Twig\Environment; final class CheckEmailActionTest extends TestCase @@ -31,11 +28,6 @@ final class CheckEmailActionTest extends TestCase */ private MockObject $templating; - /** - * @var MockObject&UrlGeneratorInterface - */ - private MockObject $urlGenerator; - private Pool $pool; /** @@ -48,32 +40,13 @@ final class CheckEmailActionTest extends TestCase protected function setUp(): void { $this->templating = $this->createMock(Environment::class); - $this->urlGenerator = $this->createMock(UrlGeneratorInterface::class); $this->pool = new Pool(new Container()); $this->templateRegistry = $this->createMock(TemplateRegistryInterface::class); $this->resetTtl = 60; } - public function testWithoutUsername(): void - { - $request = new Request(); - - $this->urlGenerator->expects(static::once()) - ->method('generate') - ->with('sonata_user_admin_resetting_request') - ->willReturn('/foo'); - - $action = $this->getAction(); - $result = $action($request); - - static::assertInstanceOf(RedirectResponse::class, $result); - static::assertSame('/foo', $result->getTargetUrl()); - } - public function testWithUsername(): void { - $request = new Request(['username' => 'bar']); - $parameters = [ 'base_template' => 'base.html.twig', 'admin_pool' => $this->pool, @@ -91,13 +64,13 @@ public function testWithUsername(): void ->willReturn('base.html.twig'); $action = $this->getAction(); - $result = $action($request); + $result = $action(); static::assertSame('template content', $result->getContent()); } private function getAction(): CheckEmailAction { - return new CheckEmailAction($this->templating, $this->urlGenerator, $this->pool, $this->templateRegistry, $this->resetTtl); + return new CheckEmailAction($this->templating, $this->pool, $this->templateRegistry, $this->resetTtl); } } diff --git a/tests/Action/RequestActionTest.php b/tests/Action/RequestActionTest.php index 17a30b24f..a32049a2e 100644 --- a/tests/Action/RequestActionTest.php +++ b/tests/Action/RequestActionTest.php @@ -238,7 +238,7 @@ public function testEmailSent(): void $this->urlGenerator->method('generate')->with( 'sonata_user_admin_resetting_check_email', - ['username' => 'bar'], + [], )->willReturn('/check-email'); $action = $this->getAction();