52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Services;
|
|
|
|
use Domain\Models\ValueObjects\Code;
|
|
use Domain\Ports\Directories\CodeDirectory;
|
|
use Domain\Services\CodeGenerationService;
|
|
use Mockery;
|
|
|
|
describe('Code Generation Service', function () {
|
|
it('generates a valid code of exactly 10 characters', function () {
|
|
// GIVEN
|
|
$repository = Mockery::mock(CodeDirectory::class);
|
|
$repository->shouldReceive('exists')->andReturn(false);
|
|
|
|
$service = new CodeGenerationService($repository);
|
|
|
|
// WHEN
|
|
$code = $service->generate();
|
|
|
|
// THEN
|
|
expect($code)->toBeInstanceOf(Code::class)
|
|
->and(strlen($code->value))->toBe(10);
|
|
});
|
|
|
|
it('regenerates code if it already exists', function () {
|
|
// GIVEN
|
|
$repository = Mockery::mock(CodeDirectory::class);
|
|
$repository->shouldReceive('exists')->times(2)->andReturn(true, false);
|
|
|
|
$service = new CodeGenerationService($repository);
|
|
|
|
// WHEN
|
|
$code = $service->generate();
|
|
|
|
// THEN
|
|
expect(strlen($code->value))->toBe(10);
|
|
});
|
|
|
|
it('throws exception for code with bad characters', function () {
|
|
// GIVEN
|
|
$invalidCodeString = 'ABCDEFGH1O';
|
|
|
|
// WHEN
|
|
$action = fn () => new Code($invalidCodeString);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Code contains invalid characters');
|
|
});
|
|
});
|