SluggableTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\Util\Debug,
  4. Sluggable\Fixture\Article;
  5. /**
  6. * These are tests for translatable behavior
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @package Gedmo.Translatable
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class SluggableTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CLASS = 'Sluggable\Fixture\Article';
  16. private $articleId;
  17. /**
  18. * @var EntityManager
  19. */
  20. private $em;
  21. public function setUp()
  22. {
  23. $config = new \Doctrine\ORM\Configuration();
  24. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  26. $config->setProxyDir(__DIR__ . '/Proxy');
  27. $config->setProxyNamespace('Gedmo\Sluggable\Proxies');
  28. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  29. $conn = array(
  30. 'driver' => 'pdo_sqlite',
  31. 'memory' => true,
  32. );
  33. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  34. $evm = new \Doctrine\Common\EventManager();
  35. $sluggableListener = new SluggableListener();
  36. $evm->addEventSubscriber($sluggableListener);
  37. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  38. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  39. $schemaTool->dropSchema(array());
  40. $schemaTool->createSchema(array(
  41. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS)
  42. ));
  43. $article = new Article();
  44. $article->setTitle('the title');
  45. $article->setCode('my code');
  46. $this->em->persist($article);
  47. $this->em->flush();
  48. $this->articleId = $article->getId();
  49. $this->em->clear();
  50. }
  51. public function testInsertedNewSlug()
  52. {
  53. $article = $this->em->find(
  54. self::TEST_ENTITY_CLASS,
  55. $this->articleId
  56. );
  57. $this->assertTrue($article instanceof Sluggable);
  58. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  59. }
  60. public function testUniqueSlugGeneration()
  61. {
  62. for ($i = 0; $i < 12; $i++) {
  63. $article = new Article();
  64. $article->setTitle('the title');
  65. $article->setCode('my code');
  66. $this->em->persist($article);
  67. $this->em->flush();
  68. $this->em->clear();
  69. $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
  70. }
  71. }
  72. public function testUniqueSlugLimit()
  73. {
  74. $long = 'the title the title the title the title the title the title the title';
  75. $article = new Article();
  76. $article->setTitle($long);
  77. $article->setCode('my code');
  78. $this->em->persist($article);
  79. $this->em->flush();
  80. $this->em->clear();
  81. for ($i = 0; $i < 12; $i++) {
  82. $article = new Article();
  83. $article->setTitle($long);
  84. $article->setCode('my code');
  85. $this->em->persist($article);
  86. $this->em->flush();
  87. $this->em->clear();
  88. $shorten = $article->getSlug();
  89. $this->assertEquals(strlen($shorten), 64);
  90. $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
  91. $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
  92. $this->assertEquals($shorten, $expected);
  93. }
  94. }
  95. public function testUniqueNumberedSlug()
  96. {
  97. $article = new Article();
  98. $article->setTitle('the title');
  99. $article->setCode('my code 123');
  100. $this->em->persist($article);
  101. $this->em->flush();
  102. for ($i = 0; $i < 12; $i++) {
  103. $article = new Article();
  104. $article->setTitle('the title');
  105. $article->setCode('my code 123');
  106. $this->em->persist($article);
  107. $this->em->flush();
  108. $this->em->clear();
  109. $this->assertEquals($article->getSlug(), 'the-title-my-code-123-' . ($i + 1));
  110. }
  111. }
  112. public function testUpdatableSlug()
  113. {
  114. $article = $this->em->find(
  115. self::TEST_ENTITY_CLASS,
  116. $this->articleId
  117. );
  118. $article->setTitle('the title updated');
  119. $this->em->persist($article);
  120. $this->em->flush();
  121. $this->em->clear();
  122. $this->assertEquals($article->getSlug(), 'the-title-updated-my-code');
  123. }
  124. }