OneToOneOrphanRemovalTest.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\CMS\CmsUser,
  4. Doctrine\Tests\Models\CMS\CmsAddress,
  5. Doctrine\Tests\Models\CMS\CmsPhonenumber;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a bidirectional one-to-one association mapping with orphan removal.
  9. */
  10. class OneToOneOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected function setUp()
  13. {
  14. $this->useModelSet('cms');
  15. parent::setUp();
  16. }
  17. public function testOrphanRemoval()
  18. {
  19. $user = new CmsUser;
  20. $user->status = 'dev';
  21. $user->username = 'romanb';
  22. $user->name = 'Roman B.';
  23. $address = new CmsAddress;
  24. $address->country = 'de';
  25. $address->zip = 1234;
  26. $address->city = 'Berlin';
  27. $user->setAddress($address);
  28. $this->_em->persist($user);
  29. $this->_em->flush();
  30. $userId = $user->getId();
  31. $this->_em->clear();
  32. $userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $userId);
  33. $this->_em->remove($userProxy);
  34. $this->_em->flush();
  35. $this->_em->clear();
  36. $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
  37. $result = $query->getResult();
  38. $this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
  39. $query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a');
  40. $result = $query->getResult();
  41. $this->assertEquals(0, count($result), 'CmsAddress should be removed by orphanRemoval');
  42. }
  43. }