HardRelationTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Gedmo\SoftDeleteable;
  3. use Tool\BaseTestCaseORM;
  4. use Doctrine\Common\EventManager;
  5. use SoftDeleteable\Fixture\Entity\Person;
  6. use SoftDeleteable\Fixture\Entity\Address;
  7. use Gedmo\SoftDeleteable\SoftDeleteableListener;
  8. class HardRelationTest extends BaseTestCaseORM
  9. {
  10. private $softDeleteableListener;
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. $evm = new EventManager();
  15. $evm->addEventSubscriber($this->softDeleteableListener = new SoftDeleteableListener);
  16. $this->getMockSqliteEntityManager($evm);
  17. $this->em->getConfiguration()->addFilter('softdelete', 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
  18. $this->em->getFilters()->enable('softdelete');
  19. }
  20. /**
  21. * @test
  22. */
  23. function shouldCascadeSoftdeleteForHardRelations()
  24. {
  25. $address = new Address;
  26. $address->setStreet('13 Boulangerie, 404');
  27. $person = new Person;
  28. $person->setName('Gedi');
  29. $person->setAddress($address);
  30. $this->em->persist($address);
  31. $this->em->persist($person);
  32. $this->em->flush();
  33. // softdelete a hard relation
  34. $this->em->remove($address);
  35. $this->em->flush();
  36. $this->em->clear();
  37. $person = $this->em->getRepository('SoftDeleteable\Fixture\Entity\Person')->findOneById($person->getId());
  38. $this->assertNull($person, "Softdelete should cascade to hard relation entity");
  39. }
  40. /**
  41. * @test
  42. */
  43. function shouldCascadeToInversedRelationAsWell()
  44. {
  45. $address = new Address;
  46. $address->setStreet('13 Boulangerie, 404');
  47. $person = new Person;
  48. $person->setName('Gedi');
  49. $person->setAddress($address);
  50. $this->em->persist($address);
  51. $this->em->persist($person);
  52. $this->em->flush();
  53. // softdelete a hard relation
  54. $this->em->remove($person);
  55. $this->em->flush();
  56. $this->em->clear();
  57. $address = $this->em->getRepository('SoftDeleteable\Fixture\Entity\Address')->findOneById($address->getId());
  58. $this->assertNull($address, "Softdelete should cascade to hard relation entity");
  59. }
  60. /**
  61. * @test
  62. */
  63. function shouldHandleTimeAwareSoftDeleteable()
  64. {
  65. $address = new Address;
  66. $address->setStreet('13 Boulangerie, 404');
  67. $person = new Person;
  68. $person->setName('Gedi');
  69. $person->setDeletedAt(new \DateTime(date('Y-m-d H:i:s', time() + 15 * 3600))); // in an hour
  70. $person->setAddress($address);
  71. $this->em->persist($address);
  72. $this->em->persist($person);
  73. $this->em->flush();
  74. $this->em->clear();
  75. $person = $this->em->getRepository('SoftDeleteable\Fixture\Entity\Person')->findOneById($person->getId());
  76. $this->assertNotNull($person, "Should not be softdeleted");
  77. $person->setDeletedAt(new \DateTime(date('Y-m-d H:i:s', time() - 15 * 3600))); // in an hour
  78. $this->em->persist($person);
  79. $this->em->flush();
  80. $this->em->clear();
  81. $person = $this->em->getRepository('SoftDeleteable\Fixture\Entity\Person')->findOneById($person->getId());
  82. $this->assertNull($person, "Should be softdeleted");
  83. }
  84. protected function getUsedEntityFixtures()
  85. {
  86. return array(
  87. 'SoftDeleteable\Fixture\Entity\Person',
  88. 'SoftDeleteable\Fixture\Entity\Address',
  89. );
  90. }
  91. }