SluggableDocumentTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(
  39. new \Doctrine\MongoDB\Connection(),
  40. $config,
  41. $evm
  42. );
  43. }
  44. public function testSluggable()
  45. {
  46. $art0 = new Article();
  47. $art0->setTitle('hello');
  48. $art0->setCode('code');
  49. $this->dm->persist($art0);
  50. $this->dm->flush();
  51. $this->assertEquals('hello-code', $art0->getSlug());
  52. $repo = $this->dm->getRepository(self::TEST_ENTITY_CLASS);
  53. $art = $repo->findOneByTitle('hello');
  54. $art->setTitle('New Title');
  55. $this->dm->persist($art);
  56. $this->dm->flush();
  57. $art = $repo->findOneByTitle('New Title');
  58. $this->assertEquals('new-title-code', $art->getSlug());
  59. }
  60. }