MaterializedPathORMTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. /**
  6. * These are tests for Tree behavior
  7. *
  8. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class MaterializedPathORMTest extends BaseTestCaseORM
  14. {
  15. const CATEGORY = "Tree\\Fixture\\MPCategory";
  16. protected $config;
  17. protected $listener;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $this->listener = new TreeListener;
  22. $evm = new EventManager;
  23. $evm->addEventSubscriber($this->listener);
  24. $this->getMockSqliteEntityManager($evm);
  25. $meta = $this->em->getClassMetadata(self::CATEGORY);
  26. $this->config = $this->listener->getConfiguration($this->em, $meta->name);
  27. }
  28. /**
  29. * @test
  30. */
  31. function insertUpdateAndRemove()
  32. {
  33. // Insert
  34. $category = $this->createCategory();
  35. $category->setTitle('1');
  36. $category2 = $this->createCategory();
  37. $category2->setTitle('2');
  38. $category3 = $this->createCategory();
  39. $category3->setTitle('3');
  40. $category4 = $this->createCategory();
  41. $category4->setTitle('4');
  42. $category2->setParent($category);
  43. $category3->setParent($category2);
  44. $this->em->persist($category4);
  45. $this->em->persist($category3);
  46. $this->em->persist($category2);
  47. $this->em->persist($category);
  48. $this->em->flush();
  49. $this->em->refresh($category);
  50. $this->em->refresh($category2);
  51. $this->em->refresh($category3);
  52. $this->em->refresh($category4);
  53. $this->assertEquals($this->generatePath(array('1' => $category->getId())), $category->getPath());
  54. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId())), $category2->getPath());
  55. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId(), '3' => $category3->getId())), $category3->getPath());
  56. $this->assertEquals($this->generatePath(array('4' => $category4->getId())), $category4->getPath());
  57. $this->assertEquals(1, $category->getLevel());
  58. $this->assertEquals(2, $category2->getLevel());
  59. $this->assertEquals(3, $category3->getLevel());
  60. $this->assertEquals(1, $category4->getLevel());
  61. // Update
  62. $category2->setParent(null);
  63. $this->em->persist($category2);
  64. $this->em->flush();
  65. $this->em->refresh($category);
  66. $this->em->refresh($category2);
  67. $this->em->refresh($category3);
  68. $this->assertEquals($this->generatePath(array('1' => $category->getId())), $category->getPath());
  69. $this->assertEquals($this->generatePath(array('2' => $category2->getId())), $category2->getPath());
  70. $this->assertEquals($this->generatePath(array('2' => $category2->getId(), '3' => $category3->getId())), $category3->getPath());
  71. $this->assertEquals(1, $category->getLevel());
  72. $this->assertEquals(1, $category2->getLevel());
  73. $this->assertEquals(2, $category3->getLevel());
  74. $this->assertEquals(1, $category4->getLevel());
  75. // Remove
  76. $this->em->remove($category);
  77. $this->em->remove($category2);
  78. $this->em->flush();
  79. $result = $this->em->createQueryBuilder()->select('c')->from(self::CATEGORY, 'c')->getQuery()->execute();
  80. $firstResult = $result[0];
  81. $this->assertCount(1, $result);
  82. $this->assertEquals('4', $firstResult->getTitle());
  83. $this->assertEquals(1, $firstResult->getLevel());
  84. }
  85. /**
  86. * @test
  87. */
  88. public function useOfSeparatorInPathSourceShouldThrowAnException()
  89. {
  90. $this->setExpectedException('Gedmo\Exception\RuntimeException');
  91. $category = $this->createCategory();
  92. $category->setTitle('1'.$this->config['path_separator']);
  93. $this->em->persist($category);
  94. $this->em->flush();
  95. }
  96. public function createCategory()
  97. {
  98. $class = self::CATEGORY;
  99. return new $class;
  100. }
  101. protected function getUsedEntityFixtures()
  102. {
  103. return array(
  104. self::CATEGORY
  105. );
  106. }
  107. public function generatePath(array $sources)
  108. {
  109. $path = '';
  110. foreach ($sources as $p => $id) {
  111. $path .= $p.'-'.$id.$this->config['path_separator'];
  112. }
  113. return $path;
  114. }
  115. }