MaterializedPathORMFeaturesTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. /**
  6. * These are tests for Tree behavior
  7. *
  8. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  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 MaterializedPathORMFeaturesTest extends BaseTestCaseORM
  14. {
  15. const CATEGORY = "Tree\\Fixture\\MPFeaturesCategory";
  16. protected $config;
  17. protected $listener;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $this->listener = new TreeListener;
  22. $evm = new EventManager;
  23. $evm->addEventSubscriber($this->listener);
  24. $this->getMockSqliteEntityManager($evm);
  25. $meta = $this->em->getClassMetadata(self::CATEGORY);
  26. $this->config = $this->listener->getConfiguration($this->em, $meta->name);
  27. }
  28. /**
  29. * @test
  30. */
  31. function checkPathsAndHash()
  32. {
  33. $category = $this->createCategory();
  34. $category->setTitle('1');
  35. $category2 = $this->createCategory();
  36. $category2->setTitle('2');
  37. $category3 = $this->createCategory();
  38. $category3->setTitle('3');
  39. $category4 = $this->createCategory();
  40. $category4->setTitle('4');
  41. $category2->setParent($category);
  42. $category3->setParent($category2);
  43. $this->em->persist($category4);
  44. $this->em->persist($category3);
  45. $this->em->persist($category2);
  46. $this->em->persist($category);
  47. $this->em->flush();
  48. $this->em->refresh($category);
  49. $this->em->refresh($category2);
  50. $this->em->refresh($category3);
  51. $this->em->refresh($category4);
  52. $this->assertEquals($this->generatePath(array('1' => $category->getId())), $category->getPath());
  53. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId())), $category2->getPath());
  54. $this->assertEquals($this->generatePath(array('1' => $category->getId(), '2' => $category2->getId(), '3' => $category3->getId())), $category3->getPath());
  55. $this->assertEquals($this->generatePath(array('4' => $category4->getId())), $category4->getPath());
  56. $this->assertEquals($this->generatePathHash(array('1' => $category->getId())), $category->getPathHash());
  57. $this->assertEquals($this->generatePathHash(array('1' => $category->getId(), '2' => $category2->getId())), $category2->getPathHash());
  58. $this->assertEquals($this->generatePathHash(array('1' => $category->getId(), '2' => $category2->getId(), '3' => $category3->getId())), $category3->getPathHash());
  59. $this->assertEquals($this->generatePathHash(array('4' => $category4->getId())), $category4->getPathHash());
  60. }
  61. public function createCategory()
  62. {
  63. $class = self::CATEGORY;
  64. return new $class;
  65. }
  66. protected function getUsedEntityFixtures()
  67. {
  68. return array(
  69. self::CATEGORY
  70. );
  71. }
  72. public function generatePath(array $sources)
  73. {
  74. $path = '';
  75. foreach ($sources as $p => $id) {
  76. $path .= $this->config['path_separator'] . $p;
  77. }
  78. return $path;
  79. }
  80. public function generatePathHash(array $sources)
  81. {
  82. return md5($this->generatePath($sources));
  83. }
  84. }