1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/OneToOneOrphanRemovalTest.php

94 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\CMS\CmsUser,
Doctrine\Tests\Models\CMS\CmsEmail,
Doctrine\Tests\Models\CMS\CmsAddress,
Doctrine\Tests\Models\CMS\CmsPhonenumber;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests a bidirectional one-to-one association mapping with orphan removal.
*/
class OneToOneOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
$this->useModelSet('cms');
2011-12-20 01:56:19 +04:00
parent::setUp();
}
2011-12-20 01:56:19 +04:00
public function testOrphanRemoval()
{
$user = new CmsUser;
$user->status = 'dev';
$user->username = 'romanb';
$user->name = 'Roman B.';
2011-12-20 01:56:19 +04:00
$address = new CmsAddress;
$address->country = 'de';
$address->zip = 1234;
$address->city = 'Berlin';
2011-12-20 01:56:19 +04:00
$user->setAddress($address);
2011-12-20 01:56:19 +04:00
$this->_em->persist($user);
$this->_em->flush();
2011-12-20 01:56:19 +04:00
$userId = $user->getId();
2011-12-20 01:56:19 +04:00
$this->_em->clear();
2011-12-20 01:56:19 +04:00
$userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $userId);
2011-12-20 01:56:19 +04:00
$this->_em->remove($userProxy);
$this->_em->flush();
$this->_em->clear();
2011-12-20 01:56:19 +04:00
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
$this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
2011-12-20 01:56:19 +04:00
$query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a');
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
$this->assertEquals(0, count($result), 'CmsAddress should be removed by orphanRemoval');
}
2011-12-20 01:56:19 +04:00
public function testOrphanRemovalWhenUnlink()
{
$user = new CmsUser;
$user->status = 'dev';
$user->username = 'beberlei';
$user->name = 'Bejamin Eberlei';
2011-12-20 01:56:19 +04:00
$email = new CmsEmail;
$email->email = 'beberlei@domain.com';
2011-12-20 01:56:19 +04:00
$user->setEmail($email);
2011-12-20 01:56:19 +04:00
$this->_em->persist($user);
$this->_em->flush();
2011-12-20 01:56:19 +04:00
$userId = $user->getId();
2011-12-20 01:56:19 +04:00
$this->_em->clear();
2011-12-20 01:56:19 +04:00
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId);
2011-12-20 01:56:19 +04:00
$user->setEmail(null);
2011-12-20 01:56:19 +04:00
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
2011-12-20 01:56:19 +04:00
$query = $this->_em->createQuery('SELECT e FROM Doctrine\Tests\Models\CMS\CmsEmail e');
$result = $query->getResult();
2011-12-20 01:56:19 +04:00
$this->assertEquals(0, count($result), 'CmsEmail should be removed by orphanRemoval');
}
}