MultInheritanceWithJoinedTableTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. /**
  7. * These are tests for Tree behavior
  8. * Based on reported github issue #12
  9. * JOINED table inheritance mapping bug on Tree;
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MultiInheritanceWithJoinedTableTest extends BaseTestCaseORM
  16. {
  17. const USER = "Tree\\Fixture\\User";
  18. const GROUP = "Tree\\Fixture\\UserGroup";
  19. const ROLE = "Tree\\Fixture\\Role";
  20. const USERLDAP = "Tree\\Fixture\\UserLDAP";
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $this->tree = new TreeListener;
  26. $evm->addEventSubscriber($this->tree);
  27. $this->getMockSqliteEntityManager($evm);
  28. $this->populate();
  29. }
  30. /**
  31. * @test
  32. */
  33. public function shouldHandleMultilevelInheritance()
  34. {
  35. $admins = $this->em->getRepository(self::GROUP)->findOneByName('Admins');
  36. $adminRight = $admins->getRight();
  37. $userLdap = new \Tree\Fixture\UserLDAP('testname');
  38. $userLdap->init();
  39. $userLdap->setParent($admins);
  40. $this->em->persist($userLdap);
  41. $this->em->flush();
  42. $this->em->clear();
  43. $admins = $this->em->getRepository(self::GROUP)->findOneByName('Admins');
  44. self::assertNotEquals($adminRight, $admins->getRight());
  45. }
  46. /**
  47. * @test
  48. */
  49. public function shouldBeAbleToPopulateTree()
  50. {
  51. $admins = $this->em->getRepository(self::GROUP)->findOneByName('Admins');
  52. $user3 = new \Tree\Fixture\User('user3@test.com', 'secret');
  53. $user3->init();
  54. $user3->setParent($admins);
  55. $this->em->persist($user3);
  56. $this->em->flush();
  57. $this->em->clear();
  58. // run tree consistence checks
  59. $everyBody = $this->em->getRepository(self::GROUP)->findOneByName('Everybody');
  60. $this->assertEquals(1, $everyBody->getLeft());
  61. $this->assertEquals(14, $everyBody->getRight());
  62. $this->assertEquals(0, $everyBody->getLevel());
  63. $admins = $this->em->getRepository(self::GROUP)->findOneByName('Admins');
  64. $this->assertEquals(2, $admins->getLeft());
  65. $this->assertEquals(7, $admins->getRight());
  66. $this->assertEquals(1, $admins->getLevel());
  67. $visitors = $this->em->getRepository(self::GROUP)->findOneByName('Visitors');
  68. $this->assertEquals(8, $visitors->getLeft());
  69. $this->assertEquals(13, $visitors->getRight());
  70. $this->assertEquals(1, $visitors->getLevel());
  71. $user0 = $this->em->getRepository(self::USER)->findOneByEmail('user0@test.com');
  72. $this->assertEquals(3, $user0->getLeft());
  73. $this->assertEquals(4, $user0->getRight());
  74. $this->assertEquals(2, $user0->getLevel());
  75. $user1 = $this->em->getRepository(self::USER)->findOneByEmail('user1@test.com');
  76. $this->assertEquals(9, $user1->getLeft());
  77. $this->assertEquals(10, $user1->getRight());
  78. $this->assertEquals(2, $user1->getLevel());
  79. $user2 = $this->em->getRepository(self::USER)->findOneByEmail('user2@test.com');
  80. $this->assertEquals(11, $user2->getLeft());
  81. $this->assertEquals(12, $user2->getRight());
  82. $this->assertEquals(2, $user2->getLevel());
  83. $user3 = $this->em->getRepository(self::USER)->findOneByEmail('user3@test.com');
  84. $this->assertEquals(5, $user3->getLeft());
  85. $this->assertEquals(6, $user3->getRight());
  86. $this->assertEquals(2, $user3->getLevel());
  87. }
  88. protected function getUsedEntityFixtures()
  89. {
  90. return array(
  91. self::USER,
  92. self::GROUP,
  93. self::ROLE,
  94. self::USERLDAP
  95. );
  96. }
  97. private function populate()
  98. {
  99. $everyBody = new \Tree\Fixture\UserGroup('Everybody');
  100. $admins = new \Tree\Fixture\UserGroup('Admins');
  101. $admins->setParent($everyBody);
  102. $visitors = new \Tree\Fixture\UserGroup('Visitors');
  103. $visitors->setParent($everyBody);
  104. $user0 = new \Tree\Fixture\User('user0@test.com', 'secret');
  105. $user0->init();
  106. $user0->setParent($admins);
  107. $user1 = new \Tree\Fixture\User('user1@test.com', 'secret');
  108. $user1->init();
  109. $user1->setParent($visitors);
  110. $user2 = new \Tree\Fixture\User('user2@test.com', 'secret');
  111. $user2->init();
  112. $user2->setParent($visitors);
  113. $this->em->persist($everyBody);
  114. $this->em->persist($admins);
  115. $this->em->persist($visitors);
  116. $this->em->persist($user0);
  117. $this->em->persist($user1);
  118. $this->em->persist($user2);
  119. $this->em->flush();
  120. $this->em->clear();
  121. }
  122. }