MultiInheritanceWithSingleTableTest.php 4.3 KB

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