-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from coopTilleuls/fix/109
Revert "chore: remove Symfony < 4.4.* BC code (#106)"
- Loading branch information
Showing
5 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,15 +14,17 @@ | |
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 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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the CoopTilleulsForgotPasswordBundle package. | ||
* | ||
* (c) Vincent CHALAMON <[email protected]> | ||
* | ||
* 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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
@@ -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)); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
@@ -75,9 +77,15 @@ 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(); | ||
$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', '[email protected]'); | ||
} | ||
|
@@ -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', '[email protected]'); | ||
} | ||
|
@@ -114,9 +128,15 @@ 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(); | ||
|
||
$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', '[email protected]'); | ||
} | ||
|
@@ -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'); | ||
|