Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 26, 2024
1 parent ef6fbe3 commit 196a602
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 52 deletions.
35 changes: 13 additions & 22 deletions src/Action/CheckEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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__,
Expand All @@ -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',
Expand All @@ -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',
Expand Down
31 changes: 2 additions & 29 deletions tests/Action/CheckEmailActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,11 +28,6 @@ final class CheckEmailActionTest extends TestCase
*/
private MockObject $templating;

/**
* @var MockObject&UrlGeneratorInterface
*/
private MockObject $urlGenerator;

private Pool $pool;

/**
Expand All @@ -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,
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Action/RequestActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 196a602

Please sign in to comment.