ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
UuidTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("vendor/composer/vendor/autoload.php");
22
23use PHPUnit\Framework\TestCase;
26use Ramsey\Uuid\Exception\InvalidUuidStringException;
27
28class UuidTest extends TestCase
29{
30 private const VALID_UUID4 = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/';
31
32 private const UUID4 = 'f47ac10b-58cc-4372-a567-0e02b2c3d479';
33 private const NO_UUID = 'lorem ipsum dolor';
34
35 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
36 public function test_init(): Factory
37 {
38 return new Factory();
39 }
40
41 #[\PHPUnit\Framework\Attributes\Depends('test_init')]
42 public function test_uuid4(): void
43 {
44 $factory = new Factory();
45 $uuid = $factory->uuid4();
46
47 $this->assertMatchesRegularExpression(self::VALID_UUID4, $uuid->toString());
48 }
49
50 #[\PHPUnit\Framework\Attributes\Depends('test_init')]
51 public function test_uuid4_string(): void
52 {
53 $factory = new Factory();
54 $uuid = $factory->uuid4AsString();
55
56 $this->assertIsString($uuid);
57 $this->assertMatchesRegularExpression(self::VALID_UUID4, $uuid);
58 }
59
64 #[\PHPUnit\Framework\Attributes\Depends('test_init')]
65 public function test_uuid3_is_stable(): void
66 {
67 $factory = new Factory();
68
69 $this->assertSame(
70 'cdefba47-4fc6-3904-99e9-989195f87f2c',
71 $factory->uuid3AsString('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'www.ilias.de')
72 );
73 // namespace without hyphens, as stored by CmiXapi
74 $this->assertSame(
75 'd60304bc-9cb8-3988-9fa1-e6953cc506e7',
76 $factory->uuid3AsString('0123456789abcdef0123456789abcdef', '12-34')
77 );
78 $this->assertSame(
79 'cdefba47-4fc6-3904-99e9-989195f87f2c',
80 $factory->uuid3($factory->fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), 'www.ilias.de')->toString()
81 );
82 }
83
84 #[\PHPUnit\Framework\Attributes\Depends('test_init')]
85 public function test_from_string(): void
86 {
87 $factory = new Factory();
88 $uuid = $factory->fromString(self::UUID4);
89
90 $this->assertMatchesRegularExpression(self::VALID_UUID4, $uuid->toString());
91 $this->assertEquals(self::UUID4, $uuid->toString());
92 }
93
94 #[\PHPUnit\Framework\Attributes\Depends('test_init')]
95 public function test_from_illegal_string(): void
96 {
97 $this->expectException(InvalidUuidStringException::class);
98
99 $factory = new Factory();
100 $factory->fromString(self::NO_UUID);
101 }
102}
test_from_illegal_string()
Definition: UuidTest.php:95
test_uuid4_string()
Definition: UuidTest.php:51
const VALID_UUID4
Definition: UuidTest.php:30
const NO_UUID
Definition: UuidTest.php:33
test_uuid4()
Definition: UuidTest.php:42
test_from_string()
Definition: UuidTest.php:85
test_init()
Definition: UuidTest.php:36
const UUID4
Definition: UuidTest.php:32
test_uuid3_is_stable()
Name-based UUIDs must never change: they are stored and compared, e.g.
Definition: UuidTest.php:65