SluggableConfigurationTest.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * @package Gedmo.Sluggable
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class SluggableConfigurationTest extends BaseTestCaseORM
  16. {
  17. const ARTICLE = 'Sluggable\\Fixture\\ConfigurationArticle';
  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('the-title-my-code', $article->getSlug());
  32. }
  33. public function testNonUniqueSlugGeneration()
  34. {
  35. for ($i = 0; $i < 5; $i++) {
  36. $article = new ConfigurationArticle();
  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('the-title-my-code', $article->getSlug());
  43. }
  44. }
  45. public function testSlugLimit()
  46. {
  47. $long = 'the title the title the title the title the';
  48. $article = new ConfigurationArticle();
  49. $article->setTitle($long);
  50. $article->setCode('my code');
  51. $this->em->persist($article);
  52. $this->em->flush();
  53. $this->em->clear();
  54. $shorten = $article->getSlug();
  55. $this->assertEquals(32, strlen($shorten));
  56. }
  57. public function testNonUpdatableSlug()
  58. {
  59. $article = $this->em->find(self::ARTICLE, $this->articleId);
  60. $article->setTitle('the title updated');
  61. $this->em->persist($article);
  62. $this->em->flush();
  63. $this->em->clear();
  64. $this->assertEquals('the-title-my-code', $article->getSlug());
  65. }
  66. protected function getUsedEntityFixtures()
  67. {
  68. return array(
  69. self::ARTICLE,
  70. );
  71. }
  72. private function populate()
  73. {
  74. $article = new ConfigurationArticle();
  75. $article->setTitle('the title');
  76. $article->setCode('my code');
  77. $this->em->persist($article);
  78. $this->em->flush();
  79. $this->em->clear();
  80. $this->articleId = $article->getId();
  81. }
  82. }