MultiInheritanceWithSingleTableTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\Transport\Car;
  7. use Tree\Fixture\Transport\Bus;
  8. use Tree\Fixture\Transport\Vehicle;
  9. use Tree\Fixture\Transport\Engine;
  10. /**
  11. * These are tests for Tree behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class MultiInheritanceWithSingleTableTest extends BaseTestCaseORM
  19. {
  20. const CAR = "Tree\Fixture\Transport\Car";
  21. const BUS = "Tree\Fixture\Transport\Bus";
  22. const VEHICLE = "Tree\Fixture\Transport\Vehicle";
  23. const ENGINE = "Tree\Fixture\Transport\Engine";
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $evm->addEventSubscriber(new TreeListener);
  29. $this->getMockSqliteEntityManager($evm);
  30. }
  31. public function testConsistence()
  32. {
  33. $this->populate();
  34. $carRepo = $this->em->getRepository(self::CAR);
  35. $audi = $carRepo->findOneByTitle('Audi-80');
  36. $this->assertEquals(2, $carRepo->childCount($audi));
  37. $this->assertEquals(1, $audi->getLeft());
  38. $this->assertEquals(6, $audi->getRight());
  39. $children = $carRepo->children($audi);
  40. $this->assertCount(2, $children);
  41. $path = $carRepo->getPath($children[0]);
  42. $this->assertCount(2, $path);
  43. $carRepo->moveDown($children[0]);
  44. $this->assertEquals(4, $children[0]->getLeft());
  45. $this->assertEquals(5, $children[0]->getRight());
  46. $this->assertTrue($carRepo->verify());
  47. }
  48. /*public function testHeavyLoad()
  49. {
  50. $carRepo = $this->em->getRepository(self::CAR);
  51. $parent = null;
  52. $num = 100;
  53. for($i = 0; $i < 100; $i++) {
  54. $engine = new Engine;
  55. $engine->setType('e'.$i);
  56. $engine->setValves(8);
  57. $this->em->persist($engine);
  58. $car = new Car;
  59. $car->setParent($parent);
  60. $car->setTitle('car'.$i);
  61. $car->setEngine($engine);
  62. $this->em->persist($car);
  63. // siblings
  64. $rnd = rand(0, 3);
  65. for ($j = 0; $j < $rnd; $j++) {
  66. $siblingEngine = new Engine;
  67. $siblingEngine->setType('e'.$i.$j);
  68. $siblingEngine->setValves(8);
  69. $this->em->persist($siblingEngine);
  70. $siblingCar = new Car;
  71. $siblingCar->setTitle('car'.$i.$j);
  72. $siblingCar->setEngine($siblingEngine);
  73. $siblingCar->setParent($car);
  74. $this->em->persist($siblingCar);
  75. }
  76. $num += $rnd;
  77. $parent = $car;
  78. }
  79. $this->em->flush();
  80. $this->assertTrue($carRepo->verify());
  81. var_dump('processed: '.$num);
  82. }*/
  83. protected function getUsedEntityFixtures()
  84. {
  85. return array(
  86. self::VEHICLE,
  87. self::CAR,
  88. self::ENGINE,
  89. self::BUS
  90. );
  91. }
  92. private function populate()
  93. {
  94. // engines
  95. $v8 = new Engine;
  96. $v8->setType('V8');
  97. $v8->setValves(8);
  98. $this->em->persist($v8);
  99. $v6 = new Engine;
  100. $v6->setType('V6');
  101. $v6->setValves(8);
  102. $this->em->persist($v6);
  103. $vb8 = new Engine;
  104. $vb8->setType('VB8');
  105. $vb8->setValves(8);
  106. $this->em->persist($vb8);
  107. $jet = new Engine;
  108. $jet->setType('Jet');
  109. $jet->setValves(16);
  110. $this->em->persist($jet);
  111. // cars
  112. $audi80 = new Car;
  113. $audi80->setEngine($v6);
  114. $audi80->setTitle('Audi-80');
  115. $this->em->persist($audi80);
  116. $audi80s = new Car;
  117. $audi80s->setParent($audi80);
  118. $audi80s->setTitle('Audi-80s');
  119. $audi80s->setEngine($v8);
  120. $this->em->persist($audi80s);
  121. $icarus = new Bus;
  122. $icarus->setEngine($vb8);
  123. $icarus->setTitle('Icarus');
  124. $this->em->persist($icarus);
  125. $audiJet = new Car;
  126. $audiJet->setParent($audi80);
  127. $audiJet->setTitle('Audi-jet');
  128. $audiJet->setEngine($jet);
  129. $this->em->persist($audiJet);
  130. $this->em->flush();
  131. $this->em->flush();
  132. }
  133. }