From 4ba815634269f332a78c0b38caade72d6c109ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20D=C5=BAwiga=C5=82a?= Date: Thu, 5 Sep 2024 08:04:16 +0200 Subject: [PATCH] Add tests to EmailCodeValidatedEvent --- .../TwoFactor}/Event/EmailCodeValidated.php | 2 +- .../TwoFactor}/Event/EmailTwoFactorEvents.php | 2 +- .../Provider/Email/EmailTwoFactorProvider.php | 4 ++-- .../Email/EmailTwoFactorProviderTest.php | 24 ++++++++++++++++++- 4 files changed, 27 insertions(+), 5 deletions(-) rename src/email/{ => Security/TwoFactor}/Event/EmailCodeValidated.php (71%) rename src/email/{ => Security/TwoFactor}/Event/EmailTwoFactorEvents.php (76%) diff --git a/src/email/Event/EmailCodeValidated.php b/src/email/Security/TwoFactor/Event/EmailCodeValidated.php similarity index 71% rename from src/email/Event/EmailCodeValidated.php rename to src/email/Security/TwoFactor/Event/EmailCodeValidated.php index f9af5612..52282f39 100644 --- a/src/email/Event/EmailCodeValidated.php +++ b/src/email/Security/TwoFactor/Event/EmailCodeValidated.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scheb\TwoFactorBundle\Event; +namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event; readonly class EmailCodeValidated { diff --git a/src/email/Event/EmailTwoFactorEvents.php b/src/email/Security/TwoFactor/Event/EmailTwoFactorEvents.php similarity index 76% rename from src/email/Event/EmailTwoFactorEvents.php rename to src/email/Security/TwoFactor/Event/EmailTwoFactorEvents.php index d4ec084c..fcc4ae3e 100644 --- a/src/email/Event/EmailTwoFactorEvents.php +++ b/src/email/Security/TwoFactor/Event/EmailTwoFactorEvents.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scheb\TwoFactorBundle\Event; +namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event; class EmailTwoFactorEvents { diff --git a/src/email/Security/TwoFactor/Provider/Email/EmailTwoFactorProvider.php b/src/email/Security/TwoFactor/Provider/Email/EmailTwoFactorProvider.php index d55ed9b7..0ce36b0b 100644 --- a/src/email/Security/TwoFactor/Provider/Email/EmailTwoFactorProvider.php +++ b/src/email/Security/TwoFactor/Provider/Email/EmailTwoFactorProvider.php @@ -4,10 +4,10 @@ namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email; -use Scheb\TwoFactorBundle\Event\EmailCodeValidated; -use Scheb\TwoFactorBundle\Event\EmailTwoFactorEvents; use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface; +use Scheb\TwoFactorBundle\Security\TwoFactor\Event\EmailCodeValidated; +use Scheb\TwoFactorBundle\Security\TwoFactor\Event\EmailTwoFactorEvents; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface; diff --git a/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php b/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php index e8093423..91e88516 100644 --- a/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php +++ b/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php @@ -11,8 +11,8 @@ use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface; use Scheb\TwoFactorBundle\Tests\TestCase; use stdClass; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class EmailTwoFactorProviderTest extends TestCase { @@ -59,6 +59,20 @@ private function createAuthenticationContext(UserInterface|null $user = null): M return $authContext; } + private function expectEmailValidatedEventDispatched(): void + { + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatch'); + } + + private function expectEmailValidatedEventNotDispatched(): void + { + $this->eventDispatcher + ->expects($this->never()) + ->method('dispatch'); + } + /** * @test */ @@ -132,6 +146,8 @@ public function prepareAuthentication_interfaceImplemented_codeGenerated(): void public function validateAuthenticationCode_noTwoFactorUser_returnFalse(): void { $user = new stdClass(); + + $this->expectEmailValidatedEventNotDispatched(); $returnValue = $this->provider->validateAuthenticationCode($user, 'code'); $this->assertFalse($returnValue); } @@ -142,6 +158,8 @@ public function validateAuthenticationCode_noTwoFactorUser_returnFalse(): void public function validateAuthenticationCode_validCodeGiven_returnTrue(): void { $user = $this->createUser(); + + $this->expectEmailValidatedEventDispatched(); $returnValue = $this->provider->validateAuthenticationCode($user, self::VALID_AUTH_CODE); $this->assertTrue($returnValue); } @@ -152,6 +170,8 @@ public function validateAuthenticationCode_validCodeGiven_returnTrue(): void public function validateAuthenticationCode_validCodeWithSpaces_returnTrue(): void { $user = $this->createUser(); + + $this->expectEmailValidatedEventDispatched(); $returnValue = $this->provider->validateAuthenticationCode($user, self::VALID_AUTH_CODE_WITH_SPACES); $this->assertTrue($returnValue); } @@ -162,6 +182,8 @@ public function validateAuthenticationCode_validCodeWithSpaces_returnTrue(): voi public function validateAuthenticationCode_validCodeGiven_returnFalse(): void { $user = $this->createUser(); + + $this->expectEmailValidatedEventNotDispatched(); $returnValue = $this->provider->validateAuthenticationCode($user, self::INVALID_AUTH_CODE); $this->assertFalse($returnValue); }