SluggableDocumentTest.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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('my-title-the-code-' . ($i + 1), $article->getSlug());
  49. }
  50. }
  51. public function testGithubIssue57()
  52. {
  53. // slug matched by prefix
  54. $article = new Article;
  55. $article->setTitle('my');
  56. $article->setCode('slug');
  57. $this->dm->persist($article);
  58. $article2 = new Article;
  59. $article2->setTitle('my');
  60. $article2->setCode('s');
  61. $this->dm->persist($article2);
  62. $this->dm->flush();
  63. $this->assertEquals('my-s', $article2->getSlug());
  64. }
  65. private function populate()
  66. {
  67. $art0 = new Article();
  68. $art0->setTitle('My Title');
  69. $art0->setCode('The Code');
  70. $this->dm->persist($art0);
  71. $this->dm->flush();
  72. $this->dm->clear();
  73. }
  74. }