SluggableDocumentTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class SluggableDocumentTest extends BaseTestCaseMongoODM
  14. {
  15. const ARTICLE = 'Sluggable\\Fixture\\Document\\Article';
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $evm = new EventManager();
  20. $evm->addEventSubscriber(new SluggableListener);
  21. $this->getMockDocumentManager($evm);
  22. $this->populate();
  23. }
  24. public function testSlugGeneration()
  25. {
  26. // test insert
  27. $repo = $this->dm->getRepository(self::ARTICLE);
  28. $article = $repo->findOneByTitle('My Title');
  29. $this->assertEquals('my-title-the-code', $article->getSlug());
  30. // test update
  31. $article->setTitle('New Title');
  32. $this->dm->persist($article);
  33. $this->dm->flush();
  34. $this->dm->clear();
  35. $article = $repo->findOneByTitle('New Title');
  36. $this->assertEquals('new-title-the-code', $article->getSlug());
  37. }
  38. public function testUniqueSlugGeneration()
  39. {
  40. for ($i = 0; $i < 12; $i++) {
  41. $article = new Article();
  42. $article->setTitle('My Title');
  43. $article->setCode('The Code');
  44. $this->dm->persist($article);
  45. $this->dm->flush();
  46. $this->dm->clear();
  47. $this->assertEquals('my-title-the-code-' . ($i + 1), $article->getSlug());
  48. }
  49. }
  50. public function testGithubIssue57()
  51. {
  52. // slug matched by prefix
  53. $article = new Article;
  54. $article->setTitle('my');
  55. $article->setCode('slug');
  56. $this->dm->persist($article);
  57. $article2 = new Article;
  58. $article2->setTitle('my');
  59. $article2->setCode('s');
  60. $this->dm->persist($article2);
  61. $this->dm->flush();
  62. $this->assertEquals('my-s', $article2->getSlug());
  63. }
  64. private function populate()
  65. {
  66. $art0 = new Article();
  67. $art0->setTitle('My Title');
  68. $art0->setCode('The Code');
  69. $this->dm->persist($art0);
  70. $this->dm->flush();
  71. $this->dm->clear();
  72. }
  73. }