public
Last active

Test for Doctrine Issue DDC-1734

  • Download Gist
DDC1734Article.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
 
namespace Doctrine\Tests\Models\DDC1734;
 
/**
* @Entity
*/
class DDC1734Article
{
 
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
protected $id;
 
/**
* @Column(type="string")
*/
protected $name;
 
public function __construct($name)
{
$this->name = $name;
}
 
public function getId()
{
return $this->id;
}
 
public function getName()
{
return $this->name;
}
 
}
DDC1734Test.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
<?php
 
namespace Doctrine\Tests\ORM\Functional\Ticket;
 
use Doctrine\Tests\Models\DDC1734\DDC1734Article;
 
require_once __DIR__ . '/../../../TestInit.php';
 
class DDC1734Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
 
protected function setUp()
{
$this->useModelSet('ddc1734');
parent::setUp();
}
 
/**
* This test is DDC-1734 minus the serialization, i.e. it works
* @group DDC-1734
*/
public function testMergeWorksOnNonSerializedProxies()
{
$article = new DDC1734Article("Foo");
$this->_em->persist($article);
$this->_em->flush();
// Get a proxy of the entity
$this->_em->clear();
$proxy = $this->getProxy($article);
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $proxy);
$this->assertFalse($proxy->__isInitialized__);
// Detach
$this->_em->detach($proxy);
$this->_em->clear();
// Merge
$proxy = $this->_em->merge($proxy);
$this->assertEquals("Foo", $proxy->getName(), "The entity is broken");
}
 
/**
* This test reproduces DDC-1734 which is:
* - A non-initialized proxy is detached and serialized (the identifier of the proxy is *not* serialized)
* - the object is deserialized and merged (to turn into an entity)
* - the entity is broken because it has no identifier and no field defined
* @group DDC-1734
*/
public function testMergeWorksOnSerializedProxies()
{
$article = new DDC1734Article("Foo");
$this->_em->persist($article);
$this->_em->flush();
// Get a proxy of the entity
$this->_em->clear();
$proxy = $this->getProxy($article);
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $proxy);
$this->assertFalse($proxy->__isInitialized__);
// Detach and serialize
$this->_em->detach($proxy);
$serializedProxy = serialize($proxy);
$this->_em->clear();
// Unserialize and merge
/** @var $unserializedProxy DDC1734Article */
$unserializedProxy = unserialize($serializedProxy);
// Merge
$unserializedProxy = $this->_em->merge($unserializedProxy);
$this->assertEquals("Foo", $unserializedProxy->getName(), "The entity is broken");
}
 
private function getProxy($object)
{
$metadataFactory = $this->_em->getMetadataFactory();
$identifier = $metadataFactory->getMetadataFor(get_class($object))->getIdentifierValues($object);
$proxyFactory = $this->_em->getProxyFactory();
return $proxyFactory->getProxy(get_class($object), $identifier);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.