Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
53 / 53
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10use Phexium\Domain\Id\UuidV4;
11use Phexium\Plugin\IdGenerator\Adapter\UuidV4Generator;
12
13test('Generator returns a UuidV4 object', function (): void {
14    $generator = new UuidV4Generator();
15
16    $result = $generator->generate();
17
18    expect($result)->toBeInstanceOf(UuidV4::class);
19});
20
21test('Generator returns a UuidV4 object from a string', function (): void {
22    $generator = new UuidV4Generator();
23    $validUuid = '550e8400-e29b-41d4-a716-446655440000';
24
25    $result = $generator->from($validUuid);
26
27    expect($result)->toBeInstanceOf(UuidV4::class)
28        ->and($result->getValue())->toBe($validUuid)
29    ;
30});
31
32it('generator throws exception when creating from int', function (): void {
33    $generator = new UuidV4Generator();
34
35    expect(fn (): UuidV4 => $generator->from(123))
36        ->toThrow(LogicException::class, 'UuidV4 generator cannot create UUIDs from integers')
37    ;
38});
39
40it('UuidV4 throws exception when creating from int', function (): void {
41    expect(fn (): UuidV4 => UuidV4::from(123))
42        ->toThrow(LogicException::class, 'UUID cannot be created from an integer')
43    ;
44});
45
46it('UuidV4 throws exception with invalid string', function (): void {
47    expect(fn (): UuidV4 => UuidV4::from('invalid-uuid'))
48        ->toThrow(InvalidArgumentException::class, 'Invalid UUID: invalid-uuid')
49    ;
50});
51
52test('UuidV4 equality', function (): void {
53    $uuid = '550e8400-e29b-41d4-a716-446655440000';
54    $id1 = UuidV4::from($uuid);
55
56    expect($id1->equals($id1))->toBeTrue();
57
58    $anotherUuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
59    $id2 = UuidV4::from($anotherUuid);
60
61    expect($id1->equals($id2))->toBeFalse();
62});
63
64test('UuidV4 can be converted to string', function (): void {
65    $uuid = '550e8400-e29b-41d4-a716-446655440000';
66    $id = UuidV4::from($uuid);
67
68    expect((string) $id)->toBeString()
69        ->toBe($uuid)
70        ->and($id->getValue())->toBe($uuid)
71    ;
72});
73
74test('Generated UUIDs are different', function (): void {
75    $generator = new UuidV4Generator();
76
77    $id1 = $generator->generate();
78    $id2 = $generator->generate();
79
80    expect($id1->equals($id2))->toBeFalse()
81        ->and($id1->getValue())->not->toBe($id2->getValue())
82    ;
83});