Skip to content

Commit

Permalink
Merge pull request #110 from coopTilleuls/fix/109
Browse files Browse the repository at this point in the history
Revert "chore: remove Symfony < 4.4.* BC code (#106)"
  • Loading branch information
vincentchalamon committed Oct 6, 2022
2 parents 09e6974 + 70ba108 commit d18a1a6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
Expand Up @@ -15,6 +15,7 @@

use App\Entity\User;
use CoopTilleuls\ForgotPasswordBundle\Event\CreateTokenEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\ForgotPasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\UpdatePasswordEvent;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -56,6 +57,9 @@ public static function getSubscribedEvents()
return [
CreateTokenEvent::class => 'onCreateToken',
UpdatePasswordEvent::class => 'onUpdatePassword',
// Symfony 4.3 and inferior
ForgotPasswordEvent::CREATE_TOKEN => 'onCreateToken',
ForgotPasswordEvent::UPDATE_PASSWORD => 'onUpdatePassword',
];
}

Expand Down
6 changes: 4 additions & 2 deletions src/Event/ForgotPasswordEvent.php
Expand Up @@ -14,15 +14,17 @@
namespace CoopTilleuls\ForgotPasswordBundle\Event;

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @author Vincent CHALAMON <vincent@les-tilleuls.coop>
*
* @deprecated Use CreateTokenEvent and UpdatePasswordEvent instead
*/
class ForgotPasswordEvent extends Event
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;

Expand Down
31 changes: 31 additions & 0 deletions src/Event/PolyfillEvent.php
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent CHALAMON <vincent@les-tilleuls.coop>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace CoopTilleuls\ForgotPasswordBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

if (is_subclass_of(EventDispatcher::class, EventDispatcherInterface::class)) {
// Symfony 4.4 and upper
abstract class PolyfillEvent extends ContractsEvent
{
}
} else {
// Symfony 4.3 and inferior
abstract class PolyfillEvent extends Event
{
}
}
16 changes: 13 additions & 3 deletions src/Manager/ForgotPasswordManager.php
Expand Up @@ -15,9 +15,11 @@

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Event\CreateTokenEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\ForgotPasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\UpdatePasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Manager\Bridge\ManagerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

/**
* @author Vincent CHALAMON <vincent@les-tilleuls.coop>
Expand Down Expand Up @@ -63,7 +65,11 @@ public function resetPassword($propertyName, $value): void
}

// Generate password token
$this->dispatcher->dispatch(new CreateTokenEvent($token));
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch(new CreateTokenEvent($token));
} else {
$this->dispatcher->dispatch(ForgotPasswordEvent::CREATE_TOKEN, new CreateTokenEvent($token));
}
}

/**
Expand All @@ -74,7 +80,11 @@ public function resetPassword($propertyName, $value): void
public function updatePassword(AbstractPasswordToken $passwordToken, $password)
{
// Update user password
$this->dispatcher->dispatch(new UpdatePasswordEvent($passwordToken, $password));
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch(new UpdatePasswordEvent($passwordToken, $password));
} else {
$this->dispatcher->dispatch(ForgotPasswordEvent::UPDATE_PASSWORD, new UpdatePasswordEvent($passwordToken, $password));
}

// Remove PasswordToken
$this->manager->remove($passwordToken);
Expand Down
52 changes: 39 additions & 13 deletions tests/Manager/ForgotPasswordManagerTest.php
Expand Up @@ -15,15 +15,17 @@

use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Event\CreateTokenEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\ForgotPasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\UpdatePasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Manager\Bridge\ManagerInterface;
use CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager;
use CoopTilleuls\ForgotPasswordBundle\Manager\PasswordTokenManager;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

/**
* @author Vincent CHALAMON <vincent@les-tilleuls.coop>
Expand Down Expand Up @@ -75,9 +77,15 @@ public function testResetPasswordWithNoPreviousToken(): void
$this->managerMock->findOneBy('App\Entity\User', ['email' => 'foo@example.com'])->willReturn($this->userMock->reveal())->shouldBeCalledOnce();
$this->passwordManagerMock->findOneByUser($this->userMock->reveal())->willReturn(null)->shouldBeCalledOnce();
$this->passwordManagerMock->createPasswordToken($this->userMock->reveal())->willReturn($tokenMock->reveal())->shouldBeCalledOnce();
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
if ($this->eventDispatcherMock->reveal() instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
} else {
$this->eventDispatcherMock->dispatch(ForgotPasswordEvent::CREATE_TOKEN, Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
}

$this->manager->resetPassword('email', 'foo@example.com');
}
Expand All @@ -90,9 +98,15 @@ public function testResetPasswordWithExpiredPreviousToken(): void
$this->passwordManagerMock->findOneByUser($this->userMock->reveal())->willReturn($this->tokenMock->reveal())->shouldBeCalledOnce();
$this->tokenMock->isExpired()->willReturn(true)->shouldBeCalledOnce();
$this->passwordManagerMock->createPasswordToken($this->userMock->reveal())->willReturn($tokenMock->reveal())->shouldBeCalledOnce();
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
if ($this->eventDispatcherMock->reveal() instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
} else {
$this->eventDispatcherMock->dispatch(ForgotPasswordEvent::CREATE_TOKEN, Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();
}

$this->manager->resetPassword('email', 'foo@example.com');
}
Expand All @@ -114,9 +128,15 @@ public function testResetPasswordWithUnexpiredTokenHttp(): void
$this->managerMock->findOneBy('App\Entity\User', ['email' => 'foo@example.com'])->willReturn($this->userMock->reveal())->shouldBeCalledOnce();
$this->passwordManagerMock->findOneByUser($this->userMock->reveal())->willReturn($token)->shouldBeCalledOnce();

$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($token) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
if ($this->eventDispatcherMock->reveal() instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($token) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
} else {
$this->eventDispatcherMock->dispatch(ForgotPasswordEvent::CREATE_TOKEN, Argument::that(function ($event) use ($token) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
}

$this->manager->resetPassword('email', 'foo@example.com');
}
Expand All @@ -125,9 +145,15 @@ public function testUpdatePassword(): void
{
$token = $this->tokenMock->reveal();

$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($token) {
return $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
if ($this->eventDispatcherMock->reveal() instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($token) {
return $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
} else {
$this->eventDispatcherMock->dispatch(ForgotPasswordEvent::UPDATE_PASSWORD, Argument::that(function ($event) use ($token) {
return $event instanceof UpdatePasswordEvent && 'bar' === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();
}
$this->managerMock->remove($token)->shouldBeCalledOnce();

$this->manager->updatePassword($token, 'bar');
Expand Down

0 comments on commit d18a1a6

Please sign in to comment.