MaterializedPathORMFeaturesTest.php 3.3KB

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