SluggableConfigurationTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\Util\Debug,
  4. Sluggable\Fixture\ConfigurationArticle;
  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 SluggableConfigurationTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CLASS = 'Sluggable\Fixture\ConfigurationArticle';
  16. private $articleId;
  17. private $em;
  18. public function setUp()
  19. {
  20. $classLoader = new \Doctrine\Common\ClassLoader('Sluggable\Fixture', __DIR__ . '/../');
  21. $classLoader->register();
  22. $config = new \Doctrine\ORM\Configuration();
  23. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  24. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setProxyDir(__DIR__ . '/Proxy');
  26. $config->setProxyNamespace('Gedmo\Sluggable\Proxies');
  27. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  28. $conn = array(
  29. 'driver' => 'pdo_sqlite',
  30. 'memory' => true,
  31. );
  32. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  33. $evm = new \Doctrine\Common\EventManager();
  34. $sluggableListener = new SluggableListener();
  35. $evm->addEventSubscriber($sluggableListener);
  36. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  37. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  38. $schemaTool->dropSchema(array());
  39. $schemaTool->createSchema(array(
  40. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS)
  41. ));
  42. $article = new ConfigurationArticle();
  43. $article->setTitle('the title');
  44. $article->setCode('my code');
  45. $this->em->persist($article);
  46. $this->em->flush();
  47. $this->em->clear();
  48. $this->articleId = $article->getId();
  49. }
  50. public function testInsertedNewSlug()
  51. {
  52. $article = $this->em->find(
  53. self::TEST_ENTITY_CLASS,
  54. $this->articleId
  55. );
  56. $this->assertTrue($article instanceof Sluggable);
  57. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  58. }
  59. public function testNonUniqueSlugGeneration()
  60. {
  61. for ($i = 0; $i < 5; $i++) {
  62. $article = new ConfigurationArticle();
  63. $article->setTitle('the title');
  64. $article->setCode('my code');
  65. $this->em->persist($article);
  66. $this->em->flush();
  67. $this->em->clear();
  68. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  69. }
  70. }
  71. public function testSlugLimit()
  72. {
  73. $long = 'the title the title the title the title the';
  74. $article = new ConfigurationArticle();
  75. $article->setTitle($long);
  76. $article->setCode('my code');
  77. $this->em->persist($article);
  78. $this->em->flush();
  79. $this->em->clear();
  80. $shorten = $article->getSlug();
  81. $this->assertEquals(strlen($shorten), 32);
  82. }
  83. public function testNonUpdatableSlug()
  84. {
  85. $article = $this->em->find(
  86. self::TEST_ENTITY_CLASS,
  87. $this->articleId
  88. );
  89. $article->setTitle('the title updated');
  90. $this->em->persist($article);
  91. $this->em->flush();
  92. $this->em->clear();
  93. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  94. }
  95. }