Skip to content

Commit e6ffa3c

Browse files
committed
Add UUID generator
1 parent 7847f81 commit e6ffa3c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\IdentityGeneratorSymfony\Uuid;
6+
7+
use Gember\EventSourcing\Util\Generator\Identity\IdentityGenerator;
8+
use Symfony\Component\Uid\Factory\UuidFactory;
9+
10+
final readonly class SymfonyUuidIdentityGenerator implements IdentityGenerator
11+
{
12+
public function __construct(
13+
private UuidFactory $uuidFactory,
14+
) {}
15+
16+
public function generate(): string
17+
{
18+
return (string) $this->uuidFactory->randomBased()->create();
19+
}
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\IdentityGeneratorSymfony\Test\Uuid;
6+
7+
use Gember\IdentityGeneratorSymfony\Uuid\SymfonyUuidIdentityGenerator;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Uid\Factory\UuidFactory;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class SymfonyUuidIdentityGeneratorTest extends TestCase
16+
{
17+
private SymfonyUuidIdentityGenerator $identityGenerator;
18+
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->identityGenerator = new SymfonyUuidIdentityGenerator(
24+
new UuidFactory(),
25+
);
26+
}
27+
28+
#[Test]
29+
public function itShouldGenerateUuid(): void
30+
{
31+
$uuid = $this->identityGenerator->generate();
32+
33+
self::assertMatchesRegularExpression(
34+
'#^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$#',
35+
$uuid,
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)