SluggableTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\Article;
  6. /**
  7. * These are tests for sluggable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Sluggable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class SluggableTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\Article';
  17. private $articleId;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new SluggableListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. $this->populate();
  25. }
  26. public function testInsertedNewSlug()
  27. {
  28. $article = $this->em->find(self::ARTICLE, $this->articleId);
  29. $this->assertTrue($article instanceof Sluggable);
  30. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  31. }
  32. public function testUniqueSlugGeneration()
  33. {
  34. for ($i = 0; $i < 12; $i++) {
  35. $article = new Article();
  36. $article->setTitle('the title');
  37. $article->setCode('my code');
  38. $this->em->persist($article);
  39. $this->em->flush();
  40. $this->em->clear();
  41. $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
  42. }
  43. }
  44. public function testUniqueSlugLimit()
  45. {
  46. $long = 'the title the title the title the title the title the title the title';
  47. $article = new Article();
  48. $article->setTitle($long);
  49. $article->setCode('my code');
  50. $this->em->persist($article);
  51. $this->em->flush();
  52. $this->em->clear();
  53. for ($i = 0; $i < 12; $i++) {
  54. $article = new Article();
  55. $article->setTitle($long);
  56. $article->setCode('my code');
  57. $this->em->persist($article);
  58. $this->em->flush();
  59. $this->em->clear();
  60. $shorten = $article->getSlug();
  61. $this->assertEquals(64, strlen($shorten));
  62. $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
  63. $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
  64. $this->assertEquals($shorten, $expected);
  65. }
  66. }
  67. public function testUniqueNumberedSlug()
  68. {
  69. $article = new Article();
  70. $article->setTitle('the title');
  71. $article->setCode('my code 123');
  72. $this->em->persist($article);
  73. $this->em->flush();
  74. for ($i = 0; $i < 12; $i++) {
  75. $article = new Article();
  76. $article->setTitle('the title');
  77. $article->setCode('my code 123');
  78. $this->em->persist($article);
  79. $this->em->flush();
  80. $this->em->clear();
  81. $this->assertEquals($article->getSlug(), 'the-title-my-code-123-' . ($i + 1));
  82. }
  83. }
  84. public function testUpdatableSlug()
  85. {
  86. $article = $this->em->find(self::ARTICLE, $this->articleId);
  87. $article->setTitle('the title updated');
  88. $this->em->persist($article);
  89. $this->em->flush();
  90. $this->em->clear();
  91. $this->assertEquals($article->getSlug(), 'the-title-updated-my-code');
  92. }
  93. public function testGithubIssue45()
  94. {
  95. // persist new records with same slug
  96. $article = new Article;
  97. $article->setTitle('test');
  98. $article->setCode('code');
  99. $this->em->persist($article);
  100. $article2 = new Article;
  101. $article2->setTitle('test');
  102. $article2->setCode('code');
  103. $this->em->persist($article2);
  104. $this->em->flush();
  105. $this->assertEquals('test-code', $article->getSlug());
  106. $this->assertEquals('test-code-1', $article2->getSlug());
  107. }
  108. public function testGithubIssue57()
  109. {
  110. // slug matched by prefix
  111. $article = new Article;
  112. $article->setTitle('my');
  113. $article->setCode('slug');
  114. $this->em->persist($article);
  115. $article2 = new Article;
  116. $article2->setTitle('my');
  117. $article2->setCode('s');
  118. $this->em->persist($article2);
  119. $this->em->flush();
  120. $this->assertEquals('my-s', $article2->getSlug());
  121. }
  122. protected function getUsedEntityFixtures()
  123. {
  124. return array(
  125. self::ARTICLE,
  126. );
  127. }
  128. private function populate()
  129. {
  130. $article = new Article();
  131. $article->setTitle('the title');
  132. $article->setCode('my code');
  133. $this->em->persist($article);
  134. $this->em->flush();
  135. $this->em->clear();
  136. $this->articleId = $article->getId();
  137. }
  138. }