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