MaterializedPathODMMongoDBTest.php 4.5KB

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