SluggableTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $classLoader = new \Doctrine\Common\ClassLoader('Sluggable\Fixture', __DIR__ . '/../');
  24. $classLoader->register();
  25. $config = new \Doctrine\ORM\Configuration();
  26. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  27. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  28. $config->setProxyDir(__DIR__ . '/Proxy');
  29. $config->setProxyNamespace('Gedmo\Sluggable\Proxies');
  30. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  31. $conn = array(
  32. 'driver' => 'pdo_sqlite',
  33. 'memory' => true,
  34. );
  35. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  36. $evm = new \Doctrine\Common\EventManager();
  37. $sluggableListener = new SluggableListener();
  38. $evm->addEventSubscriber($sluggableListener);
  39. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  40. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  41. $schemaTool->dropSchema(array());
  42. $schemaTool->createSchema(array(
  43. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS)
  44. ));
  45. $article = new Article();
  46. $article->setTitle('the title');
  47. $article->setCode('my code');
  48. $this->em->persist($article);
  49. $this->em->flush();
  50. $this->articleId = $article->getId();
  51. $this->em->clear();
  52. }
  53. public function testInsertedNewSlug()
  54. {
  55. $article = $this->em->find(
  56. self::TEST_ENTITY_CLASS,
  57. $this->articleId
  58. );
  59. $this->assertTrue($article instanceof Sluggable);
  60. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  61. }
  62. public function testUniqueSlugGeneration()
  63. {
  64. for ($i = 0; $i < 12; $i++) {
  65. $article = new Article();
  66. $article->setTitle('the title');
  67. $article->setCode('my code');
  68. $this->em->persist($article);
  69. $this->em->flush();
  70. $this->em->clear();
  71. $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
  72. }
  73. }
  74. public function testUniqueSlugLimit()
  75. {
  76. $long = 'the title the title the title the title the title the title the title';
  77. $article = new Article();
  78. $article->setTitle($long);
  79. $article->setCode('my code');
  80. $this->em->persist($article);
  81. $this->em->flush();
  82. $this->em->clear();
  83. for ($i = 0; $i < 12; $i++) {
  84. $article = new Article();
  85. $article->setTitle($long);
  86. $article->setCode('my code');
  87. $this->em->persist($article);
  88. $this->em->flush();
  89. $this->em->clear();
  90. $shorten = $article->getSlug();
  91. $this->assertEquals(strlen($shorten), 64);
  92. $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
  93. $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
  94. $this->assertEquals($shorten, $expected);
  95. }
  96. }
  97. public function testUpdatableSlug()
  98. {
  99. $article = $this->em->find(
  100. self::TEST_ENTITY_CLASS,
  101. $this->articleId
  102. );
  103. $article->setTitle('the title updated');
  104. $this->em->persist($article);
  105. $this->em->flush();
  106. $this->em->clear();
  107. $this->assertEquals($article->getSlug(), 'the-title-updated-my-code');
  108. }
  109. }