From c20cb20610a964b6fe96abea40903fb69e6cfbd9 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 11 Aug 2017 12:35:39 +0300 Subject: [PATCH] Fix custom typed ID reconstruction Fixes #254 --- .../DataFixtures/ProxyReferenceRepository.php | 8 +-- .../ProxyReferenceRepositoryTest.php | 45 +++++++++++++++++ .../Common/DataFixtures/TestEntity/Link.php | 49 +++++++++++++++++++ .../DataFixtures/TestTypes/UuidType.php | 37 ++++++++++++++ .../DataFixtures/TestValueObjects/Uuid.php | 49 +++++++++++++++++++ 5 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Link.php create mode 100644 tests/Doctrine/Tests/Common/DataFixtures/TestTypes/UuidType.php create mode 100644 tests/Doctrine/Tests/Common/DataFixtures/TestValueObjects/Uuid.php diff --git a/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php b/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php index 6fcffca0..44823f7d 100644 --- a/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php +++ b/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php @@ -10,9 +10,9 @@ use function file_get_contents; use function file_put_contents; use function get_class; -use function json_decode; -use function json_encode; +use function serialize; use function substr; +use function unserialize; /** * Proxy reference repository @@ -58,7 +58,7 @@ public function serialize() $simpleReferences[$name] = [$className, $this->getIdentifier($reference, $unitOfWork)]; } - return json_encode([ + return serialize([ 'references' => $simpleReferences, 'identities' => $this->getIdentities(), ]); @@ -71,7 +71,7 @@ public function serialize() */ public function unserialize($serializedData) { - $repositoryData = json_decode($serializedData, true); + $repositoryData = unserialize($serializedData); $references = $repositoryData['references']; foreach ($references as $name => $proxyReference) { diff --git a/tests/Doctrine/Tests/Common/DataFixtures/ProxyReferenceRepositoryTest.php b/tests/Doctrine/Tests/Common/DataFixtures/ProxyReferenceRepositoryTest.php index 9b0ece3f..99ebbac7 100644 --- a/tests/Doctrine/Tests/Common/DataFixtures/ProxyReferenceRepositoryTest.php +++ b/tests/Doctrine/Tests/Common/DataFixtures/ProxyReferenceRepositoryTest.php @@ -6,9 +6,13 @@ use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener; use Doctrine\Common\DataFixtures\ProxyReferenceRepository; +use Doctrine\DBAL\Types\Type; use Doctrine\ORM\Proxy\Proxy; use Doctrine\ORM\Tools\SchemaTool; +use Doctrine\Tests\Common\DataFixtures\TestEntity\Link; use Doctrine\Tests\Common\DataFixtures\TestEntity\Role; +use Doctrine\Tests\Common\DataFixtures\TestTypes\UuidType; +use Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid; /** * Test ProxyReferenceRepository. @@ -16,6 +20,18 @@ class ProxyReferenceRepositoryTest extends BaseTest { public const TEST_ENTITY_ROLE = Role::class; + public const TEST_ENTITY_LINK = Link::class; + + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + if (Type::hasType('uuid')) { + return; + } + + Type::addType('uuid', UuidType::class); + } public function testReferenceEntry() { @@ -113,6 +129,35 @@ public function testReferenceReconstruction() $this->assertInstanceOf(Proxy::class, $ref); } + public function testReconstructionOfCustomTypedId(): void + { + $em = $this->getMockSqliteEntityManager(); + $referenceRepository = new ProxyReferenceRepository($em); + $listener = new ORMReferenceListener($referenceRepository); + $em->getEventManager()->addEventSubscriber($listener); + + $schemaTool = new SchemaTool($em); + $schemaTool->dropSchema([]); + $schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_LINK)]); + + $link = new TestEntity\Link(new Uuid('5e48c0d7-78c2-44f5-bed0-e7970b2822b8')); + $link->setUrl('http://example.com'); + + $referenceRepository->addReference('home-link', $link); + $em->persist($link); + $em->flush(); + $em->clear(); + + $serializedData = $referenceRepository->serialize(); + $proxyReferenceRepository = new ProxyReferenceRepository($em); + $proxyReferenceRepository->unserialize($serializedData); + + $this->assertInstanceOf( + 'Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid', + $proxyReferenceRepository->getReference('home-link')->getId() + ); + } + public function testReferenceMultipleEntries() { $em = $this->getMockSqliteEntityManager(); diff --git a/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Link.php b/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Link.php new file mode 100644 index 00000000..c49ba11c --- /dev/null +++ b/tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Link.php @@ -0,0 +1,49 @@ +id = $id; + } + + public function getId() + { + return $this->id; + } + + public function getUrl() + { + return $this->url; + } + + public function setUrl($url) + { + $this->url = $url; + } +} diff --git a/tests/Doctrine/Tests/Common/DataFixtures/TestTypes/UuidType.php b/tests/Doctrine/Tests/Common/DataFixtures/TestTypes/UuidType.php new file mode 100644 index 00000000..4b9d83e0 --- /dev/null +++ b/tests/Doctrine/Tests/Common/DataFixtures/TestTypes/UuidType.php @@ -0,0 +1,37 @@ +getVarcharTypeDeclarationSQL($field_declaration); + } + + public function convertToPHPValue($value, AbstractPlatform $platform) + { + return $value === null ? null : new Uuid($value); + } + + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + return $value === null ? null : (string) $value; + } + + public function getName() + { + return self::NAME; + } +} diff --git a/tests/Doctrine/Tests/Common/DataFixtures/TestValueObjects/Uuid.php b/tests/Doctrine/Tests/Common/DataFixtures/TestValueObjects/Uuid.php new file mode 100644 index 00000000..637bcbd9 --- /dev/null +++ b/tests/Doctrine/Tests/Common/DataFixtures/TestValueObjects/Uuid.php @@ -0,0 +1,49 @@ +id = $id; + } + + public function getId() + { + return $this->id; + } + + public function jsonSerialize() + { + return $this->id; + } + + public function serialize() + { + return $this->id; + } + + public function unserialize($serialized) + { + $this->id = $serialized; + } + + public function __toString() + { + return $this->id; + } + + public static function unknown() + { + return new self('00000000-0000-0000-C000-000000000046'); + } +}