ReferenceIntegrityDocumentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Gedmo\ReferenceIntegrity;
  3. use Doctrine\Common\EventManager;
  4. use Gedmo\ReferenceIntegrity\ReferenceIntegrityListener;
  5. use Tool\BaseTestCaseMongoODM;
  6. /**
  7. * These are tests for the ReferenceIntegrity extension
  8. *
  9. * @author Evert Harmeling <evert.harmeling@freshheads.com>
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class ReferenceIntegrityDocumentTest extends BaseTestCaseMongoODM
  13. {
  14. const TYPE_ONE_NULLIFY_CLASS = 'ReferenceIntegrity\Fixture\Document\OneNullify\Type';
  15. const ARTICLE_ONE_NULLIFY_CLASS = 'ReferenceIntegrity\Fixture\Document\OneNullify\Article';
  16. const TYPE_MANY_NULLIFY_CLASS = 'ReferenceIntegrity\Fixture\Document\ManyNullify\Type';
  17. const ARTICLE_MANY_NULLIFY_CLASS = 'ReferenceIntegrity\Fixture\Document\ManyNullify\Article';
  18. const TYPE_ONE_RESTRICT_CLASS = 'ReferenceIntegrity\Fixture\Document\OneRestrict\Type';
  19. const ARTICLE_ONE_RESTRICT_CLASS = 'ReferenceIntegrity\Fixture\Document\OneRestrict\Article';
  20. const TYPE_MANY_RESTRICT_CLASS = 'ReferenceIntegrity\Fixture\Document\ManyRestrict\Type';
  21. const ARTICLE_MANY_RESTRICT_CLASS = 'ReferenceIntegrity\Fixture\Document\ManyRestrict\Article';
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $evm = new EventManager();
  26. $evm->addEventSubscriber(new ReferenceIntegrityListener());
  27. $this->dm = $this->getMockDocumentManager($evm, $this->getMockAnnotatedConfig());
  28. $this->populateOneNullify();
  29. $this->populateManyNullify();
  30. $this->populateOneRestrict();
  31. $this->populateManyRestrict();
  32. }
  33. public function testOneNullify()
  34. {
  35. $type = $this->dm->getRepository(self::TYPE_ONE_NULLIFY_CLASS)
  36. ->findOneByTitle('One Nullify Type');
  37. $this->assertFalse(is_null($type));
  38. $this->assertTrue(is_object($type));
  39. $this->dm->remove($type);
  40. $this->dm->flush();
  41. $type = $this->dm->getRepository(self::TYPE_ONE_NULLIFY_CLASS)
  42. ->findOneByTitle('One Nullify Type');
  43. $this->assertNull($type);
  44. $article = $this->dm->getRepository(self::ARTICLE_ONE_NULLIFY_CLASS)
  45. ->findOneByTitle('One Nullify Article');
  46. $this->assertNull($article->getType());
  47. $this->dm->clear();
  48. }
  49. public function testManyNullify()
  50. {
  51. $type = $this->dm->getRepository(self::TYPE_MANY_NULLIFY_CLASS)
  52. ->findOneByTitle('Many Nullify Type');
  53. $this->assertFalse(is_null($type));
  54. $this->assertTrue(is_object($type));
  55. $this->dm->remove($type);
  56. $this->dm->flush();
  57. $type = $this->dm->getRepository(self::TYPE_MANY_NULLIFY_CLASS)
  58. ->findOneByTitle('Many Nullify Type');
  59. $this->assertNull($type);
  60. $article = $this->dm->getRepository(self::ARTICLE_MANY_NULLIFY_CLASS)
  61. ->findOneByTitle('Many Nullify Article');
  62. $this->assertNull($article->getType());
  63. $this->dm->clear();
  64. }
  65. /**
  66. * @test
  67. * @expectedException Gedmo\Exception\ReferenceIntegrityStrictException
  68. */
  69. public function testOneRestrict()
  70. {
  71. $type = $this->dm->getRepository(self::TYPE_ONE_RESTRICT_CLASS)
  72. ->findOneByTitle('One Restrict Type');
  73. $this->assertFalse(is_null($type));
  74. $this->assertTrue(is_object($type));
  75. $this->dm->remove($type);
  76. $this->dm->flush();
  77. }
  78. /**
  79. * @test
  80. * @expectedException Gedmo\Exception\ReferenceIntegrityStrictException
  81. */
  82. public function testManyRestrict()
  83. {
  84. $type = $this->dm->getRepository(self::TYPE_MANY_RESTRICT_CLASS)
  85. ->findOneByTitle('Many Restrict Type');
  86. $this->assertFalse(is_null($type));
  87. $this->assertTrue(is_object($type));
  88. $this->dm->remove($type);
  89. $this->dm->flush();
  90. }
  91. private function populateOneNullify()
  92. {
  93. $typeClass = self::TYPE_ONE_NULLIFY_CLASS;
  94. $type = new $typeClass();
  95. $type->setTitle('One Nullify Type');
  96. $articleClass = self::ARTICLE_ONE_NULLIFY_CLASS;
  97. $article = new $articleClass();
  98. $article->setTitle('One Nullify Article');
  99. $article->setType($type);
  100. $this->dm->persist($article);
  101. $this->dm->persist($type);
  102. $this->dm->flush();
  103. $this->dm->clear();
  104. }
  105. private function populateManyNullify()
  106. {
  107. $typeClass = self::TYPE_MANY_NULLIFY_CLASS;
  108. $type = new $typeClass();
  109. $type->setTitle('Many Nullify Type');
  110. $articleClass = self::ARTICLE_MANY_NULLIFY_CLASS;
  111. $article = new $articleClass();
  112. $article->setTitle('Many Nullify Article');
  113. $article->setType($type);
  114. $this->dm->persist($article);
  115. $this->dm->persist($type);
  116. $this->dm->flush();
  117. $this->dm->clear();
  118. }
  119. private function populateOneRestrict()
  120. {
  121. $typeClass = self::TYPE_ONE_RESTRICT_CLASS;
  122. $type = new $typeClass();
  123. $type->setTitle('One Restrict Type');
  124. $articleClass = self::ARTICLE_ONE_RESTRICT_CLASS;
  125. $article = new $articleClass();
  126. $article->setTitle('One Restrict Article');
  127. $article->setType($type);
  128. $this->dm->persist($article);
  129. $this->dm->persist($type);
  130. $this->dm->flush();
  131. $this->dm->clear();
  132. }
  133. private function populateManyRestrict()
  134. {
  135. $typeClass = self::TYPE_MANY_RESTRICT_CLASS;
  136. $type = new $typeClass();
  137. $type->setTitle('Many Restrict Type');
  138. $articleClass = self::ARTICLE_MANY_RESTRICT_CLASS;
  139. $article = new $articleClass();
  140. $article->setTitle('Many Restrict Article');
  141. $article->setType($type);
  142. $this->dm->persist($article);
  143. $this->dm->persist($type);
  144. $this->dm->flush();
  145. $this->dm->clear();
  146. }
  147. }