2011-09-06 01:58:16 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
2011-09-06 01:58:16 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a bidirectional one-to-many association mapping with orphan removal.
|
|
|
|
*/
|
2016-05-11 02:41:26 +07:00
|
|
|
class OneToManyOrphanRemovalTest extends OrmFunctionalTestCase
|
2011-09-06 01:58:16 -03:00
|
|
|
{
|
2011-11-18 15:44:06 +01:00
|
|
|
protected $userId;
|
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('cms');
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
parent::setUp();
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->status = 'dev';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->name = 'Roman B.';
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2015-06-11 22:12:55 +02:00
|
|
|
$phone1 = new CmsPhonenumber;
|
|
|
|
$phone1->phonenumber = '123456';
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2015-06-11 22:12:55 +02:00
|
|
|
$phone2 = new CmsPhonenumber;
|
|
|
|
$phone2->phonenumber = '234567';
|
|
|
|
|
|
|
|
$user->addPhonenumber($phone1);
|
|
|
|
$user->addPhonenumber($phone2);
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
2011-11-18 15:44:06 +01:00
|
|
|
|
|
|
|
$this->userId = $user->getId();
|
2011-09-06 01:58:16 -03:00
|
|
|
$this->_em->clear();
|
2011-11-18 15:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOrphanRemoval()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$userProxy = $this->_em->getReference(CmsUser::class, $this->userId);
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$this->_em->remove($userProxy);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
|
|
|
|
$result = $query->getResult();
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
|
2011-11-18 15:44:06 +01:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
|
|
|
|
}
|
|
|
|
|
2015-06-11 22:12:55 +02:00
|
|
|
/**
|
|
|
|
* @group DDC-3382
|
|
|
|
*/
|
|
|
|
public function testOrphanRemovalRemoveFromCollection()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->find(CmsUser::class, $this->userId);
|
2015-06-11 22:12:55 +02:00
|
|
|
|
|
|
|
$phonenumber = $user->getPhonenumbers()->remove(0);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-3382
|
|
|
|
*/
|
|
|
|
public function testOrphanRemovalClearCollectionAndReAdd()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->find(CmsUser::class, $this->userId);
|
2015-06-11 22:12:55 +02:00
|
|
|
|
|
|
|
$phone1 = $user->getPhonenumbers()->first();
|
|
|
|
|
|
|
|
$user->getPhonenumbers()->clear();
|
|
|
|
$user->addPhonenumber($phone1);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
|
|
|
|
}
|
|
|
|
|
2015-11-25 04:20:35 +00:00
|
|
|
/**
|
|
|
|
* @group DDC-2524
|
|
|
|
*/
|
|
|
|
public function testOrphanRemovalClearCollectionAndAddNew()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->find(CmsUser::class, $this->userId);
|
2015-11-25 04:20:35 +00:00
|
|
|
$newPhone = new CmsPhonenumber();
|
|
|
|
|
|
|
|
$newPhone->phonenumber = '654321';
|
|
|
|
|
|
|
|
$user->getPhonenumbers()->clear();
|
|
|
|
$user->addPhonenumber($newPhone);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($result), 'Old CmsPhonenumbers should be removed by orphanRemoval and new one added');
|
|
|
|
}
|
|
|
|
|
2011-11-18 15:44:06 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1496
|
|
|
|
*/
|
|
|
|
public function testOrphanRemovalUnitializedCollection()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$user = $this->_em->find(CmsUser::class, $this->userId);
|
2011-11-18 15:44:06 +01:00
|
|
|
|
|
|
|
$user->phonenumbers->clear();
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
2011-11-18 15:44:06 +01:00
|
|
|
|
2011-09-06 01:58:16 -03:00
|
|
|
$this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
|
|
|
|
}
|
2014-04-07 14:43:25 +02:00
|
|
|
}
|