MaterializedPathODMMongoDBTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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->getPath());
  57. $this->assertEquals($this->generatePath(array('1', '2')), $category2->getPath());
  58. $this->assertEquals($this->generatePath(array('1', '2', '3')), $category3->getPath());
  59. $this->assertEquals($this->generatePath(array('4')), $category4->getPath());
  60. // Update
  61. $category2->setParent(null);
  62. $this->dm->persist($category2);
  63. $this->dm->flush();
  64. $this->dm->refresh($category);
  65. $this->dm->refresh($category2);
  66. $this->dm->refresh($category3);
  67. $this->assertEquals($this->generatePath(array('1')), $category->getPath());
  68. $this->assertEquals($this->generatePath(array('2')), $category2->getPath());
  69. $this->assertEquals($this->generatePath(array('2', '3')), $category3->getPath());
  70. // Remove
  71. $this->dm->remove($category);
  72. $this->dm->remove($category2);
  73. $this->dm->flush();
  74. $result = $this->dm->createQueryBuilder()->find(self::CATEGORY)->getQuery()->execute();
  75. $this->assertEquals(1, $result->count());
  76. $this->assertEquals('4', $result->getNext()->getTitle());
  77. }
  78. public function createCategory()
  79. {
  80. $class = self::CATEGORY;
  81. return new $class;
  82. }
  83. public function generatePath(array $sources)
  84. {
  85. return implode($this->config['path_separator'], $sources).$this->config['path_separator'];
  86. }
  87. }