BothSlugHandlerTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\Handler\People\Occupation;
  6. use Sluggable\Fixture\Handler\People\Person;
  7. use Gedmo\Tree\TreeListener;
  8. /**
  9. * These are tests for Sluggable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Sluggable
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class BothSlugHandlerTest extends BaseTestCaseORM
  17. {
  18. const OCCUPATION = "Sluggable\\Fixture\\Handler\\People\\Occupation";
  19. const PERSON = "Sluggable\\Fixture\\Handler\\People\\Person";
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $evm->addEventSubscriber(new TreeListener);
  25. $evm->addEventSubscriber(new SluggableListener);
  26. $this->getMockSqliteEntityManager($evm);
  27. }
  28. public function testSlugGeneration()
  29. {
  30. $this->populate();
  31. $repo = $this->em->getRepository(self::PERSON);
  32. $herzult = $repo->findOneByName('Herzult');
  33. $this->assertEquals('web/developer/php/herzult', $herzult->getSlug());
  34. $gedi = $repo->findOneByName('Gedi');
  35. $this->assertEquals('web/developer/gedi', $gedi->getSlug());
  36. $hurty = $repo->findOneByName('Hurty');
  37. $this->assertEquals('singer/hurty', $hurty->getSlug());
  38. }
  39. public function testSlugUpdates()
  40. {
  41. $this->populate();
  42. $repo = $this->em->getRepository(self::PERSON);
  43. $gedi = $repo->findOneByName('Gedi');
  44. $gedi->setName('Upd Gedi');
  45. $this->em->persist($gedi);
  46. $this->em->flush();
  47. $this->assertEquals('web/developer/upd-gedi', $gedi->getSlug());
  48. $artist = $this->em->getRepository(self::OCCUPATION)->findOneByTitle('Singer');
  49. $artist->setTitle('Artist');
  50. $this->em->persist($artist);
  51. $this->em->flush();
  52. $gedi->setOccupation($artist);
  53. $this->em->persist($gedi);
  54. $this->em->flush();
  55. $this->assertEquals('artist/upd-gedi', $gedi->getSlug());
  56. $hurty = $repo->findOneByName('Hurty');
  57. $this->assertEquals('artist/hurty', $hurty->getSlug());
  58. }
  59. protected function getUsedEntityFixtures()
  60. {
  61. return array(
  62. self::OCCUPATION,
  63. self::PERSON
  64. );
  65. }
  66. private function populate()
  67. {
  68. $repo = $this->em->getRepository(self::OCCUPATION);
  69. $web = new Occupation;
  70. $web->setTitle('Web');
  71. $developer = new Occupation;
  72. $developer->setTitle('Developer');
  73. $designer = new Occupation;
  74. $designer->setTitle('Designer');
  75. $php = new Occupation;
  76. $php->setTitle('PHP');
  77. $singer = new Occupation;
  78. $singer->setTitle('Singer');
  79. $rock = new Occupation;
  80. $rock->setTitle('Rock');
  81. $repo
  82. ->persistAsFirstChild($web)
  83. ->persistAsFirstChild($singer)
  84. ->persistAsFirstChildOf($developer, $web)
  85. ->persistAsFirstChildOf($designer, $web)
  86. ->persistAsLastChildOf($php, $developer)
  87. ->persistAsLastChildOf($rock, $singer)
  88. ;
  89. $herzult = new Person;
  90. $herzult->setName('Herzult');
  91. $herzult->setOccupation($php);
  92. $this->em->persist($herzult);
  93. $gedi = new Person;
  94. $gedi->setName('Gedi');
  95. $gedi->setOccupation($developer);
  96. $this->em->persist($gedi);
  97. $hurty = new Person;
  98. $hurty->setName('Hurty');
  99. $hurty->setOccupation($singer);
  100. $this->em->persist($hurty);
  101. $this->em->flush();
  102. }
  103. }