diff --git a/src/Action/SendEmailAction.php b/src/Action/SendEmailAction.php index 2dc71316..ee00842b 100644 --- a/src/Action/SendEmailAction.php +++ b/src/Action/SendEmailAction.php @@ -17,36 +17,18 @@ use Nucleos\UserBundle\Mailer\MailerInterface; use Nucleos\UserBundle\Model\UserManagerInterface; use Nucleos\UserBundle\Util\TokenGeneratorInterface; -use Sonata\AdminBundle\Admin\Pool; -use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Twig\Environment; final class SendEmailAction { - /** - * @var Environment - */ - private $twig; - /** * @var UrlGeneratorInterface */ private $urlGenerator; - /** - * @var Pool - */ - private $adminPool; - - /** - * @var TemplateRegistryInterface - */ - private $templateRegistry; - /** * @var UserManagerInterface */ @@ -68,19 +50,13 @@ final class SendEmailAction private $resetTtl; public function __construct( - Environment $twig, UrlGeneratorInterface $urlGenerator, - Pool $adminPool, - TemplateRegistryInterface $templateRegistry, UserManagerInterface $userManager, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, int $resetTtl ) { - $this->twig = $twig; $this->urlGenerator = $urlGenerator; - $this->adminPool = $adminPool; - $this->templateRegistry = $templateRegistry; $this->userManager = $userManager; $this->mailer = $mailer; $this->tokenGenerator = $tokenGenerator; diff --git a/src/Resources/config/action.php b/src/Resources/config/action.php index b2c7050f..e9d297a1 100644 --- a/src/Resources/config/action.php +++ b/src/Resources/config/action.php @@ -35,10 +35,7 @@ ->set(SendEmailAction::class) ->public() ->args([ - ref('twig'), ref('router'), - ref('sonata.admin.pool'), - ref('sonata.admin.global_template_registry'), ref('nucleos_user.user_manager'), ref('nucleos_user.mailer'), ref('nucleos_user.util.token_generator'), diff --git a/tests/Action/SendEmailActionTest.php b/tests/Action/SendEmailActionTest.php index 32cb2bd9..1cea9f58 100644 --- a/tests/Action/SendEmailActionTest.php +++ b/tests/Action/SendEmailActionTest.php @@ -20,13 +20,10 @@ use Nucleos\UserBundle\Util\TokenGeneratorInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Sonata\AdminBundle\Admin\Pool; -use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Twig\Environment; final class SendEmailActionTest extends TestCase { @@ -35,16 +32,6 @@ final class SendEmailActionTest extends TestCase */ protected $urlGenerator; - /** - * @var MockObject|Pool - */ - protected $pool; - - /** - * @var MockObject|TemplateRegistryInterface - */ - protected $templateRegistry; - /** * @var MockObject|UserManagerInterface */ @@ -70,33 +57,19 @@ final class SendEmailActionTest extends TestCase */ protected $fromEmail; - /** - * @var string - */ - protected $template; - /** * @var ContainerBuilder&MockObject */ protected $container; - /** - * @var Environment&MockObject - */ - protected $templating; - protected function setUp(): void { - $this->templating = $this->createMock(Environment::class); $this->urlGenerator = $this->createMock(UrlGeneratorInterface::class); - $this->pool = $this->createMock(Pool::class); - $this->templateRegistry = $this->createMock(TemplateRegistryInterface::class); $this->userManager = $this->createMock(UserManagerInterface::class); $this->mailer = $this->createMock(MailerInterface::class); $this->tokenGenerator = $this->createMock(TokenGeneratorInterface::class); $this->resetTtl = 60; $this->fromEmail = 'noreply@localhost'; - $this->template = 'email.txt.twig'; $this->container = $this->createMock(ContainerBuilder::class); } @@ -104,34 +77,27 @@ public function testUnknownUsername(): void { $request = new Request([], ['username' => 'bar']); - $parameters = [ - 'base_template' => 'base.html.twig', - 'admin_pool' => $this->pool, - 'invalid_username' => 'bar', - ]; - - $this->templating->expects(static::once()) - ->method('render') - ->with('@NucleosUserAdmin/Admin/Security/Resetting/request.html.twig', $parameters) - ->willReturn('template content') - ; - - $this->templateRegistry - ->method('getTemplate') - ->with('layout') - ->willReturn('base.html.twig') - ; - $this->userManager ->method('findUserByUsernameOrEmail') ->with('bar') ->willReturn(null) ; + $this->urlGenerator + ->method('generate') + ->withConsecutive( + ['nucleos_user_admin_resetting_check_email', ['username' => 'bar']] + ) + ->willReturnOnConsecutiveCalls( + '/check-email' + ) + ; + $action = $this->getAction(); $result = $action($request); - static::assertSame('template content', $result->getContent()); + static::assertInstanceOf(RedirectResponse::class, $result); + static::assertSame('/check-email', $result->getTargetUrl()); } public function testPasswordRequestNonExpired(): void @@ -278,10 +244,7 @@ static function () use (&$storedToken): ?string { private function getAction(): SendEmailAction { return new SendEmailAction( - $this->templating, $this->urlGenerator, - $this->pool, - $this->templateRegistry, $this->userManager, $this->mailer, $this->tokenGenerator,