TreeSlugHandlerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\Handler\TreeSlug;
  6. use Gedmo\Tree\TreeListener;
  7. /**
  8. * These are tests for Sluggable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TreeSlugHandlerTest extends BaseTestCaseORM
  15. {
  16. const TARGET = "Sluggable\\Fixture\\Handler\\TreeSlug";
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $evm = new EventManager;
  21. $evm->addEventSubscriber(new SluggableListener);
  22. $evm->addEventSubscriber(new TreeListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. }
  25. public function testSlugGeneration()
  26. {
  27. $this->populate();
  28. $repo = $this->em->getRepository(self::TARGET);
  29. $food = $repo->findOneByTitle('Food');
  30. $this->assertEquals('food', $food->getSlug());
  31. $fruits = $repo->findOneByTitle('Fruits');
  32. $this->assertEquals('food/fruits', $fruits->getSlug());
  33. $oranges = $repo->findOneByTitle('Oranges');
  34. $this->assertEquals('food/fruits/oranges', $oranges->getSlug());
  35. $citrons = $repo->findOneByTitle('Citrons');
  36. $this->assertEquals('food/fruits/citrons', $citrons->getSlug());
  37. $apple = $repo->findOneByTitle('Apple');
  38. $this->assertEquals('food/fruits/apple', $apple->getSlug());
  39. $kiwi = $repo->findOneByTitle('Kiwi');
  40. $this->assertEquals('food/fruits/kiwi', $kiwi->getSlug());
  41. $banana = $repo->findOneByTitle('Banana');
  42. $this->assertEquals('food/fruits/banana', $banana->getSlug());
  43. }
  44. public function testSlugUpdates()
  45. {
  46. $this->populate();
  47. $repo = $this->em->getRepository(self::TARGET);
  48. $fruits = $repo->findOneByTitle('Fruits');
  49. $fruits->setTitle('Fructis');
  50. $this->em->persist($fruits);
  51. $this->em->flush();
  52. $this->assertEquals('food/fructis', $fruits->getSlug());
  53. $oranges = $repo->findOneByTitle('Oranges');
  54. $this->assertEquals('food/fructis/oranges', $oranges->getSlug());
  55. $citrons = $repo->findOneByTitle('Citrons');
  56. $this->assertEquals('food/fructis/citrons', $citrons->getSlug());
  57. $food = $repo->findOneByTitle('Food');
  58. $food->setTitle('Foodissimo');
  59. $this->em->persist($food);
  60. $this->em->flush();
  61. $this->assertEquals('foodissimo', $food->getSlug());
  62. $this->assertEquals('foodissimo/fructis/oranges', $oranges->getSlug());
  63. $this->assertEquals('foodissimo/fructis/citrons', $citrons->getSlug());
  64. }
  65. public function testMoreSlugUpdates()
  66. {
  67. $this->populate();
  68. $repo = $this->em->getRepository(self::TARGET);
  69. $fruits = $repo->findOneByTitle('Fruits');
  70. $fruits->setTitle('Fructis');
  71. $milk = $repo->findOneByTitle('Milk');
  72. $repo->persistAsFirstChildOf($fruits, $milk);
  73. $this->em->flush();
  74. $this->assertEquals('food/milk/fructis', $fruits->getSlug());
  75. $oranges = $repo->findOneByTitle('Oranges');
  76. $this->assertEquals('food/milk/fructis/oranges', $oranges->getSlug());
  77. $citrons = $repo->findOneByTitle('Citrons');
  78. $this->assertEquals('food/milk/fructis/citrons', $citrons->getSlug());
  79. $food = $repo->findOneByTitle('Food');
  80. $food->setTitle('Foodissimo');
  81. $this->em->persist($food);
  82. $this->em->flush();
  83. $this->assertEquals('foodissimo', $food->getSlug());
  84. $this->assertEquals('foodissimo/milk/fructis/oranges', $oranges->getSlug());
  85. $this->assertEquals('foodissimo/milk/fructis/citrons', $citrons->getSlug());
  86. $repo->persistAsFirstChildOf($fruits, $food);
  87. $this->em->flush();
  88. $this->assertEquals('foodissimo/fructis/oranges', $oranges->getSlug());
  89. $this->assertEquals('foodissimo/fructis/citrons', $citrons->getSlug());
  90. }
  91. protected function getUsedEntityFixtures()
  92. {
  93. return array(
  94. self::TARGET
  95. );
  96. }
  97. private function populate()
  98. {
  99. $repo = $this->em->getRepository(self::TARGET);
  100. $food = new TreeSlug;
  101. $food->setTitle('Food');
  102. $fruits = new TreeSlug;
  103. $fruits->setTitle('Fruits');
  104. $vegitables = new TreeSlug;
  105. $vegitables->setTitle('Vegitables');
  106. $milk = new TreeSlug;
  107. $milk->setTitle('Milk');
  108. $meat = new TreeSlug;
  109. $meat->setTitle('Meat');
  110. $oranges = new TreeSlug;
  111. $oranges->setTitle('Oranges');
  112. $citrons = new TreeSlug;
  113. $citrons->setTitle('Citrons');
  114. $apple = new TreeSlug;
  115. $apple->setTitle('Apple');
  116. $kiwi = new TreeSlug;
  117. $kiwi->setTitle('Kiwi');
  118. $banana = new TreeSlug;
  119. $banana->setTitle('Banana');
  120. $repo
  121. ->persistAsFirstChild($food)
  122. ->persistAsFirstChildOf($fruits, $food)
  123. ->persistAsFirstChildOf($vegitables, $food)
  124. ->persistAsLastChildOf($milk, $food)
  125. ->persistAsLastChildOf($meat, $food)
  126. ->persistAsFirstChildOf($oranges, $fruits)
  127. ->persistAsFirstChildOf($citrons, $fruits)
  128. ->persistAsFirstChildOf($apple, $fruits)
  129. ->persistAsPrevSiblingOf($kiwi, $apple)
  130. ->persistAsNextSiblingOf($banana, $apple);
  131. $this->em->flush();
  132. }
  133. }