MaterializedPathORMTest.php 4.5KB

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