xref: /plugin/struct/_test/NestedResultTest.php (revision 7b7a9290f8a0cb5d24bd768166706412896cffe5)
15bc00e11SAndreas Gohr<?php
25bc00e11SAndreas Gohr
35bc00e11SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
45bc00e11SAndreas Gohr
55bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
65bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\NestedResult;
75bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
85bc00e11SAndreas Gohruse dokuwiki\plugin\struct\types\Text;
95bc00e11SAndreas Gohr
10f6acb215SAndreas Gohr/**
11f6acb215SAndreas Gohr * Tests for the NestedResult class
12f6acb215SAndreas Gohr *
13f6acb215SAndreas Gohr * @group plugin_struct
14f6acb215SAndreas Gohr * @group plugins
15f6acb215SAndreas Gohr *
16f6acb215SAndreas Gohr */
175bc00e11SAndreas Gohrclass NestedResultTest extends StructTest
185bc00e11SAndreas Gohr{
195bc00e11SAndreas Gohr    protected $simpleItems = [
205bc00e11SAndreas Gohr        ['car', 'audi', 'a80'],
215bc00e11SAndreas Gohr        ['car', 'audi', 'a4'],
225bc00e11SAndreas Gohr        ['car', 'audi', 'quattro'],
235bc00e11SAndreas Gohr        ['car', 'bmw', 'i3'],
245bc00e11SAndreas Gohr        ['car', 'bmw', 'mini'],
255bc00e11SAndreas Gohr        ['car', 'bmw', 'z1'],
265bc00e11SAndreas Gohr        ['laptop', 'apple', 'pro 16'],
275bc00e11SAndreas Gohr        ['laptop', 'apple', 'air'],
285bc00e11SAndreas Gohr        ['laptop', 'apple', 'm1'],
295bc00e11SAndreas Gohr        ['laptop', 'dell', 'xps'],
305bc00e11SAndreas Gohr        ['laptop', 'dell', 'inspiron'],
315bc00e11SAndreas Gohr        ['laptop', 'dell', 'latitude'],
325bc00e11SAndreas Gohr    ];
335bc00e11SAndreas Gohr
345bc00e11SAndreas Gohr    protected $multiItems = [
355bc00e11SAndreas Gohr        [['green', 'yellow'], 'car', 'audi', 'a80'],
365bc00e11SAndreas Gohr        [['yellow', 'blue'], 'car', 'audi', 'a4'],
375bc00e11SAndreas Gohr        [['black', 'green'], 'car', 'audi', 'quattro'],
385bc00e11SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'i3'],
395bc00e11SAndreas Gohr        [['blue', 'gray'], 'car', 'bmw', 'mini'],
405bc00e11SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'z1'],
415bc00e11SAndreas Gohr        [['green', 'blue'], 'laptop', 'apple', 'pro 16'],
425bc00e11SAndreas Gohr        [['red', 'blue'], 'laptop', 'apple', 'air'],
435bc00e11SAndreas Gohr        [['black', 'red'], 'laptop', 'apple', 'm1'],
445bc00e11SAndreas Gohr        [['gray', 'green'], 'laptop', 'dell', 'xps'],
455bc00e11SAndreas Gohr        [['blue', 'yellow'], 'laptop', 'dell', 'inspiron'],
465bc00e11SAndreas Gohr        [['gray', 'yellow'], 'laptop', 'dell', 'latitude'],
475bc00e11SAndreas Gohr    ];
485bc00e11SAndreas Gohr
49*7b7a9290SAndreas Gohr    protected $multiHoleItems = [
50*7b7a9290SAndreas Gohr        [['green', 'yellow'], 'car', 'audi', 'a80'],
51*7b7a9290SAndreas Gohr        [[], 'car', 'audi', 'a4'],
52*7b7a9290SAndreas Gohr        [['black', 'green'], '', 'audi', 'quattro'],
53*7b7a9290SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'i3'],
54*7b7a9290SAndreas Gohr        [['blue', 'gray'], 'car', 'bmw', 'mini'],
55*7b7a9290SAndreas Gohr        [['red', 'black'], 'car', 'bmw', 'z1'],
56*7b7a9290SAndreas Gohr        [['green', 'blue'], 'laptop', 'apple', 'pro 16'],
57*7b7a9290SAndreas Gohr        [['red', 'blue'], 'laptop', 'apple', 'air'],
58*7b7a9290SAndreas Gohr        [['black', 'red'], 'laptop', 'apple', 'm1'],
59*7b7a9290SAndreas Gohr        [[], 'laptop', 'dell', 'xps'],
60*7b7a9290SAndreas Gohr        [['blue', 'yellow'], '', 'dell', 'inspiron'],
61*7b7a9290SAndreas Gohr        [['gray', 'yellow'], 'laptop', 'dell', 'latitude'],
62*7b7a9290SAndreas Gohr    ];
63*7b7a9290SAndreas Gohr
641ee0a2dcSAndreas Gohr    protected $multiMultiItems = [
651ee0a2dcSAndreas Gohr        [['metal', 'wood'], ['green', 'yellow'], 'car', 'audi', 'a80'],
661ee0a2dcSAndreas Gohr        [['metal', 'wood', 'plastic'], ['yellow', 'blue'], 'car', 'audi', 'a4'],
671ee0a2dcSAndreas Gohr        [['plastic', 'metal'], ['red', 'blue'], 'laptop', 'apple', 'pro 16'],
681ee0a2dcSAndreas Gohr        [['metal', 'plastic'], ['black', 'red'], 'laptop', 'apple', 'air'],
691ee0a2dcSAndreas Gohr    ];
701ee0a2dcSAndreas Gohr
715bc00e11SAndreas Gohr
725bc00e11SAndreas Gohr    /**
735bc00e11SAndreas Gohr     * Create a result set from a given flat array
745bc00e11SAndreas Gohr     * @param array $rows
755bc00e11SAndreas Gohr     * @return array
765bc00e11SAndreas Gohr     */
775bc00e11SAndreas Gohr    protected function makeResult($rows)
785bc00e11SAndreas Gohr    {
795bc00e11SAndreas Gohr        $result = [];
805bc00e11SAndreas Gohr
815bc00e11SAndreas Gohr        foreach ($rows as $row) {
825bc00e11SAndreas Gohr            $resultRow = [];
835bc00e11SAndreas Gohr            foreach ($row as $cell) {
845bc00e11SAndreas Gohr                $resultRow[] = new Value(
855bc00e11SAndreas Gohr                    new Column(
865bc00e11SAndreas Gohr                        10,
875bc00e11SAndreas Gohr                        new Text(null, '', is_array($cell)),
885bc00e11SAndreas Gohr                        0,
895bc00e11SAndreas Gohr                        true,
905bc00e11SAndreas Gohr                        'test'
915bc00e11SAndreas Gohr                    ),
925bc00e11SAndreas Gohr                    $cell
935bc00e11SAndreas Gohr                );
945bc00e11SAndreas Gohr            }
955bc00e11SAndreas Gohr            $result[] = $resultRow;
965bc00e11SAndreas Gohr        }
975bc00e11SAndreas Gohr
985bc00e11SAndreas Gohr        return $result;
995bc00e11SAndreas Gohr    }
1005bc00e11SAndreas Gohr
1015bc00e11SAndreas Gohr    /**
1025bc00e11SAndreas Gohr     * Don't nest at all
1035bc00e11SAndreas Gohr     */
1045bc00e11SAndreas Gohr    public function testSimpleZeroLevel()
1055bc00e11SAndreas Gohr    {
1065bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1075bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1085bc00e11SAndreas Gohr        $root = $nestedResult->getRoot(0);
1095bc00e11SAndreas Gohr
1105bc00e11SAndreas Gohr        $this->assertCount(0, $root->getChildren(), 'no children expected');
1115bc00e11SAndreas Gohr        $this->assertCount(12, $root->getResultRows(), '12 result rows expected');
1125bc00e11SAndreas Gohr    }
1135bc00e11SAndreas Gohr
1145bc00e11SAndreas Gohr
1155bc00e11SAndreas Gohr    /**
1165bc00e11SAndreas Gohr     * Nest by the first level, no multi values
1175bc00e11SAndreas Gohr     */
1185bc00e11SAndreas Gohr    public function testSimpleOneLevel()
1195bc00e11SAndreas Gohr    {
1205bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1215bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1221ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(1);
1231ee0a2dcSAndreas Gohr        $tree = $root->getChildren();
1245bc00e11SAndreas Gohr
1255bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1265bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1275bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1285bc00e11SAndreas Gohr
1295bc00e11SAndreas Gohr        $this->assertCount(0, $tree[0]->getChildren(), 'no children expected');
1305bc00e11SAndreas Gohr        $this->assertCount(0, $tree[1]->getChildren(), 'no children expected');
1315bc00e11SAndreas Gohr
1325bc00e11SAndreas Gohr        $this->assertCount(6, $tree[0]->getResultRows(), 'result rows');
1335bc00e11SAndreas Gohr        $this->assertCount(6, $tree[1]->getResultRows(), 'result rows');
1345bc00e11SAndreas Gohr
1355bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getResultRows()[0][1]->getValue(), 'Audi 80 expected');
1365bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getResultRows()[0][1]->getValue(), 'Mac Pro 16 expected');
1375bc00e11SAndreas Gohr    }
1385bc00e11SAndreas Gohr
1395bc00e11SAndreas Gohr
1405bc00e11SAndreas Gohr    /**
1415bc00e11SAndreas Gohr     * Nest by two levels, no multi values
1425bc00e11SAndreas Gohr     */
1435bc00e11SAndreas Gohr    public function testSimpleTwoLevels()
1445bc00e11SAndreas Gohr    {
1455bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1465bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1471ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(2);
1481ee0a2dcSAndreas Gohr        $tree = $root->getChildren();
1495bc00e11SAndreas Gohr
1505bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1515bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1525bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1535bc00e11SAndreas Gohr
1545bc00e11SAndreas Gohr        $this->assertCount(2, $tree[0]->getChildren(), '2 second level nodes expected');
1555bc00e11SAndreas Gohr        $this->assertCount(2, $tree[1]->getChildren(), '2 second level nodes expected');
1565bc00e11SAndreas Gohr
1575bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[0]->getResultRows(), 'result rows');
1585bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[1]->getResultRows(), 'result rows');
1595bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[0]->getResultRows(), 'result rows');
1605bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[1]->getResultRows(), 'result rows');
1615bc00e11SAndreas Gohr
1625bc00e11SAndreas Gohr
1635bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
1645bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
1655bc00e11SAndreas Gohr    }
1665bc00e11SAndreas Gohr
1671ee0a2dcSAndreas Gohr    /**
1681ee0a2dcSAndreas Gohr     * Nest by three levels, the first one being multi-value
1691ee0a2dcSAndreas Gohr     */
1701ee0a2dcSAndreas Gohr    public function testMultiThreeLevels()
1715bc00e11SAndreas Gohr    {
1725bc00e11SAndreas Gohr        $result = $this->makeResult($this->multiItems);
1735bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1741ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(3);
1751ee0a2dcSAndreas Gohr        $tree = $root->getChildren(); // nest: color, type, brand -> model
1765bc00e11SAndreas Gohr
1775bc00e11SAndreas Gohr        $this->assertCount(6, $tree, '6 root nodes of colors expected');
1785bc00e11SAndreas Gohr
1795bc00e11SAndreas Gohr        // Values on the first level will be multi-values, thus returning arrays
1805bc00e11SAndreas Gohr        $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]);
1815bc00e11SAndreas Gohr        $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]);
1825bc00e11SAndreas Gohr        $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]);
1835bc00e11SAndreas Gohr        $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]);
1845bc00e11SAndreas Gohr        $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]);
1855bc00e11SAndreas Gohr        $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]);
1865bc00e11SAndreas Gohr
1875bc00e11SAndreas Gohr        // Results should now show up under multiple top-level nodes
1885bc00e11SAndreas Gohr        $this->assertEquals('a80',
1895bc00e11SAndreas Gohr            $tree[3] // green
1905bc00e11SAndreas Gohr            ->getChildren()[0] // car
1915bc00e11SAndreas Gohr            ->getChildren()[0] // audi
1925bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
1935bc00e11SAndreas Gohr            ->getValue(),
1945bc00e11SAndreas Gohr            'green car audi a80 expected'
1955bc00e11SAndreas Gohr        );
1965bc00e11SAndreas Gohr        $this->assertEquals('a80',
1975bc00e11SAndreas Gohr            $tree[5] // yellow
1985bc00e11SAndreas Gohr            ->getChildren()[0] // car
1995bc00e11SAndreas Gohr            ->getChildren()[0] // audi
2005bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
2015bc00e11SAndreas Gohr            ->getValue(),
2025bc00e11SAndreas Gohr            'yellow car audi a80 expected'
2035bc00e11SAndreas Gohr        );
2045bc00e11SAndreas Gohr    }
2055bc00e11SAndreas Gohr
206*7b7a9290SAndreas Gohr    public function testMultiHoles()
207*7b7a9290SAndreas Gohr    {
208*7b7a9290SAndreas Gohr        $result = $this->makeResult($this->multiHoleItems);
209*7b7a9290SAndreas Gohr        $nestedResult = new NestedResult($result);
210*7b7a9290SAndreas Gohr        $root = $nestedResult->getRoot(3);
211*7b7a9290SAndreas Gohr        $tree = $root->getChildren(); // nest: color, type, brand -> model
212*7b7a9290SAndreas Gohr        $this->assertCount(7, $tree, '6 root nodes of colors + 1 n/a expected');  // should have one n/a node
213*7b7a9290SAndreas Gohr        $this->assertCount(2, $tree[6]->getChildren(), 'top n/a node should have car, laptop');
214*7b7a9290SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren(), 'black should have car,laptop,n/a');
215*7b7a9290SAndreas Gohr    }
216*7b7a9290SAndreas Gohr
2171ee0a2dcSAndreas Gohr    /**
2181ee0a2dcSAndreas Gohr     * Nest by two multi value levels
2191ee0a2dcSAndreas Gohr     */
2201ee0a2dcSAndreas Gohr    public function testMultiMultiTwoLevels()
2211ee0a2dcSAndreas Gohr    {
2221ee0a2dcSAndreas Gohr        $result = $this->makeResult($this->multiMultiItems);
2231ee0a2dcSAndreas Gohr        $nestedResult = new NestedResult($result);
2241ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(2);
2251ee0a2dcSAndreas Gohr        $tree = $root->getChildren(); // nest: material, color, *
2261ee0a2dcSAndreas Gohr
2271ee0a2dcSAndreas Gohr        $this->assertCount(3, $tree, '3 root nodes of material expected');
2281ee0a2dcSAndreas Gohr        $this->assertCount(1, $tree[0]->getChildren()[0]->getResultRows(), '1 metal black row expected');
2291ee0a2dcSAndreas Gohr    }
230ce44c639SAndreas Gohr
231ce44c639SAndreas Gohr    /**
232ce44c639SAndreas Gohr     * Nest by two multi value levels with indexing
233ce44c639SAndreas Gohr     */
234ce44c639SAndreas Gohr    public function testMultiMultiTwoLevelsIndex()
235ce44c639SAndreas Gohr    {
236ce44c639SAndreas Gohr        $result = $this->makeResult($this->multiMultiItems);
237ce44c639SAndreas Gohr        $nestedResult = new NestedResult($result);
238ce44c639SAndreas Gohr        $root = $nestedResult->getRoot(2, 1);
239ce44c639SAndreas Gohr        $tree = $root->getChildren(); // nest: index, material, color, *
240ce44c639SAndreas Gohr
241ce44c639SAndreas Gohr        $this->assertCount(3, $tree, '3 root index nodes  expected');
242ce44c639SAndreas Gohr        $this->assertEquals('M', $tree[0]->getValueObject()->getValue(), 'M expected');
243ce44c639SAndreas Gohr        $this->assertCount(1, $tree[0]->getChildren(), '1 metal sub node under M expected');
244ce44c639SAndreas Gohr    }
245ce44c639SAndreas Gohr
246ce44c639SAndreas Gohr    /**
247ce44c639SAndreas Gohr     * Index a flat result with no multi values
248ce44c639SAndreas Gohr     */
249ce44c639SAndreas Gohr    public function testSimpleIndex()
250ce44c639SAndreas Gohr    {
251ce44c639SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
252ce44c639SAndreas Gohr        $nestedResult = new NestedResult($result);
253ce44c639SAndreas Gohr        $root = $nestedResult->getRoot(0, 2);
254ce44c639SAndreas Gohr        $tree = $root->getChildren();
255ce44c639SAndreas Gohr
256ce44c639SAndreas Gohr        $this->assertCount(2, $tree, '2 root index nodes  expected');
257ce44c639SAndreas Gohr        $this->assertEquals('CA', $tree[0]->getValueObject()->getValue(), 'CA(r) index expected');
258ce44c639SAndreas Gohr        $this->assertEquals('LA', $tree[1]->getValueObject()->getValue(), 'LA(ptop) index expected');
259ce44c639SAndreas Gohr
260ce44c639SAndreas Gohr        $this->assertCount(6, $tree[0]->getResultRows(), '6 rows under CA expected');
261ce44c639SAndreas Gohr    }
262ce44c639SAndreas Gohr
263ce44c639SAndreas Gohr
264ce44c639SAndreas Gohr    /**
265ce44c639SAndreas Gohr     * Index a flat result with multi values
266ce44c639SAndreas Gohr     */
267ce44c639SAndreas Gohr    public function testMultiIndex()
268ce44c639SAndreas Gohr    {
269ce44c639SAndreas Gohr        $result = $this->makeResult($this->multiItems);
270ce44c639SAndreas Gohr        $nestedResult = new NestedResult($result);
271ce44c639SAndreas Gohr        $root = $nestedResult->getRoot(0, 2);
272ce44c639SAndreas Gohr        $tree = $root->getChildren();
273ce44c639SAndreas Gohr
274ce44c639SAndreas Gohr        $this->assertCount(4, $tree, '4 root index nodes  expected');
275ce44c639SAndreas Gohr        $this->assertEquals('BL', $tree[0]->getValueObject()->getValue(), 'BL(ack|blue) index expected');
276ce44c639SAndreas Gohr
277ce44c639SAndreas Gohr        $this->assertCount(4, $tree[0]->getResultRows(), '4 rows under BL expected');
278ce44c639SAndreas Gohr    }
2795bc00e11SAndreas Gohr}
280