Skip to content

Commit

Permalink
Fix custom typed ID reconstruction
Browse files Browse the repository at this point in the history
Fixes #254
  • Loading branch information
JustBlackBird committed Jan 7, 2020
1 parent 608a35a commit 3686a2b
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +58,7 @@ public function serialize()
$simpleReferences[$name] = [$className, $this->getIdentifier($reference, $unitOfWork)];
}

return json_encode([
return serialize([
'references' => $simpleReferences,
'identities' => $this->getIdentities(),
]);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@

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.
*/
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()
{
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid;

/**
* @ORM\Entity
*/
class Link
{
/**
* @ORM\Id
* @ORM\Column(type="uuid")
*
* @var Uuid
*/
private $id;

/**
* @ORM\Column(length=150)
*
* @var string
*/
private $url;

public function __construct(Uuid $id)
{
$this->id = $id;
}

public function getId()
{
return $this->id;
}

public function getUrl()
{
return $this->url;
}

public function setUrl($url)
{
$this->url = $url;
}
}
37 changes: 37 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestTypes/UuidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestTypes;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid;

class UuidType extends Type
{
public const NAME = 'uuid';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$field_declaration['length'] = 36;
$field_declaration['fixed'] = true;

return $platform->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;
}
}
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestValueObjects/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestValueObjects;

use JsonSerializable;
use Serializable;

class Uuid implements JsonSerializable, Serializable
{
/** @var string $id */
private $id;

public function __construct($id)
{
$this->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');
}
}

0 comments on commit 3686a2b

Please sign in to comment.