|
| 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\Bridge\Doctrine\Tests\IdGenerator; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator; |
| 16 | +use Symfony\Component\Uid\Factory\UuidFactory; |
| 17 | +use Symfony\Component\Uid\NilUuid; |
| 18 | +use Symfony\Component\Uid\Uuid; |
| 19 | +use Symfony\Component\Uid\UuidV4; |
| 20 | +use Symfony\Component\Uid\UuidV6; |
| 21 | + |
| 22 | +/** |
| 23 | + * @requires function \Symfony\Component\Uid\Factory\UuidFactory::create |
| 24 | + */ |
| 25 | +class UuidGeneratorTest extends TestCase |
| 26 | +{ |
| 27 | + public function testUuidCanBeGenerated() |
| 28 | + { |
| 29 | + $em = new EntityManager(); |
| 30 | + $generator = new UuidGenerator(); |
| 31 | + $uuid = $generator->generate($em, new Entity()); |
| 32 | + |
| 33 | + $this->assertInstanceOf(Uuid::class, $uuid); |
| 34 | + } |
| 35 | + |
| 36 | + public function testCustomUuidfactory() |
| 37 | + { |
| 38 | + $uuid = new NilUuid(); |
| 39 | + $em = new EntityManager(); |
| 40 | + $factory = $this->createMock(UuidFactory::class); |
| 41 | + $factory->expects($this->any()) |
| 42 | + ->method('create') |
| 43 | + ->willReturn($uuid); |
| 44 | + $generator = new UuidGenerator($factory); |
| 45 | + |
| 46 | + $this->assertSame($uuid, $generator->generate($em, new Entity())); |
| 47 | + } |
| 48 | + |
| 49 | + public function testUuidfactory() |
| 50 | + { |
| 51 | + $em = new EntityManager(); |
| 52 | + $generator = new UuidGenerator(); |
| 53 | + $this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity())); |
| 54 | + |
| 55 | + $generator = $generator->randomBased(); |
| 56 | + $this->assertInstanceOf(UuidV4::class, $generator->generate($em, new Entity())); |
| 57 | + |
| 58 | + $generator = $generator->timeBased(); |
| 59 | + $this->assertInstanceOf(UuidV6::class, $generator->generate($em, new Entity())); |
| 60 | + |
| 61 | + $generator = $generator->nameBased('prop1', Uuid::NAMESPACE_OID); |
| 62 | + $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity())); |
| 63 | + |
| 64 | + $generator = $generator->nameBased('prop2', Uuid::NAMESPACE_OID); |
| 65 | + $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '2'), $generator->generate($em, new Entity())); |
| 66 | + |
| 67 | + $generator = $generator->nameBased('getProp4', Uuid::NAMESPACE_OID); |
| 68 | + $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '4'), $generator->generate($em, new Entity())); |
| 69 | + |
| 70 | + $factory = new UuidFactory(6, 6, 5, 5, null, Uuid::NAMESPACE_OID); |
| 71 | + $generator = new UuidGenerator($factory); |
| 72 | + $generator = $generator->nameBased('prop1'); |
| 73 | + $this->assertEquals(Uuid::v5(new Uuid(Uuid::NAMESPACE_OID), '3'), $generator->generate($em, new Entity())); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class Entity |
| 78 | +{ |
| 79 | + public $prop1 = 1; |
| 80 | + public $prop2 = 2; |
| 81 | + |
| 82 | + public function prop1() |
| 83 | + { |
| 84 | + return 3; |
| 85 | + } |
| 86 | + |
| 87 | + public function getProp4() |
| 88 | + { |
| 89 | + return 4; |
| 90 | + } |
| 91 | +} |
0 commit comments