TreeSlugHandlerDocumentTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Sluggable\Fixture\Document\Handler\TreeSlug;
  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 TreeSlugHandlerDocumentTest extends BaseTestCaseMongoODM
  14. {
  15. const SLUG = 'Sluggable\\Fixture\\Document\\Handler\\TreeSlug';
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $evm = new EventManager();
  20. $evm->addEventSubscriber(new SluggableListener);
  21. $this->getMockDocumentManager($evm);
  22. }
  23. public function testSlugGeneration()
  24. {
  25. $this->populate();
  26. $repo = $this->dm->getRepository(self::SLUG);
  27. $food = $repo->findOneByTitle('Food');
  28. $this->assertEquals('food', $food->getSlug());
  29. $fruits = $repo->findOneByTitle('Fruits');
  30. $this->assertEquals('food/fruits', $fruits->getSlug());
  31. $oranges = $repo->findOneByTitle('Oranges');
  32. $this->assertEquals('food/fruits/oranges', $oranges->getSlug());
  33. $citrons = $repo->findOneByTitle('Citrons');
  34. $this->assertEquals('food/fruits/citrons', $citrons->getSlug());
  35. }
  36. public function testSlugUpdates()
  37. {
  38. $this->populate();
  39. $repo = $this->dm->getRepository(self::SLUG);
  40. $fruits = $repo->findOneByTitle('Fruits');
  41. $fruits->setTitle('Fructis');
  42. $this->dm->persist($fruits);
  43. $this->dm->flush();
  44. $this->assertEquals('food/fructis', $fruits->getSlug());
  45. $oranges = $repo->findOneByTitle('Oranges');
  46. $this->assertEquals('food/fructis/oranges', $oranges->getSlug());
  47. $citrons = $repo->findOneByTitle('Citrons');
  48. $this->assertEquals('food/fructis/citrons', $citrons->getSlug());
  49. $food = $repo->findOneByTitle('Food');
  50. $food->setTitle('Foodissimo');
  51. $this->dm->persist($food);
  52. $this->dm->flush();
  53. $this->assertEquals('foodissimo', $food->getSlug());
  54. $this->assertEquals('foodissimo/fructis/oranges', $oranges->getSlug());
  55. $this->assertEquals('foodissimo/fructis/citrons', $citrons->getSlug());
  56. }
  57. private function populate()
  58. {
  59. $food = new TreeSlug;
  60. $food->setTitle('Food');
  61. $this->dm->persist($food);
  62. $fruits = new TreeSlug;
  63. $fruits->setTitle('Fruits');
  64. $fruits->setParent($food);
  65. $this->dm->persist($fruits);
  66. $vegitables = new TreeSlug;
  67. $vegitables->setTitle('Vegitables');
  68. $vegitables->setParent($food);
  69. $this->dm->persist($vegitables);
  70. $milk = new TreeSlug;
  71. $milk->setTitle('Milk');
  72. $milk->setParent($food);
  73. $this->dm->persist($milk);
  74. $meat = new TreeSlug;
  75. $meat->setTitle('Meat');
  76. $meat->setParent($food);
  77. $this->dm->persist($meat);
  78. $oranges = new TreeSlug;
  79. $oranges->setTitle('Oranges');
  80. $oranges->setParent($fruits);
  81. $this->dm->persist($oranges);
  82. $citrons = new TreeSlug;
  83. $citrons->setTitle('Citrons');
  84. $citrons->setParent($fruits);
  85. $this->dm->persist($citrons);
  86. $this->dm->flush();
  87. }
  88. }