|
| 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\UuidFactory; |
| 16 | +use Symfony\Component\Uid\Uuid; |
| 17 | +use Symfony\Component\Uid\UuidV1; |
| 18 | +use Symfony\Component\Uid\UuidV3; |
| 19 | +use Symfony\Component\Uid\UuidV4; |
| 20 | +use Symfony\Component\Uid\UuidV5; |
| 21 | +use Symfony\Component\Uid\UuidV6; |
| 22 | + |
| 23 | +final class UuidFactoryTest extends TestCase |
| 24 | +{ |
| 25 | + public function testCreateNamedDefaultVersion() |
| 26 | + { |
| 27 | + $this->assertInstanceOf(UuidV5::class, (new UuidFactory())->createNamed('foo')); |
| 28 | + $this->assertInstanceOf(UuidV3::class, (new UuidFactory(UuidV3::class))->createNamed('foo')); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCreateNamed() |
| 32 | + { |
| 33 | + $uuidFactory = new UuidFactory(); |
| 34 | + |
| 35 | + // Test custom namespace |
| 36 | + $uuid1 = $uuidFactory->createNamed('foo', Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84')); |
| 37 | + $this->assertInstanceOf(UuidV5::class, $uuid1); |
| 38 | + $this->assertSame('d521ceb7-3e31-5954-b873-92992c697ab9', (string) $uuid1); |
| 39 | + |
| 40 | + // Test default namespace |
| 41 | + $uuidFactory = $uuidFactory->withDefaultNamespace(Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84')); |
| 42 | + $uuid2 = $uuidFactory->createNamed('foo'); |
| 43 | + $this->assertInstanceOf(UuidV5::class, $uuid2); |
| 44 | + $this->assertTrue($uuid1->equals($uuid2)); |
| 45 | + |
| 46 | + // Test default namespace override |
| 47 | + $uuid3 = $uuidFactory->createNamed('foo', Uuid::v4()); |
| 48 | + $this->assertFalse($uuid2->equals($uuid3)); |
| 49 | + |
| 50 | + // Test version override |
| 51 | + $uuid4 = $uuidFactory->createNamed('foo', null, UuidV3::class); |
| 52 | + $this->assertInstanceOf(UuidV3::class, $uuid4); |
| 53 | + } |
| 54 | + |
| 55 | + public function testCreateNamedWithUnsupportedVersion() |
| 56 | + { |
| 57 | + $this->expectException(\InvalidArgumentException::class); |
| 58 | + $this->expectExceptionMessage('The "Symfony\Component\Uid\UuidV1" version is unsupported.'); |
| 59 | + |
| 60 | + (new UuidFactory())->createNamed('foo', null, UuidV1::class); |
| 61 | + } |
| 62 | + |
| 63 | + public function testCreateTimedDefaultVersion() |
| 64 | + { |
| 65 | + $this->assertInstanceOf(UuidV6::class, (new UuidFactory())->createTimed()); |
| 66 | + $this->assertInstanceOf(UuidV1::class, (new UuidFactory(null, UuidV1::class))->createTimed()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testCreateTimed() |
| 70 | + { |
| 71 | + $uuidFactory = new UuidFactory(); |
| 72 | + |
| 73 | + // Test custom timestamp and node |
| 74 | + $uuid1 = $uuidFactory->createTimed('138303697380560000', Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84')); |
| 75 | + $this->assertInstanceOf(UuidV6::class, $uuid1); |
| 76 | + $this->assertSame(1611076938.056, $uuid1->getTime()); |
| 77 | + $this->assertSame('c10ab929ae84', $uuid1->getNode()); |
| 78 | + |
| 79 | + // Test default node |
| 80 | + $uuidFactory = $uuidFactory->withDefaultNode(Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84')); |
| 81 | + $uuid2 = $uuidFactory->createTimed(); |
| 82 | + $this->assertInstanceOf(UuidV6::class, $uuid2); |
| 83 | + $this->assertSame('c10ab929ae84', $uuid2->getNode()); |
| 84 | + |
| 85 | + // Test default node override |
| 86 | + $uuid3 = $uuidFactory->createTimed(null, Uuid::fromString('7c1ede70-3586-48ed-a984-23c8018d9174')); |
| 87 | + $this->assertInstanceOf(UuidV6::class, $uuid3); |
| 88 | + $this->assertSame('23c8018d9174', $uuid3->getNode()); |
| 89 | + |
| 90 | + // Test version override |
| 91 | + $uuid4 = $uuidFactory->createTimed(null, null, UuidV1::class); |
| 92 | + $this->assertInstanceOf(UuidV1::class, $uuid4); |
| 93 | + $this->assertSame($uuid2->getNode(), $uuid4->getNode()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testCreateTimedWithUnsupportedVersion() |
| 97 | + { |
| 98 | + $this->expectException(\InvalidArgumentException::class); |
| 99 | + $this->expectExceptionMessage('The "Symfony\Component\Uid\UuidV4" version is unsupported.'); |
| 100 | + |
| 101 | + (new UuidFactory())->createTimed(null, null, UuidV4::class); |
| 102 | + } |
| 103 | + |
| 104 | + public function testCreateRandom() |
| 105 | + { |
| 106 | + $this->assertInstanceOf(UuidV4::class, (new UuidFactory())->createRandom()); |
| 107 | + } |
| 108 | +} |
0 commit comments