Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Mar 31, 2021
1 parent c236d95 commit c5a3aae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 76 deletions.
24 changes: 0 additions & 24 deletions src/Action/SendEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/config/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
61 changes: 12 additions & 49 deletions tests/Action/SendEmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -35,16 +32,6 @@ final class SendEmailActionTest extends TestCase
*/
protected $urlGenerator;

/**
* @var MockObject|Pool
*/
protected $pool;

/**
* @var MockObject|TemplateRegistryInterface
*/
protected $templateRegistry;

/**
* @var MockObject|UserManagerInterface
*/
Expand All @@ -70,68 +57,47 @@ 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);
}

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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c5a3aae

Please sign in to comment.