SluggableConfigurationTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Sluggable\Fixture\ConfigurationArticle;
  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 SluggableConfigurationTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\ConfigurationArticle';
  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('the-title-my-code', $article->getSlug());
  31. }
  32. public function testNonUniqueSlugGeneration()
  33. {
  34. for ($i = 0; $i < 5; $i++) {
  35. $article = new ConfigurationArticle();
  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('the-title-my-code', $article->getSlug());
  42. }
  43. }
  44. public function testSlugLimit()
  45. {
  46. $long = 'the title the title the title the title the';
  47. $article = new ConfigurationArticle();
  48. $article->setTitle($long);
  49. $article->setCode('my code');
  50. $this->em->persist($article);
  51. $this->em->flush();
  52. $this->em->clear();
  53. $shorten = $article->getSlug();
  54. $this->assertEquals(32, strlen($shorten));
  55. }
  56. public function testNonUpdatableSlug()
  57. {
  58. $article = $this->em->find(self::ARTICLE, $this->articleId);
  59. $article->setTitle('the title updated');
  60. $this->em->persist($article);
  61. $this->em->flush();
  62. $this->em->clear();
  63. $this->assertEquals('the-title-my-code', $article->getSlug());
  64. }
  65. protected function getUsedEntityFixtures()
  66. {
  67. return array(
  68. self::ARTICLE,
  69. );
  70. }
  71. private function populate()
  72. {
  73. $article = new ConfigurationArticle();
  74. $article->setTitle('the title');
  75. $article->setCode('my code');
  76. $this->em->persist($article);
  77. $this->em->flush();
  78. $this->em->clear();
  79. $this->articleId = $article->getId();
  80. }
  81. }