PageRenderTime 84ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Uid/Tests/UuidTest.php

https://github.com/FabienD/symfony
PHP | 336 lines | 242 code | 63 blank | 31 comment | 0 complexity | 0a342a5e921fee1892e47f1a344b69da MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Uid\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Uid\NilUuid;
  13. use Symfony\Component\Uid\Tests\Fixtures\CustomUuid;
  14. use Symfony\Component\Uid\Ulid;
  15. use Symfony\Component\Uid\Uuid;
  16. use Symfony\Component\Uid\UuidV1;
  17. use Symfony\Component\Uid\UuidV3;
  18. use Symfony\Component\Uid\UuidV4;
  19. use Symfony\Component\Uid\UuidV5;
  20. use Symfony\Component\Uid\UuidV6;
  21. class UuidTest extends TestCase
  22. {
  23. private const A_UUID_V1 = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
  24. private const A_UUID_V4 = 'd6b3345b-2905-4048-a83c-b5988e765d98';
  25. /**
  26. * @dataProvider provideInvalidUuids
  27. */
  28. public function testConstructorWithInvalidUuid(string $uuid)
  29. {
  30. $this->expectException(\InvalidArgumentException::class);
  31. $this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');
  32. Uuid::fromString($uuid);
  33. }
  34. public function provideInvalidUuids(): iterable
  35. {
  36. yield ['this is not a uuid'];
  37. yield ['these are just thirty-six characters'];
  38. }
  39. public function testConstructorWithValidUuid()
  40. {
  41. $uuid = new UuidV4(self::A_UUID_V4);
  42. $this->assertSame(self::A_UUID_V4, (string) $uuid);
  43. $this->assertSame('"'.self::A_UUID_V4.'"', json_encode($uuid));
  44. }
  45. public function testV1()
  46. {
  47. $uuid = Uuid::v1();
  48. $this->assertInstanceOf(UuidV1::class, $uuid);
  49. $uuid = new UuidV1(self::A_UUID_V1);
  50. $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '1583245966.746458'), $uuid->getDateTime());
  51. $this->assertSame('3499710062d0', $uuid->getNode());
  52. }
  53. public function testV3()
  54. {
  55. $uuid = Uuid::v3(new UuidV4(self::A_UUID_V4), 'the name');
  56. $this->assertInstanceOf(UuidV3::class, $uuid);
  57. $this->assertSame('8dac64d3-937a-3e7c-aa1d-d5d6c06a61f5', (string) $uuid);
  58. }
  59. public function testV4()
  60. {
  61. $uuid = Uuid::v4();
  62. $this->assertInstanceOf(UuidV4::class, $uuid);
  63. }
  64. public function testV5()
  65. {
  66. $uuid = Uuid::v5(new UuidV4('ec07aa88-f84e-47b9-a581-1c6b30a2f484'), 'the name');
  67. $this->assertInstanceOf(UuidV5::class, $uuid);
  68. $this->assertSame('851def0c-b9c7-55aa-a991-130e769ec0a9', (string) $uuid);
  69. }
  70. public function testV6()
  71. {
  72. $uuid = Uuid::v6();
  73. $this->assertInstanceOf(UuidV6::class, $uuid);
  74. $uuid = new UuidV6(substr_replace(self::A_UUID_V1, '6', 14, 1));
  75. $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '85916308548.278321'), $uuid->getDateTime());
  76. $this->assertSame('3499710062d0', $uuid->getNode());
  77. }
  78. public function testV6IsSeeded()
  79. {
  80. $uuidV1 = Uuid::v1();
  81. $uuidV6 = Uuid::v6();
  82. $this->assertNotSame(substr($uuidV1, 24), substr($uuidV6, 24));
  83. }
  84. public function testBinary()
  85. {
  86. $uuid = new UuidV4(self::A_UUID_V4);
  87. $uuid = Uuid::fromString($uuid->toBinary());
  88. $this->assertInstanceOf(UuidV4::class, $uuid);
  89. $this->assertSame(self::A_UUID_V4, (string) $uuid);
  90. }
  91. public function testFromUlid()
  92. {
  93. $ulid = new Ulid();
  94. $uuid = Uuid::fromString($ulid);
  95. $this->assertSame((string) $ulid, $uuid->toBase32());
  96. $this->assertSame((string) $uuid, $uuid->toRfc4122());
  97. $this->assertTrue($uuid->equals(Uuid::fromString($ulid)));
  98. }
  99. public function testBase58()
  100. {
  101. $uuid = new NilUuid();
  102. $this->assertSame('1111111111111111111111', $uuid->toBase58());
  103. $uuid = Uuid::fromString("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
  104. $this->assertSame('YcVfxkQb6JRzqk5kF2tNLv', $uuid->toBase58());
  105. $this->assertTrue($uuid->equals(Uuid::fromString('YcVfxkQb6JRzqk5kF2tNLv')));
  106. }
  107. public function testIsValid()
  108. {
  109. $this->assertFalse(Uuid::isValid('not a uuid'));
  110. $this->assertTrue(Uuid::isValid(self::A_UUID_V4));
  111. $this->assertFalse(UuidV4::isValid(self::A_UUID_V1));
  112. $this->assertTrue(UuidV4::isValid(self::A_UUID_V4));
  113. }
  114. public function testEquals()
  115. {
  116. $uuid1 = new UuidV1(self::A_UUID_V1);
  117. $uuid2 = new UuidV4(self::A_UUID_V4);
  118. $this->assertTrue($uuid1->equals($uuid1));
  119. $this->assertFalse($uuid1->equals($uuid2));
  120. }
  121. /**
  122. * @dataProvider provideInvalidEqualType
  123. */
  124. public function testEqualsAgainstOtherType($other)
  125. {
  126. $this->assertFalse((new UuidV4(self::A_UUID_V4))->equals($other));
  127. }
  128. public function provideInvalidEqualType()
  129. {
  130. yield [null];
  131. yield [self::A_UUID_V1];
  132. yield [self::A_UUID_V4];
  133. yield [new \stdClass()];
  134. }
  135. public function testCompare()
  136. {
  137. $uuids = [];
  138. $uuids[] = $b = new Uuid('00000000-0000-0000-0000-00000000000b');
  139. $uuids[] = $a = new Uuid('00000000-0000-0000-0000-00000000000a');
  140. $uuids[] = $d = new Uuid('00000000-0000-0000-0000-00000000000d');
  141. $uuids[] = $c = new Uuid('00000000-0000-0000-0000-00000000000c');
  142. $this->assertNotSame([$a, $b, $c, $d], $uuids);
  143. usort($uuids, static function (Uuid $a, Uuid $b): int {
  144. return $a->compare($b);
  145. });
  146. $this->assertSame([$a, $b, $c, $d], $uuids);
  147. }
  148. /**
  149. * @testWith ["00000000-0000-0000-0000-000000000000"]
  150. * ["1111111111111111111111"]
  151. * ["00000000000000000000000000"]
  152. */
  153. public function testNilUuid(string $uuid)
  154. {
  155. $uuid = Uuid::fromString($uuid);
  156. $this->assertInstanceOf(NilUuid::class, $uuid);
  157. $this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
  158. }
  159. public function testNewNilUuid()
  160. {
  161. $this->assertSame('00000000-0000-0000-0000-000000000000', (string) new NilUuid());
  162. }
  163. public function testFromBinary()
  164. {
  165. $this->assertEquals(
  166. Uuid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
  167. Uuid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
  168. );
  169. }
  170. /**
  171. * @dataProvider provideInvalidBinaryFormat
  172. */
  173. public function testFromBinaryInvalidFormat(string $ulid)
  174. {
  175. $this->expectException(\InvalidArgumentException::class);
  176. Uuid::fromBinary($ulid);
  177. }
  178. public function provideInvalidBinaryFormat()
  179. {
  180. return [
  181. ['01EW2RYKDCT2SAK454KBR2QG08'],
  182. ['1BVXue8CnY8ogucrHX3TeF'],
  183. ['0177058f-4dac-d0b2-a990-a49af02bc008'],
  184. ];
  185. }
  186. public function testFromBase58()
  187. {
  188. $this->assertEquals(
  189. UuidV1::fromString('94fSqj9oxGtsNbkfQNntwx'),
  190. UuidV1::fromBase58('94fSqj9oxGtsNbkfQNntwx')
  191. );
  192. }
  193. /**
  194. * @dataProvider provideInvalidBase58Format
  195. */
  196. public function testFromBase58InvalidFormat(string $ulid)
  197. {
  198. $this->expectException(\InvalidArgumentException::class);
  199. Uuid::fromBase58($ulid);
  200. }
  201. public function provideInvalidBase58Format()
  202. {
  203. return [
  204. ["\x41\x4C\x08\x92\x57\x1B\x11\xEB\xBF\x70\x93\xF9\xB0\x82\x2C\x57"],
  205. ['219G494NRV27NVYW4KZ6R84B2Q'],
  206. ['414c0892-571b-11eb-bf70-93f9b0822c57'],
  207. ];
  208. }
  209. public function testFromBase32()
  210. {
  211. $this->assertEquals(
  212. UuidV5::fromString('2VN0S74HBDBB0AQRXAHFVG35KK'),
  213. UuidV5::fromBase32('2VN0S74HBDBB0AQRXAHFVG35KK')
  214. );
  215. }
  216. /**
  217. * @dataProvider provideInvalidBase32Format
  218. */
  219. public function testFromBase32InvalidFormat(string $ulid)
  220. {
  221. $this->expectException(\InvalidArgumentException::class);
  222. Uuid::fromBase32($ulid);
  223. }
  224. public function provideInvalidBase32Format()
  225. {
  226. return [
  227. ["\x5B\xA8\x32\x72\x45\x6D\x5A\xC0\xAB\xE3\xAA\x8B\xF7\x01\x96\x73"],
  228. ['CKTRYycTes6WAqSQJsTDaz'],
  229. ['5ba83272-456d-5ac0-abe3-aa8bf7019673'],
  230. ];
  231. }
  232. public function testFromRfc4122()
  233. {
  234. $this->assertEquals(
  235. UuidV6::fromString('1eb571b4-14c0-6893-bf70-2d4c83cf755a'),
  236. UuidV6::fromRfc4122('1eb571b4-14c0-6893-bf70-2d4c83cf755a')
  237. );
  238. }
  239. /**
  240. * @dataProvider provideInvalidRfc4122Format
  241. */
  242. public function testFromRfc4122InvalidFormat(string $ulid)
  243. {
  244. $this->expectException(\InvalidArgumentException::class);
  245. Uuid::fromRfc4122($ulid);
  246. }
  247. public function provideInvalidRfc4122Format()
  248. {
  249. return [
  250. ["\x1E\xB5\x71\xB4\x14\xC0\x68\x93\xBF\x70\x2D\x4C\x83\xCF\x75\x5A"],
  251. ['0YPNRV8560D29VYW1D9J1WYXAT'],
  252. ['4nwTLZ2TdMtTVDE5AwVjaR'],
  253. ];
  254. }
  255. public function testFromStringOnExtendedClassReturnsStatic()
  256. {
  257. $this->assertInstanceOf(CustomUuid::class, CustomUuid::fromString(self::A_UUID_V4));
  258. }
  259. public function testGetDateTime()
  260. {
  261. $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '103072857660.684697'), ((new UuidV1('ffffffff-ffff-1fff-a456-426655440000'))->getDateTime()));
  262. $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.000001'), ((new UuidV1('1381400a-1dd2-11b2-a456-426655440000'))->getDateTime()));
  263. $this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814001-1dd2-11b2-a456-426655440000'))->getDateTime());
  264. $this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13814000-1dd2-11b2-a456-426655440000'))->getDateTime());
  265. $this->assertEquals(new \DateTimeImmutable('@0'), (new UuidV1('13813fff-1dd2-11b2-a456-426655440000'))->getDateTime());
  266. $this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '-0.000001'), ((new UuidV1('13813ff6-1dd2-11b2-a456-426655440000'))->getDateTime()));
  267. $this->assertEquals(new \DateTimeImmutable('@-12219292800'), ((new UuidV1('00000000-0000-1000-a456-426655440000'))->getDateTime()));
  268. }
  269. public function testFromStringBase58Padding()
  270. {
  271. $this->assertInstanceOf(Uuid::class, Uuid::fromString('111111111u9QRyVM94rdmZ'));
  272. }
  273. }