Skip to content

Commit

Permalink
chore: remove Symfony < 4.4.* BC code (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored Aug 2, 2022
1 parent 5d1119e commit 56e9ea5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

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 @@ -57,9 +56,6 @@ 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: 2 additions & 4 deletions src/Event/ForgotPasswordEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
namespace CoopTilleuls\ForgotPasswordBundle\Event;

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

/**
* @author Vincent CHALAMON <[email protected]>
*
* @deprecated Use CreateTokenEvent and UpdatePasswordEvent instead
*/
class ForgotPasswordEvent extends PolyfillEvent
class ForgotPasswordEvent extends Event
{
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: 0 additions & 31 deletions src/Event/PolyfillEvent.php

This file was deleted.

16 changes: 3 additions & 13 deletions src/Manager/ForgotPasswordManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

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\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Vincent CHALAMON <[email protected]>
Expand Down Expand Up @@ -65,11 +63,7 @@ public function resetPassword($propertyName, $value): void
}

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

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

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

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 as ContractsEventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Vincent CHALAMON <[email protected]>
Expand Down Expand Up @@ -77,15 +75,9 @@ public function testResetPasswordWithNoPreviousToken(): void
$this->managerMock->findOneBy('App\Entity\User', ['email' => '[email protected]'])->willReturn($this->userMock->reveal())->shouldBeCalledOnce();
$this->passwordManagerMock->findOneByUser($this->userMock->reveal())->willReturn(null)->shouldBeCalledOnce();
$this->passwordManagerMock->createPasswordToken($this->userMock->reveal())->willReturn($tokenMock->reveal())->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->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();

$this->manager->resetPassword('email', '[email protected]');
}
Expand All @@ -98,15 +90,9 @@ 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();
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->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($tokenMock) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $tokenMock->reveal() === $event->getPasswordToken();
}))->shouldBeCalledOnce();

$this->manager->resetPassword('email', '[email protected]');
}
Expand All @@ -128,15 +114,9 @@ public function testResetPasswordWithUnexpiredTokenHttp(): void
$this->managerMock->findOneBy('App\Entity\User', ['email' => '[email protected]'])->willReturn($this->userMock->reveal())->shouldBeCalledOnce();
$this->passwordManagerMock->findOneByUser($this->userMock->reveal())->willReturn($token)->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->eventDispatcherMock->dispatch(Argument::that(function ($event) use ($token) {
return $event instanceof CreateTokenEvent && null === $event->getPassword() && $token === $event->getPasswordToken();
}))->shouldBeCalledOnce();

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

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->eventDispatcherMock->dispatch(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 56e9ea5

Please sign in to comment.