Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
58 / 58
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
10pest()->group('integration');
11
12use Phexium\Domain\Id\UuidV7;
13use Phexium\Plugin\IdGenerator\Adapter\UuidV7Generator;
14
15describe('Generation', function (): void {
16    it('generates a UuidV7 object', function (): void {
17        $generator = new UuidV7Generator();
18
19        $result = $generator->generate();
20
21        expect($result)->toBeInstanceOf(UuidV7::class);
22    });
23
24    it('creates a UuidV7 from a string', function (): void {
25        $generator = new UuidV7Generator();
26        $validUuid = '018e89e8-86e0-7a6c-a982-3b4e8e8d8e8e';
27
28        $result = $generator->from($validUuid);
29
30        expect($result)->toBeInstanceOf(UuidV7::class)
31            ->and($result->getValue())->toBe($validUuid)
32        ;
33    });
34
35    it('converts to string', function (): void {
36        $uuid = '018e89e8-86e0-7a6c-a982-3b4e8e8d8e8e';
37        $id = UuidV7::from($uuid);
38
39        expect((string) $id)->toBeString()
40            ->toBe($uuid)
41            ->and($id->getValue())->toBe($uuid)
42        ;
43    });
44
45    it('throws exception when generator creates from int', function (): void {
46        $generator = new UuidV7Generator();
47
48        expect(fn (): UuidV7 => $generator->from(123))
49            ->toThrow(LogicException::class, 'UuidV7 generator cannot create UUIDs from integers')
50        ;
51    });
52
53    it('throws exception when UuidV7 creates from int', function (): void {
54        expect(fn (): UuidV7 => UuidV7::from(123))
55            ->toThrow(LogicException::class, 'UUID cannot be created from an integer')
56        ;
57    });
58
59    it('throws exception with invalid string', function (): void {
60        expect(fn (): UuidV7 => UuidV7::from('invalid-uuid'))
61            ->toThrow(InvalidArgumentException::class, 'Invalid UUID: invalid-uuid')
62        ;
63    });
64});
65
66describe('Uniqueness', function (): void {
67    it('compares by value', function (): void {
68        $uuid = '018e89e8-86e0-7a6c-a982-3b4e8e8d8e8e';
69        $id1 = UuidV7::from($uuid);
70
71        expect($id1->equals($id1))->toBeTrue();
72
73        $anotherUuid = '018e89e8-86e0-7a6c-a982-3b4e8e8d8e8f';
74        $id2 = UuidV7::from($anotherUuid);
75
76        expect($id1->equals($id2))->toBeFalse();
77    });
78
79    it('generates different UUIDs', function (): void {
80        $generator = new UuidV7Generator();
81
82        $id1 = $generator->generate();
83        $id2 = $generator->generate();
84
85        expect($id1->equals($id2))->toBeFalse()
86            ->and($id1->getValue())->not->toBe($id2->getValue())
87        ;
88    });
89});