Skip to content

Commit 562c8c9

Browse files
committed
[Tree] Write simple test for tree object hydrator
1 parent 81d7146 commit 562c8c9

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

tests/Gedmo/Tree/Fixture/RootCategory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ public function getLevel()
105105
{
106106
return $this->level;
107107
}
108+
109+
public function getChildren()
110+
{
111+
return $this->children;
112+
}
113+
114+
public function setChildren($children)
115+
{
116+
$this->children = $children;
117+
}
108118
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Gedmo\Tree;
4+
5+
use Doctrine\Common\EventManager;
6+
use Doctrine\DBAL\Logging\DebugStack;
7+
use Doctrine\ORM\Query;
8+
use Tool\BaseTestCaseORM;
9+
use Tree\Fixture\RootCategory;
10+
11+
/**
12+
* Tests the tree object hydrator
13+
*
14+
* @author Ilija Tovilo <[email protected]>
15+
* @link http://www.gediminasm.org
16+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
17+
*/
18+
class TreeObjectHydratorTest extends BaseTestCaseORM
19+
{
20+
const CATEGORY = "Tree\\Fixture\\Category";
21+
const ROOT_CATEGORY = "Tree\\Fixture\\RootCategory";
22+
23+
protected function setUp()
24+
{
25+
parent::setUp();
26+
27+
$evm = new EventManager();
28+
$evm->addEventSubscriber(new TreeListener());
29+
30+
$this->getMockSqliteEntityManager($evm);
31+
32+
$this->em->getConfiguration()->addCustomHydrationMode('tree', 'Gedmo\Tree\Hydrator\ORM\TreeObjectHydrator');
33+
}
34+
35+
public function testFullTreeHydration()
36+
{
37+
$this->populate();
38+
39+
$stack = new DebugStack();
40+
$this->em->getConfiguration()->setSQLLogger($stack);
41+
42+
$repo = $this->em->getRepository(self::ROOT_CATEGORY);
43+
44+
$result = $repo->createQueryBuilder('node')
45+
->getQuery()
46+
->setHint(Query::HINT_INCLUDE_META_COLUMNS, true)
47+
->getResult('tree');
48+
49+
$this->assertEquals(count($result), 1);
50+
51+
$food = $result[0];
52+
$this->assertEquals($food->getTitle(), 'Food');
53+
$this->assertEquals(count($food->getChildren()), 4);
54+
55+
$fruits = $food->getChildren()->get(0);
56+
$this->assertEquals($fruits->getTitle(), 'Fruits');
57+
$this->assertEquals(count($fruits->getChildren()), 2);
58+
59+
$vegetables = $food->getChildren()->get(1);
60+
$this->assertEquals($vegetables->getTitle(), 'Vegetables');
61+
$this->assertEquals(count($vegetables->getChildren()), 0);
62+
63+
$milk = $food->getChildren()->get(2);
64+
$this->assertEquals($milk->getTitle(), 'Milk');
65+
$this->assertEquals(count($milk->getChildren()), 0);
66+
67+
$meat = $food->getChildren()->get(3);
68+
$this->assertEquals($meat->getTitle(), 'Meat');
69+
$this->assertEquals(count($meat->getChildren()), 0);
70+
71+
$oranges = $fruits->getChildren()->get(0);
72+
$this->assertEquals($oranges->getTitle(), 'Oranges');
73+
$this->assertEquals(count($oranges->getChildren()), 0);
74+
75+
$citrons = $fruits->getChildren()->get(1);
76+
$this->assertEquals($citrons->getTitle(), 'Citrons');
77+
$this->assertEquals(count($citrons->getChildren()), 0);
78+
79+
// Make sure only one query was executed
80+
$this->assertEquals(count($stack->queries), 1);
81+
}
82+
83+
private function populate()
84+
{
85+
$repo = $this->em->getRepository(self::ROOT_CATEGORY);
86+
87+
$food = new RootCategory();
88+
$food->setTitle('Food');
89+
90+
$fruits = new RootCategory();
91+
$fruits->setTitle('Fruits');
92+
93+
$vegetables = new RootCategory();
94+
$vegetables->setTitle('Vegetables');
95+
96+
$milk = new RootCategory();
97+
$milk->setTitle('Milk');
98+
99+
$meat = new RootCategory();
100+
$meat->setTitle('Meat');
101+
102+
$oranges = new RootCategory();
103+
$oranges->setTitle('Oranges');
104+
105+
$citrons = new RootCategory();
106+
$citrons->setTitle('Citrons');
107+
108+
$repo
109+
->persistAsFirstChild($food)
110+
->persistAsFirstChildOf($fruits, $food)
111+
->persistAsFirstChildOf($vegetables, $food)
112+
->persistAsLastChildOf($milk, $food)
113+
->persistAsLastChildOf($meat, $food)
114+
->persistAsFirstChildOf($oranges, $fruits)
115+
->persistAsFirstChildOf($citrons, $fruits);
116+
117+
$this->em->flush();
118+
}
119+
120+
protected function getUsedEntityFixtures()
121+
{
122+
return array(
123+
self::CATEGORY,
124+
self::ROOT_CATEGORY,
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)