Skip to content

Commit

Permalink
Add tests to EmailCodeValidatedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
mdzwigaladp committed Sep 5, 2024
1 parent 2091bf3 commit 4ba8156
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Scheb\TwoFactorBundle\Event;
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event;

readonly class EmailCodeValidated
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Scheb\TwoFactorBundle\Event;
namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event;

class EmailTwoFactorEvents
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 4ba8156

Please sign in to comment.