SluggableTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /**
  27. * @test
  28. */
  29. function shouldInsertNewSlug()
  30. {
  31. $article = $this->em->find(self::ARTICLE, $this->articleId);
  32. $this->assertTrue($article instanceof Sluggable);
  33. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  34. }
  35. /**
  36. * @test
  37. */
  38. function shouldBuildUniqueSlug()
  39. {
  40. for ($i = 0; $i < 12; $i++) {
  41. $article = new Article();
  42. $article->setTitle('the title');
  43. $article->setCode('my code');
  44. $this->em->persist($article);
  45. $this->em->flush();
  46. $this->em->clear();
  47. $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
  48. }
  49. }
  50. /**
  51. * @test
  52. */
  53. function shouldHandleUniqueSlugLimitedLength()
  54. {
  55. $long = 'the title the title the title the title the title the title the title';
  56. $article = new Article();
  57. $article->setTitle($long);
  58. $article->setCode('my code');
  59. $this->em->persist($article);
  60. $this->em->flush();
  61. $this->em->clear();
  62. for ($i = 0; $i < 12; $i++) {
  63. $article = new Article();
  64. $article->setTitle($long);
  65. $article->setCode('my code');
  66. $this->em->persist($article);
  67. $this->em->flush();
  68. $this->em->clear();
  69. $shorten = $article->getSlug();
  70. $this->assertEquals(64, strlen($shorten));
  71. $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
  72. $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
  73. $this->assertEquals($shorten, $expected);
  74. }
  75. }
  76. /**
  77. * @test
  78. */
  79. function shouldHandleNumbersInSlug()
  80. {
  81. $article = new Article();
  82. $article->setTitle('the title');
  83. $article->setCode('my code 123');
  84. $this->em->persist($article);
  85. $this->em->flush();
  86. for ($i = 0; $i < 12; $i++) {
  87. $article = new Article();
  88. $article->setTitle('the title');
  89. $article->setCode('my code 123');
  90. $this->em->persist($article);
  91. $this->em->flush();
  92. $this->em->clear();
  93. $this->assertEquals($article->getSlug(), 'the-title-my-code-123-' . ($i + 1));
  94. }
  95. }
  96. /**
  97. * @test
  98. */
  99. function shouldUpdateSlug()
  100. {
  101. $article = $this->em->find(self::ARTICLE, $this->articleId);
  102. $article->setTitle('the title updated');
  103. $this->em->persist($article);
  104. $this->em->flush();
  105. $this->assertSame('the-title-updated-my-code', $article->getSlug());
  106. }
  107. /**
  108. * @test
  109. */
  110. function shouldBeAbleToForceRegenerationOfSlug()
  111. {
  112. $article = $this->em->find(self::ARTICLE, $this->articleId);
  113. $article->setSlug(null);
  114. $this->em->persist($article);
  115. $this->em->flush();
  116. $this->assertSame('the-title-my-code', $article->getSlug());
  117. }
  118. /**
  119. * @test
  120. */
  121. function shouldBeAbleToForceTheSlug()
  122. {
  123. $article = $this->em->find(self::ARTICLE, $this->articleId);
  124. $article->setSlug('my forced slug');
  125. $this->em->persist($article);
  126. $new = new Article;
  127. $new->setTitle('hey');
  128. $new->setCode('cc');
  129. $new->setSlug('forced');
  130. $this->em->persist($new);
  131. $this->em->flush();
  132. $this->assertSame('my-forced-slug', $article->getSlug());
  133. $this->assertSame('forced', $new->getSlug());
  134. }
  135. /**
  136. * @test
  137. */
  138. function shouldSolveGithubIssue45()
  139. {
  140. // persist new records with same slug
  141. $article = new Article;
  142. $article->setTitle('test');
  143. $article->setCode('code');
  144. $this->em->persist($article);
  145. $article2 = new Article;
  146. $article2->setTitle('test');
  147. $article2->setCode('code');
  148. $this->em->persist($article2);
  149. $this->em->flush();
  150. $this->assertEquals('test-code', $article->getSlug());
  151. $this->assertEquals('test-code-1', $article2->getSlug());
  152. }
  153. /**
  154. * @test
  155. */
  156. function shouldSolveGithubIssue57()
  157. {
  158. // slug matched by prefix
  159. $article = new Article;
  160. $article->setTitle('my');
  161. $article->setCode('slug');
  162. $this->em->persist($article);
  163. $article2 = new Article;
  164. $article2->setTitle('my');
  165. $article2->setCode('s');
  166. $this->em->persist($article2);
  167. $this->em->flush();
  168. $this->assertEquals('my-s', $article2->getSlug());
  169. }
  170. protected function getUsedEntityFixtures()
  171. {
  172. return array(
  173. self::ARTICLE,
  174. );
  175. }
  176. private function populate()
  177. {
  178. $article = new Article();
  179. $article->setTitle('the title');
  180. $article->setCode('my code');
  181. $this->em->persist($article);
  182. $this->em->flush();
  183. $this->em->clear();
  184. $this->articleId = $article->getId();
  185. }
  186. }