Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/5d4a9f1777bc45c62254 to your computer and use it in GitHub Desktop.
Save doctrinebot/5d4a9f1777bc45c62254 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-440 - https://github.com/doctrine/doctrine2/issues/4941
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
require_once __DIR__ . '/../../../TestInit.php';
class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
}
/**
* @group DDC-440
*/
public function testProxyOriginalEntityData()
{
$p = new ECommerceProduct;
$p->setName('Product');
$s = new EcommerceShipping;
$s->setDays(5);
$p->setShipping($s);
$this->_em->persist($p);
$this->_em->persist($s);
$this->_em->flush();
$id = $p->getId();
echo spl_object_hash($s) . "\n";
$this->_em->detach($s);
$this->_em->detach($p);
$this->_em->clear();
unset($s);
unset($p);
$this->_em->getConnection()->getConfiguration()->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger);
$product = $this->_em->find('Doctrine\\Tests\\Models\\ECommerce\\ECommerceProduct', $id);
$product->setName('updated');
$shipping = $product->getShipping();
echo \get_class($shipping) ."\n";
echo spl_object_hash($shipping) . "\n";
$shipping->setDays(5);
$this->_em->persist($product);
$this->_em->persist($shipping);
$this->_em->flush();
echo "ASDF\n";
}
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
require_once __DIR__ . '/../../../TestInit.php';
class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}
public function testOriginalEntityDataForProxies()
{
$user = new CmsUser();
$user->name = "Benjamin Eberlei";
$user->username = 'beberlei';
$user->status = 'active';
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
$userId = $user->id;
/* @var $proxyUser CmsUser */
$proxyUser = $this->_em->getReference('Doctrine\Tests\Models\Cms\CmsUser', $userId);
$this->assertEquals(array(), $this->_em->getUnitOfWork()->getOriginalEntityData($proxyUser) );
$proxyUser->getUsername();
$originalData = $this->_em->getUnitOfWork()->getOriginalEntityData($proxyUser);
$this->assertEquals('beberlei', $originalData['username']);
$this->assertEquals('active', $originalData['status']);
}
public function testOriginalEntityDataForRelatedEntities()
{
$user = new CmsUser();
$user->name = "Benjamin Eberlei";
$user->username = 'beberlei';
$user->status = 'active';
$phone = new CmsPhonenumber();
$phone->phonenumber = "123456";
$phone->setUser($user);
$this->_em->persist($phone);
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
$userId = $user->id;
/* @var $proxyUser CmsUser */
$user = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $userId);
$proxyLoadedPhones = $user->getPhonenumbers();
$this->assertEquals(1, count($proxyLoadedPhones));
$proxyLoadedPhone = $proxyLoadedPhones[0];
$originalData = $this->_em->getUnitOfWork()->getOriginalEntityData($proxyLoadedPhone);
$this->assertEquals('123456', $proxyLoadedPhone->phonenumber);
$this->assertTrue(count($originalData) > 0);
$this->assertEquals('123456', $originalData['phonenumber']);
}
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Phone'),
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Client')
));
} catch (\Exception $e) {
// Swallow all exceptions. We do not test the schema tool here.
}
}
/**
* @group DDC-440
*/
public function testOriginalEntityDataEmptyWhenProxyLoadedFromTwoAssociations()
{
/* The key of the problem is that the first phone is fetched via two association, main_phone and phones.
*
* You will notice that the original_entity_datas are not loaded for the first phone. (They are for the second)
*
* In the Client entity definition, if you define the main_phone relation after the phones relation, both assertions pass.
* (for the sake or this test, I defined the main_phone relation before the phones relation)
*
*/
//Initialize some data
$client = new Client;
$client->setName('Client1');
$phone = new Phone;
$phone->setNumber('418 111-1111');
$phone->setClient($client);
$phone2 = new Phone;
$phone2->setNumber('418 222-2222');
$phone2->setClient($client);
$client->setMainPhone($phone);
$this->_em->persist($client);
$this->_em->flush();
$id = $client->getId();
$this->_em->clear();
$uw = $this->_em->getUnitOfWork();
$found_client = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\Client', $id);
$found_client_phones = $found_client->getPhones();
//Test the first phone. The assertion actually fail because original entity data is not set properly.
$p1 = $found_client_phones[0];
$this->assertType('Doctrine\Tests\ORM\Functional\Ticket\Phone', $p1);
$original_data = $uw->getOriginalEntityData($p1);
$this->assertEquals($phone->getNumber(), $original_data['number']);
//If you comment out previous test, this one should pass
$p2 = $found_client_phones[1];
$this->assertType('Doctrine\Tests\ORM\Functional\Ticket\Phone', $p2);
$original_data = $uw->getOriginalEntityData($p2);
$this->assertEquals($phone2->getNumber(), $original_data['number']);
}
}
/**
* @Entity
* @Table(name="phone")
*/
class Phone {
/**
* @Column(name="id", type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToOne(targetEntity="Client",inversedBy="phones")
* @JoinColumns({
* @JoinColumn(name="client_id", referencedColumnName="id")
* })
*/
protected $client;
/**
* @Column(name="number", type="string")
*/
protected $number;
public function setNumber($value){
$this->number = $value;
}
public function getNumber(){
return $this->number;
}
public function setClient(Client $value, $update_inverse=true)
{
$this->client = $value;
if($update_inverse){
$value->addPhone($this);
}
}
public function getClient()
{
return $this->client;
}
public function getId()
{
return $this->id;
}
public function setId($value){
$this->id = $value;
}
}
/**
* @Entity
* @Table(name="client")
*/
class Client {
/**
* @Column(name="id", type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @OneToOne(targetEntity="Phone", fetch="EAGER")
* @JoinColumns({
* @JoinColumn(name="main_phone_id", referencedColumnName="id",onDelete="SET NULL")
* })
*/
protected $main_phone;
/**
* @OneToMany(targetEntity="Phone", mappedBy="client", cascade={"persist", "remove"}, fetch="EAGER")
*/
protected $phones;
/**
* @Column(name="name", type="string")
*/
protected $name;
public function __construct(){
}
public function setName($value){
$this->name = $value;
}
public function getName(){
return $this->name;
}
public function addPhone(Phone $value)
{
$this->phones[] = $value;
$value->setClient($this, false);
}
public function getPhones()
{
return $this->phones;
}
public function setMainPhone(Phone $value){
$this->main_phone = $value;
}
public function getMainPhone(){
return $this->main_phone;
}
public function getId()
{
return $this->id;
}
public function setId($value){
$this->id = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment