SluggableTest.php 3.7 KB

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