RelativeSlugHandlerDocumentTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Sluggable\Fixture\Document\Handler\Article;
  6. use Sluggable\Fixture\Document\Handler\RelativeSlug;
  7. /**
  8. * These are tests for sluggable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class RelativeSlugHandlerDocumentTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\Document\\Handler\\Article';
  17. const SLUG = 'Sluggable\\Fixture\\Document\\Handler\\RelativeSlug';
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager();
  22. $evm->addEventSubscriber(new SluggableListener);
  23. $this->getMockDocumentManager($evm);
  24. }
  25. public function testSlugGeneration()
  26. {
  27. $this->populate();
  28. $repo = $this->dm->getRepository(self::SLUG);
  29. $thomas = $repo->findOneByTitle('Thomas');
  30. $this->assertEquals('sport-test/thomas', $thomas->getSlug());
  31. $jen = $repo->findOneByTitle('Jen');
  32. $this->assertEquals('sport-test/jen', $jen->getSlug());
  33. $john = $repo->findOneByTitle('John');
  34. $this->assertEquals('cars-code/john', $john->getSlug());
  35. $single = $repo->findOneByTitle('Single');
  36. $this->assertEquals('single', $single->getSlug());
  37. }
  38. public function testUpdateOperations()
  39. {
  40. $this->populate();
  41. $repo = $this->dm->getRepository(self::SLUG);
  42. $thomas = $repo->findOneByTitle('Thomas');
  43. $thomas->setTitle('Ninja');
  44. $this->dm->persist($thomas);
  45. $this->dm->flush();
  46. $this->assertEquals('sport-test/ninja', $thomas->getSlug());
  47. $sport = $this->dm->getRepository(self::ARTICLE)->findOneByTitle('Sport');
  48. $sport->setTitle('Martial Arts');
  49. $this->dm->persist($sport);
  50. $this->dm->flush();
  51. $this->assertEquals('martial-arts-test', $sport->getSlug());
  52. $this->assertEquals('martial-arts-test/ninja', $thomas->getSlug());
  53. $jen = $repo->findOneByTitle('Jen');
  54. $this->assertEquals('martial-arts-test/jen', $jen->getSlug());
  55. $cars = $this->dm->getRepository(self::ARTICLE)->findOneByTitle('Cars');
  56. $jen->setArticle($cars);
  57. $this->dm->persist($jen);
  58. $this->dm->flush();
  59. $this->assertEquals('cars-code/jen', $jen->getSlug());
  60. }
  61. private function populate()
  62. {
  63. $sport = new Article;
  64. $sport->setTitle('Sport');
  65. $sport->setCode('test');
  66. $this->dm->persist($sport);
  67. $cars = new Article;
  68. $cars->setTitle('Cars');
  69. $cars->setCode('code');
  70. $this->dm->persist($cars);
  71. $thomas = new RelativeSlug;
  72. $thomas->setTitle('Thomas');
  73. $thomas->setArticle($sport);
  74. $this->dm->persist($thomas);
  75. $jen = new RelativeSlug;
  76. $jen->setTitle('Jen');
  77. $jen->setArticle($sport);
  78. $this->dm->persist($jen);
  79. $john = new RelativeSlug;
  80. $john->setTitle('John');
  81. $john->setArticle($cars);
  82. $this->dm->persist($john);
  83. $single = new RelativeSlug;
  84. $single->setTitle('Single');
  85. $this->dm->persist($single);
  86. $this->dm->flush();
  87. }
  88. }