BothSlugHandlerTest.php 3.6 KB

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