xref: /plugin/struct/_test/NestedResultTest.php (revision 5bc00e117f7d6c42d7b1b5918894e63f3f847966)
1*5bc00e11SAndreas Gohr<?php
2*5bc00e11SAndreas Gohr
3*5bc00e11SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
4*5bc00e11SAndreas Gohr
5*5bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
6*5bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\NestedResult;
7*5bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
8*5bc00e11SAndreas Gohruse dokuwiki\plugin\struct\types\Text;
9*5bc00e11SAndreas Gohr
10*5bc00e11SAndreas Gohrclass NestedResultTest extends StructTest
11*5bc00e11SAndreas Gohr{
12*5bc00e11SAndreas Gohr    protected $simpleItems = [
13*5bc00e11SAndreas Gohr        ['car', 'audi', 'a80'],
14*5bc00e11SAndreas Gohr        ['car', 'audi', 'a4'],
15*5bc00e11SAndreas Gohr        ['car', 'audi', 'quattro'],
16*5bc00e11SAndreas Gohr        ['car', 'bmw', 'i3'],
17*5bc00e11SAndreas Gohr        ['car', 'bmw', 'mini'],
18*5bc00e11SAndreas Gohr        ['car', 'bmw', 'z1'],
19*5bc00e11SAndreas Gohr        ['laptop', 'apple', 'pro 16'],
20*5bc00e11SAndreas Gohr        ['laptop', 'apple', 'air'],
21*5bc00e11SAndreas Gohr        ['laptop', 'apple', 'm1'],
22*5bc00e11SAndreas Gohr        ['laptop', 'dell', 'xps'],
23*5bc00e11SAndreas Gohr        ['laptop', 'dell', 'inspiron'],
24*5bc00e11SAndreas Gohr        ['laptop', 'dell', 'latitude'],
25*5bc00e11SAndreas Gohr    ];
26*5bc00e11SAndreas Gohr
27*5bc00e11SAndreas Gohr    protected $multiItems = [
28*5bc00e11SAndreas Gohr        [['green', 'yellow'], 'car', 'audi', 'a80'],
29*5bc00e11SAndreas Gohr        [['yellow', 'blue'], 'car', 'audi', 'a4'],
30*5bc00e11SAndreas Gohr        [['black', 'green'], 'car', 'audi', 'quattro'],
31*5bc00e11SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'i3'],
32*5bc00e11SAndreas Gohr        [['blue', 'gray'], 'car', 'bmw', 'mini'],
33*5bc00e11SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'z1'],
34*5bc00e11SAndreas Gohr        [['green', 'blue'], 'laptop', 'apple', 'pro 16'],
35*5bc00e11SAndreas Gohr        [['red', 'blue'], 'laptop', 'apple', 'air'],
36*5bc00e11SAndreas Gohr        [['black', 'red'], 'laptop', 'apple', 'm1'],
37*5bc00e11SAndreas Gohr        [['gray', 'green'], 'laptop', 'dell', 'xps'],
38*5bc00e11SAndreas Gohr        [['blue', 'yellow'], 'laptop', 'dell', 'inspiron'],
39*5bc00e11SAndreas Gohr        [['gray', 'yellow'], 'laptop', 'dell', 'latitude'],
40*5bc00e11SAndreas Gohr    ];
41*5bc00e11SAndreas Gohr
42*5bc00e11SAndreas Gohr
43*5bc00e11SAndreas Gohr    /**
44*5bc00e11SAndreas Gohr     * Create a result set from a given flat array
45*5bc00e11SAndreas Gohr     * @param array $rows
46*5bc00e11SAndreas Gohr     * @return array
47*5bc00e11SAndreas Gohr     */
48*5bc00e11SAndreas Gohr    protected function makeResult($rows)
49*5bc00e11SAndreas Gohr    {
50*5bc00e11SAndreas Gohr        $result = [];
51*5bc00e11SAndreas Gohr
52*5bc00e11SAndreas Gohr        foreach ($rows as $row) {
53*5bc00e11SAndreas Gohr            $resultRow = [];
54*5bc00e11SAndreas Gohr            foreach ($row as $cell) {
55*5bc00e11SAndreas Gohr                $resultRow[] = new Value(
56*5bc00e11SAndreas Gohr                    new Column(
57*5bc00e11SAndreas Gohr                        10,
58*5bc00e11SAndreas Gohr                        new Text(null, '', is_array($cell)),
59*5bc00e11SAndreas Gohr                        0,
60*5bc00e11SAndreas Gohr                        true,
61*5bc00e11SAndreas Gohr                        'test'
62*5bc00e11SAndreas Gohr                    ),
63*5bc00e11SAndreas Gohr                    $cell
64*5bc00e11SAndreas Gohr                );
65*5bc00e11SAndreas Gohr            }
66*5bc00e11SAndreas Gohr            $result[] = $resultRow;
67*5bc00e11SAndreas Gohr        }
68*5bc00e11SAndreas Gohr
69*5bc00e11SAndreas Gohr        return $result;
70*5bc00e11SAndreas Gohr    }
71*5bc00e11SAndreas Gohr
72*5bc00e11SAndreas Gohr    /**
73*5bc00e11SAndreas Gohr     * Don't nest at all
74*5bc00e11SAndreas Gohr     */
75*5bc00e11SAndreas Gohr    public function testSimpleZeroLevel()
76*5bc00e11SAndreas Gohr    {
77*5bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
78*5bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
79*5bc00e11SAndreas Gohr        $root = $nestedResult->getRoot(0);
80*5bc00e11SAndreas Gohr
81*5bc00e11SAndreas Gohr        $this->assertCount(0, $root->getChildren(), 'no children expected');
82*5bc00e11SAndreas Gohr        $this->assertCount(12, $root->getResultRows(), '12 result rows expected');
83*5bc00e11SAndreas Gohr    }
84*5bc00e11SAndreas Gohr
85*5bc00e11SAndreas Gohr
86*5bc00e11SAndreas Gohr    /**
87*5bc00e11SAndreas Gohr     * Nest by the first level, no multi values
88*5bc00e11SAndreas Gohr     */
89*5bc00e11SAndreas Gohr    public function testSimpleOneLevel()
90*5bc00e11SAndreas Gohr    {
91*5bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
92*5bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
93*5bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(1)->getChildren();
94*5bc00e11SAndreas Gohr
95*5bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
96*5bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
97*5bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
98*5bc00e11SAndreas Gohr
99*5bc00e11SAndreas Gohr        $this->assertCount(0, $tree[0]->getChildren(), 'no children expected');
100*5bc00e11SAndreas Gohr        $this->assertCount(0, $tree[1]->getChildren(), 'no children expected');
101*5bc00e11SAndreas Gohr
102*5bc00e11SAndreas Gohr        $this->assertCount(6, $tree[0]->getResultRows(), 'result rows');
103*5bc00e11SAndreas Gohr        $this->assertCount(6, $tree[1]->getResultRows(), 'result rows');
104*5bc00e11SAndreas Gohr
105*5bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getResultRows()[0][1]->getValue(), 'Audi 80 expected');
106*5bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getResultRows()[0][1]->getValue(), 'Mac Pro 16 expected');
107*5bc00e11SAndreas Gohr    }
108*5bc00e11SAndreas Gohr
109*5bc00e11SAndreas Gohr
110*5bc00e11SAndreas Gohr    /**
111*5bc00e11SAndreas Gohr     * Nest by two levels, no multi values
112*5bc00e11SAndreas Gohr     */
113*5bc00e11SAndreas Gohr    public function testSimpleTwoLevels()
114*5bc00e11SAndreas Gohr    {
115*5bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
116*5bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
117*5bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(2)->getChildren();
118*5bc00e11SAndreas Gohr
119*5bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
120*5bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
121*5bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
122*5bc00e11SAndreas Gohr
123*5bc00e11SAndreas Gohr        $this->assertCount(2, $tree[0]->getChildren(), '2 second level nodes expected');
124*5bc00e11SAndreas Gohr        $this->assertCount(2, $tree[1]->getChildren(), '2 second level nodes expected');
125*5bc00e11SAndreas Gohr
126*5bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[0]->getResultRows(), 'result rows');
127*5bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[1]->getResultRows(), 'result rows');
128*5bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[0]->getResultRows(), 'result rows');
129*5bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[1]->getResultRows(), 'result rows');
130*5bc00e11SAndreas Gohr
131*5bc00e11SAndreas Gohr
132*5bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
133*5bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
134*5bc00e11SAndreas Gohr    }
135*5bc00e11SAndreas Gohr
136*5bc00e11SAndreas Gohr    public function testMultiTwoLevels()
137*5bc00e11SAndreas Gohr    {
138*5bc00e11SAndreas Gohr        $result = $this->makeResult($this->multiItems);
139*5bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
140*5bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(3)->getChildren(); // nest: color, type, brand -> model
141*5bc00e11SAndreas Gohr
142*5bc00e11SAndreas Gohr        $this->assertCount(6, $tree, '6 root nodes of colors expected');
143*5bc00e11SAndreas Gohr
144*5bc00e11SAndreas Gohr        // Values on the first level will be multi-values, thus returning arrays
145*5bc00e11SAndreas Gohr        $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]);
146*5bc00e11SAndreas Gohr        $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]);
147*5bc00e11SAndreas Gohr        $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]);
148*5bc00e11SAndreas Gohr        $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]);
149*5bc00e11SAndreas Gohr        $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]);
150*5bc00e11SAndreas Gohr        $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]);
151*5bc00e11SAndreas Gohr
152*5bc00e11SAndreas Gohr        // Results should now show up under multiple top-level nodes
153*5bc00e11SAndreas Gohr        $this->assertEquals('a80',
154*5bc00e11SAndreas Gohr            $tree[3] // green
155*5bc00e11SAndreas Gohr            ->getChildren()[0] // car
156*5bc00e11SAndreas Gohr            ->getChildren()[0] // audi
157*5bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
158*5bc00e11SAndreas Gohr            ->getValue(),
159*5bc00e11SAndreas Gohr            'green car audi a80 expected'
160*5bc00e11SAndreas Gohr        );
161*5bc00e11SAndreas Gohr        $this->assertEquals('a80',
162*5bc00e11SAndreas Gohr            $tree[5] // yellow
163*5bc00e11SAndreas Gohr            ->getChildren()[0] // car
164*5bc00e11SAndreas Gohr            ->getChildren()[0] // audi
165*5bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
166*5bc00e11SAndreas Gohr            ->getValue(),
167*5bc00e11SAndreas Gohr            'yellow car audi a80 expected'
168*5bc00e11SAndreas Gohr        );
169*5bc00e11SAndreas Gohr    }
170*5bc00e11SAndreas Gohr
171*5bc00e11SAndreas Gohr}
172