|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Uid\Tests\Factory; |
13 | 13 |
|
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
14 | 15 | use PHPUnit\Framework\TestCase; |
15 | 16 | use Symfony\Component\Uid\Factory\MockUuidFactory; |
16 | 17 | use Symfony\Component\Uid\UuidV1; |
|
23 | 24 |
|
24 | 25 | class MockUuidFactoryTest extends TestCase |
25 | 26 | { |
26 | | - public function testCreateV1() |
| 27 | + #[DataProvider('provideSequences')] |
| 28 | + public function testCreate(iterable $sequence, array $expected) |
27 | 29 | { |
28 | | - $factory = new MockUuidFactory([ |
29 | | - '6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
30 | | - '6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
31 | | - ], UuidV1::class); |
32 | | - |
33 | | - $this->assertSame('6ba7b810-9dad-11d1-80b4-00c04fd430c8', (string) $factory->create()); |
34 | | - $this->assertSame('6ba7b810-9dad-11d1-80b4-00c04fd430c8', (string) $factory->create()); |
| 30 | + $factory = new MockUuidFactory($sequence); |
| 31 | + foreach ($expected as $expectedUuid) { |
| 32 | + $this->assertEquals($expectedUuid, $factory->create()); |
| 33 | + } |
35 | 34 | } |
36 | 35 |
|
37 | | - public function testCreateV3() |
| 36 | + public static function provideSequences(): \Generator |
38 | 37 | { |
39 | | - $factory = new MockUuidFactory([ |
40 | | - '6ba7b810-9dad-31d1-80b4-00c04fd430c8', |
41 | | - '6ba7b810-9dad-31d1-80b4-00c04fd430c8', |
42 | | - ], UuidV3::class); |
43 | | - |
44 | | - $this->assertSame('6ba7b810-9dad-31d1-80b4-00c04fd430c8', (string) $factory->create()); |
45 | | - $this->assertSame('6ba7b810-9dad-31d1-80b4-00c04fd430c8', (string) $factory->create()); |
| 38 | + $uuid1String = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
| 39 | + $uuid3String = '6ba7b810-9dad-31d1-80b4-00c04fd430c8'; |
| 40 | + $uuid4String = '6ba7b810-9dad-41d1-80b4-00c04fd430c8'; |
| 41 | + $uuid5String = '6ba7b810-9dad-51d1-80b4-00c04fd430c8'; |
| 42 | + $uuid6String = '6ba7b810-9dad-61d1-80b4-00c04fd430c8'; |
| 43 | + $uuid7String = '6ba7b810-9dad-71d1-80b4-00c04fd430c8'; |
| 44 | + $uuid8String = '6ba7b810-9dad-81d1-80b4-00c04fd430c8'; |
| 45 | + |
| 46 | + $uuid1 = UuidV1::fromString($uuid1String); |
| 47 | + $uuid3 = UuidV3::fromString($uuid3String); |
| 48 | + $uuid4 = UuidV4::fromString($uuid4String); |
| 49 | + $uuid5 = UuidV5::fromString($uuid5String); |
| 50 | + $uuid6 = UuidV6::fromString($uuid6String); |
| 51 | + $uuid7 = UuidV7::fromString($uuid7String); |
| 52 | + $uuid8 = UuidV8::fromString($uuid8String); |
| 53 | + |
| 54 | + yield 'object sequence' => [ |
| 55 | + [$uuid1, $uuid3, $uuid4, $uuid5, $uuid6, $uuid7, $uuid8], |
| 56 | + [$uuid1, $uuid3, $uuid4, $uuid5, $uuid6, $uuid7, $uuid8], |
| 57 | + ]; |
| 58 | + yield 'string sequence' => [ |
| 59 | + [ |
| 60 | + $uuid1String, |
| 61 | + $uuid3String, |
| 62 | + $uuid4String, |
| 63 | + $uuid5String, |
| 64 | + $uuid6String, |
| 65 | + $uuid7String, |
| 66 | + $uuid8String, |
| 67 | + ], |
| 68 | + [$uuid1, $uuid3, $uuid4, $uuid5, $uuid6, $uuid7, $uuid8], |
| 69 | + ]; |
| 70 | + yield 'mixed sequence' => [ |
| 71 | + [ |
| 72 | + $uuid1, |
| 73 | + $uuid3String, |
| 74 | + $uuid4, |
| 75 | + $uuid5String, |
| 76 | + $uuid6, |
| 77 | + $uuid7String, |
| 78 | + $uuid8, |
| 79 | + ], |
| 80 | + [$uuid1, $uuid3, $uuid4, $uuid5, $uuid6, $uuid7, $uuid8], |
| 81 | + ]; |
46 | 82 | } |
47 | 83 |
|
48 | | - public function testCreateV4() |
| 84 | + public function testExhaustedSequence() |
49 | 85 | { |
50 | | - $factory = new MockUuidFactory([ |
51 | | - '6ba7b810-9dad-41d1-80b4-00c04fd430c8', |
52 | | - '6ba7b810-9dad-41d1-80b4-00c04fd430c8', |
53 | | - ], UuidV4::class); |
54 | | - |
55 | | - $this->assertSame('6ba7b810-9dad-41d1-80b4-00c04fd430c8', (string) $factory->create()); |
56 | | - $this->assertSame('6ba7b810-9dad-41d1-80b4-00c04fd430c8', (string) $factory->create()); |
57 | | - } |
| 86 | + $this->expectException(\RuntimeException::class); |
| 87 | + $this->expectExceptionMessage('No more UUIDs in sequence'); |
58 | 88 |
|
59 | | - public function testCreateV5() |
60 | | - { |
61 | 89 | $factory = new MockUuidFactory([ |
62 | | - '6ba7b810-9dad-51d1-80b4-00c04fd430c8', |
63 | | - '6ba7b810-9dad-51d1-80b4-00c04fd430c8', |
64 | | - ], UuidV5::class); |
| 90 | + UuidV1::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), |
| 91 | + '6ba7b810-9dad-31d1-80b4-00c04fd430c8', |
| 92 | + ]); |
65 | 93 |
|
66 | | - $this->assertSame('6ba7b810-9dad-51d1-80b4-00c04fd430c8', (string) $factory->create()); |
67 | | - $this->assertSame('6ba7b810-9dad-51d1-80b4-00c04fd430c8', (string) $factory->create()); |
| 94 | + $factory->create(); |
| 95 | + $factory->create(); |
| 96 | + $factory->create(); |
68 | 97 | } |
69 | 98 |
|
70 | | - public function testCreateV6() |
| 99 | + public function testRandomBasedReturnsUuidV4FromSequence() |
71 | 100 | { |
| 101 | + $uuid1 = UuidV4::fromString('6ba7b810-9dad-41d1-80b4-00c04fd430c8'); |
| 102 | + $uuid2 = UuidV4::fromString('9d235ae3-a819-41e3-9216-7858734f543d'); |
| 103 | + $uuid3 = '3d813cbb-47fb-4f2e-8c1a-6b5f9e8f1e3b'; |
72 | 104 | $factory = new MockUuidFactory([ |
73 | | - '6ba7b810-9dad-61d1-80b4-00c04fd430c8', |
74 | | - '6ba7b810-9dad-61d1-80b4-00c04fd430c8', |
75 | | - ], UuidV6::class); |
76 | | - |
77 | | - $this->assertSame('6ba7b810-9dad-61d1-80b4-00c04fd430c8', (string) $factory->create()); |
78 | | - $this->assertSame('6ba7b810-9dad-61d1-80b4-00c04fd430c8', (string) $factory->create()); |
| 105 | + $uuid1, |
| 106 | + $uuid2, |
| 107 | + $uuid3, |
| 108 | + ]); |
| 109 | + $randomFactory = $factory->randomBased(); |
| 110 | + |
| 111 | + $this->assertSame($uuid1, $randomFactory->create()); |
| 112 | + $this->assertSame($uuid2, $randomFactory->create()); |
| 113 | + $this->assertEquals(UuidV4::fromString($uuid3), $randomFactory->create()); |
79 | 114 | } |
80 | 115 |
|
81 | | - public function testCreateV7() |
| 116 | + public function testRandomBasedThrowsWhenSequenceIsEmpty() |
82 | 117 | { |
83 | | - $factory = new MockUuidFactory([ |
84 | | - '6ba7b810-9dad-71d1-80b4-00c04fd430c8', |
85 | | - '6ba7b810-9dad-71d1-80b4-00c04fd430c8', |
86 | | - ], UuidV7::class); |
87 | | - |
88 | | - $this->assertSame('6ba7b810-9dad-71d1-80b4-00c04fd430c8', (string) $factory->create()); |
89 | | - $this->assertSame('6ba7b810-9dad-71d1-80b4-00c04fd430c8', (string) $factory->create()); |
90 | | - } |
| 118 | + $this->expectException(\RuntimeException::class); |
| 119 | + $this->expectExceptionMessage('No more UUIDs in sequence'); |
91 | 120 |
|
92 | | - public function testCreateV8() |
93 | | - { |
| 121 | + $uuid1 = UuidV4::fromString('6ba7b810-9dad-41d1-80b4-00c04fd430c8'); |
| 122 | + $uuid2 = '3d813cbb-47fb-4f2e-8c1a-6b5f9e8f1e3b'; |
94 | 123 | $factory = new MockUuidFactory([ |
95 | | - '6ba7b810-9dad-81d1-80b4-00c04fd430c8', |
96 | | - '6ba7b810-9dad-81d1-80b4-00c04fd430c8', |
97 | | - ], UuidV8::class); |
98 | | - |
99 | | - $this->assertSame('6ba7b810-9dad-81d1-80b4-00c04fd430c8', (string) $factory->create()); |
100 | | - $this->assertSame('6ba7b810-9dad-81d1-80b4-00c04fd430c8', (string) $factory->create()); |
| 124 | + $uuid1, |
| 125 | + $uuid2, |
| 126 | + ]); |
| 127 | + $randomFactory = $factory->randomBased(); |
| 128 | + |
| 129 | + $randomFactory->create(); |
| 130 | + $randomFactory->create(); |
| 131 | + $randomFactory->create(); |
101 | 132 | } |
102 | 133 |
|
103 | | - public function testExhaustedSequence() |
| 134 | + public function testRandomBasedThrowsOnInvalidType() |
104 | 135 | { |
105 | | - $this->expectException(\RuntimeException::class); |
106 | | - $this->expectExceptionMessage('No more UUIDs in sequence'); |
107 | | - |
108 | 136 | $factory = new MockUuidFactory([ |
109 | | - '6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
110 | | - ], UuidV1::class); |
| 137 | + UuidV4::fromString('6ba7b810-9dad-41d1-80b4-00c04fd430c8'), |
| 138 | + new \stdClass(), |
| 139 | + ]); |
| 140 | + $randomFactory = $factory->randomBased(); |
111 | 141 |
|
112 | | - $factory->create(); |
113 | | - $factory->create(); |
114 | | - } |
| 142 | + $randomFactory->create(); |
115 | 143 |
|
116 | | - public function testInvalidDefaultClass() |
117 | | - { |
118 | | - $this->expectException(\InvalidArgumentException::class); |
119 | | - $this->expectExceptionMessage('The class "stdClass" must be a subclass of "Symfony\\Component\\Uid\\Uuid".'); |
| 144 | + $this->expectException(\RuntimeException::class); |
| 145 | + $this->expectExceptionMessage('Next UUID in sequence is not a UuidV4 or string: "stdClass".'); |
120 | 146 |
|
121 | | - new MockUuidFactory([], 'stdClass'); |
| 147 | + $randomFactory->create(); |
122 | 148 | } |
123 | 149 | } |
0 commit comments