SluggableConfigurationTest.php 3.5 KB

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