TreeSlugHandlerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\TreeSlug;
  6. use Gedmo\Tree\TreeListener;
  7. /**
  8. * These are tests for Sluggable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Sluggable
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TreeSlugHandlerTest extends BaseTestCaseORM
  16. {
  17. const TARGET = "Sluggable\\Fixture\\TreeSlug";
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new SluggableListener);
  23. $evm->addEventSubscriber(new TreeListener);
  24. $conn = array(
  25. 'driver' => 'pdo_mysql',
  26. 'host' => '127.0.0.1',
  27. 'dbname' => 'tests',
  28. 'user' => 'root',
  29. 'password' => 'nimda'
  30. );
  31. //$this->getMockCustomEntityManager($conn, $evm);
  32. $this->getMockSqliteEntityManager($evm);
  33. }
  34. public function testSlugGeneration()
  35. {
  36. $this->populate();
  37. $repo = $this->em->getRepository(self::TARGET);
  38. $food = $repo->findOneByTitle('Food');
  39. $this->assertEquals('food', $food->getSlug());
  40. $fruits = $repo->findOneByTitle('Fruits');
  41. $this->assertEquals('food/fruits', $fruits->getSlug());
  42. $oranges = $repo->findOneByTitle('Oranges');
  43. $this->assertEquals('food/fruits/oranges', $oranges->getSlug());
  44. $citrons = $repo->findOneByTitle('Citrons');
  45. $this->assertEquals('food/fruits/citrons', $citrons->getSlug());
  46. }
  47. public function testSlugUpdates()
  48. {
  49. $this->populate();
  50. $repo = $this->em->getRepository(self::TARGET);
  51. $fruits = $repo->findOneByTitle('Fruits');
  52. $fruits->setTitle('Fructis');
  53. $this->em->persist($fruits);
  54. $this->em->flush();
  55. $this->assertEquals('food/fructis', $fruits->getSlug());
  56. $oranges = $repo->findOneByTitle('Oranges');
  57. $this->assertEquals('food/fructis/oranges', $oranges->getSlug());
  58. $citrons = $repo->findOneByTitle('Citrons');
  59. $this->assertEquals('food/fructis/citrons', $citrons->getSlug());
  60. $food = $repo->findOneByTitle('Food');
  61. $food->setTitle('Foodissimo');
  62. $this->em->persist($food);
  63. $this->em->flush();
  64. $this->assertEquals('foodissimo', $food->getSlug());
  65. $this->assertEquals('foodissimo/fructis/oranges', $oranges->getSlug());
  66. $this->assertEquals('foodissimo/fructis/citrons', $citrons->getSlug());
  67. }
  68. protected function getUsedEntityFixtures()
  69. {
  70. return array(
  71. self::TARGET
  72. );
  73. }
  74. private function populate()
  75. {
  76. $repo = $this->em->getRepository(self::TARGET);
  77. $food = new TreeSlug;
  78. $food->setTitle('Food');
  79. $fruits = new TreeSlug;
  80. $fruits->setTitle('Fruits');
  81. $vegitables = new TreeSlug;
  82. $vegitables->setTitle('Vegitables');
  83. $milk = new TreeSlug;
  84. $milk->setTitle('Milk');
  85. $meat = new TreeSlug;
  86. $meat->setTitle('Meat');
  87. $oranges = new TreeSlug;
  88. $oranges->setTitle('Oranges');
  89. $citrons = new TreeSlug;
  90. $citrons->setTitle('Citrons');
  91. $repo
  92. ->persistAsFirstChild($food)
  93. ->persistAsFirstChildOf($fruits, $food)
  94. ->persistAsFirstChildOf($vegitables, $food)
  95. ->persistAsLastChildOf($milk, $food)
  96. ->persistAsLastChildOf($meat, $food)
  97. ->persistAsFirstChildOf($oranges, $fruits)
  98. ->persistAsFirstChildOf($citrons, $fruits);
  99. $this->em->flush();
  100. }
  101. }