RelativeSlugHandlerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\Handler\Article;
  6. use Sluggable\Fixture\Handler\ArticleRelativeSlug;
  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 RelativeSlugHandlerTest extends BaseTestCaseORM
  15. {
  16. const SLUG = "Sluggable\\Fixture\\Handler\\ArticleRelativeSlug";
  17. const ARTICLE = "Sluggable\\Fixture\\Handler\\Article";
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new SluggableListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. }
  25. public function testSlugGeneration()
  26. {
  27. $this->populate();
  28. $repo = $this->em->getRepository(self::SLUG);
  29. $thomas = $repo->findOneByTitle('Thomas');
  30. $this->assertEquals('sport-test/thomas', $thomas->getSlug());
  31. $jen = $repo->findOneByTitle('Jen');
  32. $this->assertEquals('sport-test/jen', $jen->getSlug());
  33. $john = $repo->findOneByTitle('John');
  34. $this->assertEquals('cars-code/john', $john->getSlug());
  35. $single = $repo->findOneByTitle('Single');
  36. $this->assertEquals('single', $single->getSlug());
  37. }
  38. public function testUpdateOperations()
  39. {
  40. $this->populate();
  41. $repo = $this->em->getRepository(self::SLUG);
  42. $thomas = $repo->findOneByTitle('Thomas');
  43. $thomas->setTitle('Ninja');
  44. $this->em->persist($thomas);
  45. $this->em->flush();
  46. $this->assertEquals('sport-test/ninja', $thomas->getSlug());
  47. $sport = $this->em->getRepository(self::ARTICLE)->findOneByTitle('Sport');
  48. $sport->setTitle('Martial Arts');
  49. $this->em->persist($sport);
  50. $this->em->flush();
  51. $this->assertEquals('martial-arts-test/ninja', $thomas->getSlug());
  52. $jen = $repo->findOneByTitle('Jen');
  53. $this->assertEquals('martial-arts-test/jen', $jen->getSlug());
  54. $cars = $this->em->getRepository(self::ARTICLE)->findOneByTitle('Cars');
  55. $jen->setArticle($cars);
  56. $this->em->persist($jen);
  57. $this->em->flush();
  58. $this->assertEquals('cars-code/jen', $jen->getSlug());
  59. }
  60. protected function getUsedEntityFixtures()
  61. {
  62. return array(
  63. self::SLUG,
  64. self::ARTICLE
  65. );
  66. }
  67. private function populate()
  68. {
  69. $sport = new Article;
  70. $sport->setTitle('Sport');
  71. $sport->setCode('test');
  72. $this->em->persist($sport);
  73. $cars = new Article;
  74. $cars->setTitle('Cars');
  75. $cars->setCode('code');
  76. $this->em->persist($cars);
  77. $thomas = new ArticleRelativeSlug;
  78. $thomas->setTitle('Thomas');
  79. $thomas->setArticle($sport);
  80. $this->em->persist($thomas);
  81. $jen = new ArticleRelativeSlug;
  82. $jen->setTitle('Jen');
  83. $jen->setArticle($sport);
  84. $this->em->persist($jen);
  85. $john = new ArticleRelativeSlug;
  86. $john->setTitle('John');
  87. $john->setArticle($cars);
  88. $this->em->persist($john);
  89. $single = new ArticleRelativeSlug;
  90. $single->setTitle('Single');
  91. $this->em->persist($single);
  92. $this->em->flush();
  93. }
  94. }