|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Uid\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Uid\UidFactory; |
| 16 | + |
| 17 | +final class UidFactoryTest extends TestCase |
| 18 | +{ |
| 19 | + public function testCreateUlidWithoutArguments() |
| 20 | + { |
| 21 | + (new UidFactory())->createUlid(); |
| 22 | + |
| 23 | + $this->addToAssertionCount(1); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider provideCreateUlid |
| 28 | + */ |
| 29 | + public function testCreateUlid(?float $expectedTime, ?string $expectedRandomness, ?string $timestamp, ?string $randomness) |
| 30 | + { |
| 31 | + $ulid = (new UidFactory())->createUlid($timestamp, $randomness); |
| 32 | + |
| 33 | + if (null === $expectedTime && null === $expectedRandomness) { |
| 34 | + $this->addToAssertionCount(1); |
| 35 | + |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (null !== $expectedTime) { |
| 40 | + $this->assertSame($expectedTime, $ulid->getTime()); |
| 41 | + } |
| 42 | + |
| 43 | + if (null !== $expectedRandomness) { |
| 44 | + $this->assertStringEndsWith($expectedRandomness, $ulid->toBase32()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function provideCreateUlid() |
| 49 | + { |
| 50 | + foreach ([ |
| 51 | + [null, null], |
| 52 | + [0.0, '0'], |
| 53 | + ] as [$expectedTimestamp, $timestamp]) { |
| 54 | + foreach ([ |
| 55 | + null, |
| 56 | + '0000000000000000'
6A69
;, |
| 57 | + 'ZZZZZZZZZZZZZZZZ', |
| 58 | + '2345ABCDEFGHJKMN', |
| 59 | + ] as $randomness) { |
| 60 | + yield [$expectedTimestamp, $randomness, $timestamp, $randomness]; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider provideCreateUuid |
| 67 | + */ |
| 68 | + public function testCreateUuidV1(?float $expectedTime, ?string $expectedNode, ?string $timestamp, ?string $node) |
| 69 | + { |
| 70 | + $uuid = (new UidFactory())->createUuidV1($timestamp, $node); |
| 71 | + |
| 72 | + if (null === $expectedTime && null === $expectedNode) { |
| 73 | + $this->addToAssertionCount(1); |
| 74 | + |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (null !== $expectedTime) { |
| 79 | + $this->assertSame($expectedTime, $uuid->getTime()); |
| 80 | + } |
| 81 | + |
| 82 | + if (null !== $expectedNode) { |
| 83 | + $this->assertSame($expectedNode, $uuid->getNode()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @dataProvider provideCreateUuid |
| 89 | + */ |
| 90 | + public function testCreateUuidV4(?float $expectedTime, ?string $expectedNode, ?string $timestamp, ?string $node) |
| 91 | + { |
| 92 | + $uuid = (new UidFactory())->createUuidV4($timestamp, $node); |
| 93 | + |
| 94 | + if (null === $expectedTime && null === $expectedNode) { |
| 95 | + $this->addToAssertionCount(1); |
| 96 | + |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if (null !== $expectedTime) { |
| 101 | + $this->assertSame($expectedTime, $uuid->getTime()); |
| 102 | + } |
| 103 | + |
| 104 | + if (null !== $expectedNode) { |
| 105 | + $this->assertSame($expectedNode, $uuid->getNode()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @dataProvider provideCreateUuid |
| 111 | + */ |
| 112 | + public function testCreateUuidV6(?float $expectedTime, ?string $expectedNode, ?string $timestamp, ?string $node) |
| 113 | + { |
| 114 | + $uuid = (new UidFactory())->createUuidV6($timestamp, $node); |
| 115 | + |
| 116 | + if (null === $expectedTime && null === $expectedNode) { |
| 117 | + $this->addToAssertionCount(1); |
| 118 | + |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if (null !== $expectedTime) { |
| 123 | + $this->assertSame($expectedTime, $uuid->getTime()); |
| 124 | + } |
| 125 | + |
| 126 | + if (null !== $expectedNode) { |
| 127 | + $this->assertSame($expectedNode, $uuid->getNode()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function provideCreateUuid() |
| 132 | + { |
| 133 | + foreach ([ |
| 134 | + [-12219292800.0, '0'], |
| 135 | + [0, (string) 0x01b21dd213814000], |
| 136 | + [103072857660.6847, '999999999999999999999'], |
| 137 | + ] as [$expectedTimestamp, $timestamp]) { |
| 138 | + foreach ([ |
| 139 | + null, |
| 140 | + '000000000000', |
| 141 | + 'ffffffffffff', |
| 142 | + '370a6f305c0d', |
| 143 | + ] as $node) { |
| 144 | + yield [$expectedTimestamp, $node, $timestamp, $node]; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments