Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
<?php
namespace Tests\Unit\Services\Infrastructure\User;
use HiEvents\Services\Infrastructure\User\EmailVerificationCodeService;
use Illuminate\Cache\Repository;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;
class EmailVerificationCodeServiceTest extends TestCase
{
private EmailVerificationCodeService $service;
private MockInterface|Repository $cacheRepository;
protected function setUp(): void
{
parent::setUp();
$this->cacheRepository = Mockery::mock(Repository::class);
$this->service = new EmailVerificationCodeService($this->cacheRepository);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_store_and_return_code(): void
{
// Given
$email = '[email protected]';
$expectedCacheKey = 'email_verification_code:'.$email;
// Expect
$this->cacheRepository
->shouldReceive('put')
->once()
->withArgs(function ($key, $code, $expiry) use ($expectedCacheKey) {
return $key === $expectedCacheKey
&& $code >= 10000
&& $code <= 99999
&& $expiry->greaterThan(now()->addMinutes(29))
&& $expiry->lessThanOrEqualTo(now()->addMinutes(30)->addSecond());
});
// When
$code = $this->service->storeAndReturnCode($email);
// Then
$this->assertIsInt($code);
$this->assertGreaterThanOrEqual(10000, $code);
$this->assertLessThanOrEqual(99999, $code);
}
public function test_verify_code_with_valid_code(): void
{
// Given
$email = '[email protected]';
$validCode = '12345';
$expectedCacheKey = 'email_verification_code:'.$email;
// Expect
$this->cacheRepository
->shouldReceive('get')
->once()
->with($expectedCacheKey)
->andReturn($validCode);
$this->cacheRepository
->shouldReceive('forget')
->once()
->with($expectedCacheKey);
// When
$result = $this->service->verifyCode($email, $validCode);
// Then
$this->assertTrue($result);
}
public function test_verify_code_with_invalid_code(): void
{
// Given
$email = '[email protected]';
$storedCode = '12345';
$providedCode = '54321';
$expectedCacheKey = 'email_verification_code:'.$email;
// Expect
$this->cacheRepository
->shouldReceive('get')
->once()
->with($expectedCacheKey)
->andReturn($storedCode);
$this->cacheRepository
->shouldNotReceive('forget');
// When
$result = $this->service->verifyCode($email, $providedCode);
// Then
$this->assertFalse($result);
}
public function test_verify_code_with_no_stored_code(): void
{
// Given
$email = '[email protected]';
$providedCode = '12345';
$expectedCacheKey = 'email_verification_code:'.$email;
// Expect
$this->cacheRepository
->shouldReceive('get')
->once()
->with($expectedCacheKey)
->andReturn(null);
$this->cacheRepository
->shouldNotReceive('forget');
// When
$result = $this->service->verifyCode($email, $providedCode);
// Then
$this->assertFalse($result);
}
public function test_multiple_verification_codes_for_different_emails(): void
{
// Given
$email1 = '[email protected]';
$email2 = '[email protected]';
// Expect - Store codes for two different emails
$this->cacheRepository
->shouldReceive('put')
->once()
->withArgs(function ($key, $code, $expiry) use ($email1) {
return $key === 'email_verification_code:'.$email1;
});
$this->cacheRepository
->shouldReceive('put')
->once()
->withArgs(function ($key, $code, $expiry) use ($email2) {
return $key === 'email_verification_code:'.$email2;
});
// When
$code1 = $this->service->storeAndReturnCode($email1);
$code2 = $this->service->storeAndReturnCode($email2);
// Then
$this->assertIsInt($code1);
$this->assertIsInt($code2);
// Codes might be the same by chance, but they're generated independently
}
public function test_verify_code_is_case_insensitive_for_email(): void
{
// Given
$emailLower = '[email protected]';
$emailUpper = '[email protected]';
$code = '12345';
// Note: The service uses emails as-is, so case sensitivity depends on implementation
// This test documents the current behavior
// Expect - Different cache keys for different cases
$this->cacheRepository
->shouldReceive('get')
->once()
->with('email_verification_code:'.$emailUpper)
->andReturn(null);
// When
$result = $this->service->verifyCode($emailUpper, $code);
// Then
$this->assertFalse($result);
}
public function test_store_and_return_code_generates_unique_codes_on_multiple_calls(): void
{
// Given
$email = '[email protected]';
$generatedCodes = [];
// Expect
$this->cacheRepository
->shouldReceive('put')
->times(10)
->withArgs(function ($key, $code) use (&$generatedCodes) {
$generatedCodes[] = $code;
return true;
});
// When
for ($i = 0; $i < 10; $i++) {
$this->service->storeAndReturnCode($email);
}
// Then
// While codes could theoretically be the same, it's very unlikely
// At minimum, all codes should be in the valid range
foreach ($generatedCodes as $code) {
$this->assertGreaterThanOrEqual(10000, $code);
$this->assertLessThanOrEqual(99999, $code);
}
}
public function test_verify_code_only_works_once(): void
{
// Given
$email = '[email protected]';
$code = '12345';
$cacheKey = 'email_verification_code:'.$email;
// First verification attempt
$this->cacheRepository
->shouldReceive('get')
->once()
->with($cacheKey)
->andReturn($code);
$this->cacheRepository
->shouldReceive('forget')
->once()
->with($cacheKey);
// When
$firstAttempt = $this->service->verifyCode($email, $code);
// Then
$this->assertTrue($firstAttempt);
// Second verification attempt
$this->cacheRepository
->shouldReceive('get')
->once()
->with($cacheKey)
->andReturn(null);
// When
$secondAttempt = $this->service->verifyCode($email, $code);
// Then
$this->assertFalse($secondAttempt);
}
}