SluggableDocumentTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Sluggable\Fixture\Document\Article;
  6. /**
  7. * These are tests for sluggable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Sluggable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class SluggableDocumentTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\Document\\Article';
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $evm = new EventManager();
  21. $evm->addEventSubscriber(new SluggableListener);
  22. $this->getMockDocumentManager($evm);
  23. $this->populate();
  24. }
  25. public function testSlugGeneration()
  26. {
  27. // test insert
  28. $repo = $this->dm->getRepository(self::ARTICLE);
  29. $article = $repo->findOneByTitle('My Title');
  30. $this->assertEquals('my-title-the-code', $article->getSlug());
  31. // test update
  32. $article->setTitle('New Title');
  33. $this->dm->persist($article);
  34. $this->dm->flush();
  35. $this->dm->clear();
  36. $article = $repo->findOneByTitle('New Title');
  37. $this->assertEquals('new-title-the-code', $article->getSlug());
  38. }
  39. public function testUniqueSlugGeneration()
  40. {
  41. for ($i = 0; $i < 12; $i++) {
  42. $article = new Article();
  43. $article->setTitle('My Title');
  44. $article->setCode('The Code');
  45. $this->dm->persist($article);
  46. $this->dm->flush();
  47. $this->dm->clear();
  48. $this->assertEquals($article->getSlug(), 'my-title-the-code-' . ($i + 1));
  49. }
  50. }
  51. private function populate()
  52. {
  53. $art0 = new Article();
  54. $art0->setTitle('My Title');
  55. $art0->setCode('The Code');
  56. $this->dm->persist($art0);
  57. $this->dm->flush();
  58. $this->dm->clear();
  59. }
  60. }