SluggableDocumentTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Sluggable\Fixture\Document\Article;
  4. /**
  5. * These are tests for sluggable behavior
  6. *
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @package Gedmo.Sluggable
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class SluggableDocumentTest extends \PHPUnit_Framework_TestCase
  13. {
  14. const TEST_ENTITY_CLASS = 'Sluggable\Fixture\Document\Article';
  15. /**
  16. * @var DocumentManager
  17. */
  18. private $dm;
  19. public function setUp()
  20. {
  21. $config = new \Doctrine\ODM\MongoDB\Configuration();
  22. $config->setProxyDir(__DIR__ . '/Proxy');
  23. $config->setProxyNamespace('Gedmo\Sluggable\Proxies');
  24. $config->setHydratorDir(__DIR__ . '/Hydrator');
  25. $config->setHydratorNamespace('Hydrator');
  26. $config->setDefaultDB('doctrine_odm_sluggable_tests');
  27. $config->setLoggerCallable(function(array $log) {
  28. print_r($log);
  29. });
  30. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  31. $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
  32. $config->setMetadataDriverImpl(
  33. new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader, __DIR__ . '/Fixture/Document')
  34. );
  35. $evm = new \Doctrine\Common\EventManager();
  36. $sluggableListener = new ODM\MongoDB\SluggableListener();
  37. $evm->addEventSubscriber($sluggableListener);
  38. if (!class_exists('Mongo')) {
  39. $this->markTestSkipped('Missing Mongo extension.');
  40. }
  41. try {
  42. $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(
  43. new \Doctrine\MongoDB\Connection(),
  44. $config,
  45. $evm
  46. );
  47. $this->populate();
  48. } catch (\MongoException $e) {
  49. $this->markTestSkipped('Doctrine MongoDB ODM connection problem.');
  50. }
  51. }
  52. public function testSlugGeneration()
  53. {
  54. // test insert
  55. $repo = $this->dm->getRepository(self::TEST_ENTITY_CLASS);
  56. $article = $repo->findOneByTitle('My Title');
  57. $this->assertEquals('my-title-the-code', $article->getSlug());
  58. // test update
  59. $article->setTitle('New Title');
  60. $this->dm->persist($article);
  61. $this->dm->flush();
  62. $this->dm->clear();
  63. $article = $repo->findOneByTitle('New Title');
  64. $this->assertEquals('new-title-the-code', $article->getSlug());
  65. }
  66. public function testUniqueSlugGeneration()
  67. {
  68. for ($i = 0; $i < 12; $i++) {
  69. $article = new Article();
  70. $article->setTitle('My Title');
  71. $article->setCode('The Code');
  72. $this->dm->persist($article);
  73. $this->dm->flush();
  74. $this->dm->clear();
  75. $this->assertEquals($article->getSlug(), 'my-title-the-code-' . ($i + 1));
  76. }
  77. }
  78. private function populate()
  79. {
  80. $qb = $this->dm->createQueryBuilder(self::TEST_ENTITY_CLASS);
  81. $q = $qb->remove()
  82. ->getQuery();
  83. $q->execute();
  84. $art0 = new Article();
  85. $art0->setTitle('My Title');
  86. $art0->setCode('The Code');
  87. $this->dm->persist($art0);
  88. $this->dm->flush();
  89. $this->dm->clear();
  90. }
  91. }