MaterializedPathODMMongoDBTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseMongoODM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\RootCategory;
  7. /**
  8. * These are tests for Tree behavior
  9. *
  10. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class MaterializedPathODMMongoDBTest extends BaseTestCaseMongoODM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Document\\Category";
  18. protected $config;
  19. protected $listener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->listener = new TreeListener;
  24. $evm = new EventManager;
  25. $evm->addEventSubscriber($this->listener);
  26. $this->getMockDocumentManager($evm);
  27. $meta = $this->dm->getClassMetadata(self::CATEGORY);
  28. $this->config = $this->listener->getConfiguration($this->dm, $meta->name);
  29. }
  30. /**
  31. * @test
  32. */
  33. function insertUpdateAndRemove()
  34. {
  35. // Insert
  36. $category = $this->createCategory();
  37. $category->setTitle('1');
  38. $category2 = $this->createCategory();
  39. $category2->setTitle('2');
  40. $category3 = $this->createCategory();
  41. $category3->setTitle('3');
  42. $category4 = $this->createCategory();
  43. $category4->setTitle('4');
  44. $category2->setParent($category);
  45. $category3->setParent($category2);
  46. $this->dm->persist($category4);
  47. $this->dm->persist($category3);
  48. $this->dm->persist($category2);
  49. $this->dm->persist($category);
  50. $this->dm->flush();
  51. $this->dm->refresh($category);
  52. $this->dm->refresh($category2);
  53. $this->dm->refresh($category3);
  54. $this->dm->refresh($category4);
  55. $this->assertEquals($this->generatePath(array('1' => $category->getId())), $category->getPath());
  56. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId())), $category2->getPath());
  57. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId(), '3' => $category3->getId())), $category3->getPath());
  58. $this->assertEquals($this->generatePath(array('4' => $category4->getId())), $category4->getPath());
  59. $this->assertEquals(1, $category->getLevel());
  60. $this->assertEquals(2, $category2->getLevel());
  61. $this->assertEquals(3, $category3->getLevel());
  62. $this->assertEquals(1, $category4->getLevel());
  63. // Update
  64. $category2->setParent(null);
  65. $this->dm->persist($category2);
  66. $this->dm->flush();
  67. $this->dm->refresh($category);
  68. $this->dm->refresh($category2);
  69. $this->dm->refresh($category3);
  70. $this->assertEquals($this->generatePath(array('1' => $category->getId())), $category->getPath());
  71. $this->assertEquals($this->generatePath(array('2' => $category2->getId())), $category2->getPath());
  72. $this->assertEquals($this->generatePath(array('2' => $category2->getId(), '3' => $category3->getId())), $category3->getPath());
  73. $this->assertEquals(1, $category->getLevel());
  74. $this->assertEquals(1, $category2->getLevel());
  75. $this->assertEquals(2, $category3->getLevel());
  76. $this->assertEquals(1, $category4->getLevel());
  77. // Remove
  78. $this->dm->remove($category);
  79. $this->dm->remove($category2);
  80. $this->dm->flush();
  81. $result = $this->dm->createQueryBuilder()->find(self::CATEGORY)->getQuery()->execute();
  82. $firstResult = $result->getNext();
  83. $this->assertEquals(1, $result->count());
  84. $this->assertEquals('4', $firstResult->getTitle());
  85. $this->assertEquals(1, $firstResult->getLevel());
  86. }
  87. /**
  88. * @test
  89. */
  90. public function useOfSeparatorInPathSourceShouldThrowAnException()
  91. {
  92. $this->setExpectedException('Gedmo\Exception\RuntimeException');
  93. $category = $this->createCategory();
  94. $category->setTitle('1'.$this->config['path_separator']);
  95. $this->dm->persist($category);
  96. $this->dm->flush();
  97. }
  98. public function createCategory()
  99. {
  100. $class = self::CATEGORY;
  101. return new $class;
  102. }
  103. public function generatePath(array $sources)
  104. {
  105. $path = '';
  106. foreach ($sources as $p => $id) {
  107. $path .= $p.'-'.$id.$this->config['path_separator'];
  108. }
  109. return $path;
  110. }
  111. }