xref: /plugin/struct/_test/NestedResultTest.php (revision 1ee0a2dc290e607b00cb9d6d919f4f60a00fcd2d)
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*1ee0a2dcSAndreas Gohr    protected $multiMultiItems = [
50*1ee0a2dcSAndreas Gohr        [['metal', 'wood'], ['green', 'yellow'], 'car', 'audi', 'a80'],
51*1ee0a2dcSAndreas Gohr        [['metal', 'wood', 'plastic'], ['yellow', 'blue'], 'car', 'audi', 'a4'],
52*1ee0a2dcSAndreas Gohr        [['plastic', 'metal'], ['red', 'blue'], 'laptop', 'apple', 'pro 16'],
53*1ee0a2dcSAndreas Gohr        [['metal', 'plastic'], ['black', 'red'], 'laptop', 'apple', 'air'],
54*1ee0a2dcSAndreas Gohr    ];
55*1ee0a2dcSAndreas Gohr
565bc00e11SAndreas Gohr
575bc00e11SAndreas Gohr    /**
585bc00e11SAndreas Gohr     * Create a result set from a given flat array
595bc00e11SAndreas Gohr     * @param array $rows
605bc00e11SAndreas Gohr     * @return array
615bc00e11SAndreas Gohr     */
625bc00e11SAndreas Gohr    protected function makeResult($rows)
635bc00e11SAndreas Gohr    {
645bc00e11SAndreas Gohr        $result = [];
655bc00e11SAndreas Gohr
665bc00e11SAndreas Gohr        foreach ($rows as $row) {
675bc00e11SAndreas Gohr            $resultRow = [];
685bc00e11SAndreas Gohr            foreach ($row as $cell) {
695bc00e11SAndreas Gohr                $resultRow[] = new Value(
705bc00e11SAndreas Gohr                    new Column(
715bc00e11SAndreas Gohr                        10,
725bc00e11SAndreas Gohr                        new Text(null, '', is_array($cell)),
735bc00e11SAndreas Gohr                        0,
745bc00e11SAndreas Gohr                        true,
755bc00e11SAndreas Gohr                        'test'
765bc00e11SAndreas Gohr                    ),
775bc00e11SAndreas Gohr                    $cell
785bc00e11SAndreas Gohr                );
795bc00e11SAndreas Gohr            }
805bc00e11SAndreas Gohr            $result[] = $resultRow;
815bc00e11SAndreas Gohr        }
825bc00e11SAndreas Gohr
835bc00e11SAndreas Gohr        return $result;
845bc00e11SAndreas Gohr    }
855bc00e11SAndreas Gohr
865bc00e11SAndreas Gohr    /**
875bc00e11SAndreas Gohr     * Don't nest at all
885bc00e11SAndreas Gohr     */
895bc00e11SAndreas Gohr    public function testSimpleZeroLevel()
905bc00e11SAndreas Gohr    {
915bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
925bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
935bc00e11SAndreas Gohr        $root = $nestedResult->getRoot(0);
945bc00e11SAndreas Gohr
955bc00e11SAndreas Gohr        $this->assertCount(0, $root->getChildren(), 'no children expected');
965bc00e11SAndreas Gohr        $this->assertCount(12, $root->getResultRows(), '12 result rows expected');
975bc00e11SAndreas Gohr    }
985bc00e11SAndreas Gohr
995bc00e11SAndreas Gohr
1005bc00e11SAndreas Gohr    /**
1015bc00e11SAndreas Gohr     * Nest by the first level, no multi values
1025bc00e11SAndreas Gohr     */
1035bc00e11SAndreas Gohr    public function testSimpleOneLevel()
1045bc00e11SAndreas Gohr    {
1055bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1065bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
107*1ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(1);
108*1ee0a2dcSAndreas Gohr        $tree = $root->getChildren();
1095bc00e11SAndreas Gohr
1105bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1115bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1125bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1135bc00e11SAndreas Gohr
1145bc00e11SAndreas Gohr        $this->assertCount(0, $tree[0]->getChildren(), 'no children expected');
1155bc00e11SAndreas Gohr        $this->assertCount(0, $tree[1]->getChildren(), 'no children expected');
1165bc00e11SAndreas Gohr
1175bc00e11SAndreas Gohr        $this->assertCount(6, $tree[0]->getResultRows(), 'result rows');
1185bc00e11SAndreas Gohr        $this->assertCount(6, $tree[1]->getResultRows(), 'result rows');
1195bc00e11SAndreas Gohr
1205bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getResultRows()[0][1]->getValue(), 'Audi 80 expected');
1215bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getResultRows()[0][1]->getValue(), 'Mac Pro 16 expected');
1225bc00e11SAndreas Gohr    }
1235bc00e11SAndreas Gohr
1245bc00e11SAndreas Gohr
1255bc00e11SAndreas Gohr    /**
1265bc00e11SAndreas Gohr     * Nest by two levels, no multi values
1275bc00e11SAndreas Gohr     */
1285bc00e11SAndreas Gohr    public function testSimpleTwoLevels()
1295bc00e11SAndreas Gohr    {
1305bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1315bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
132*1ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(2);
133*1ee0a2dcSAndreas Gohr        $tree = $root->getChildren();
1345bc00e11SAndreas Gohr
1355bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1365bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1375bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1385bc00e11SAndreas Gohr
1395bc00e11SAndreas Gohr        $this->assertCount(2, $tree[0]->getChildren(), '2 second level nodes expected');
1405bc00e11SAndreas Gohr        $this->assertCount(2, $tree[1]->getChildren(), '2 second level nodes expected');
1415bc00e11SAndreas Gohr
1425bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[0]->getResultRows(), 'result rows');
1435bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[1]->getResultRows(), 'result rows');
1445bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[0]->getResultRows(), 'result rows');
1455bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[1]->getResultRows(), 'result rows');
1465bc00e11SAndreas Gohr
1475bc00e11SAndreas Gohr
1485bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
1495bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
1505bc00e11SAndreas Gohr    }
1515bc00e11SAndreas Gohr
152*1ee0a2dcSAndreas Gohr    /**
153*1ee0a2dcSAndreas Gohr     * Nest by three levels, the first one being multi-value
154*1ee0a2dcSAndreas Gohr     */
155*1ee0a2dcSAndreas Gohr    public function testMultiThreeLevels()
1565bc00e11SAndreas Gohr    {
1575bc00e11SAndreas Gohr        $result = $this->makeResult($this->multiItems);
1585bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
159*1ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(3);
160*1ee0a2dcSAndreas Gohr        $tree = $root->getChildren(); // nest: color, type, brand -> model
1615bc00e11SAndreas Gohr
1625bc00e11SAndreas Gohr        $this->assertCount(6, $tree, '6 root nodes of colors expected');
1635bc00e11SAndreas Gohr
1645bc00e11SAndreas Gohr        // Values on the first level will be multi-values, thus returning arrays
1655bc00e11SAndreas Gohr        $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]);
1665bc00e11SAndreas Gohr        $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]);
1675bc00e11SAndreas Gohr        $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]);
1685bc00e11SAndreas Gohr        $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]);
1695bc00e11SAndreas Gohr        $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]);
1705bc00e11SAndreas Gohr        $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]);
1715bc00e11SAndreas Gohr
1725bc00e11SAndreas Gohr        // Results should now show up under multiple top-level nodes
1735bc00e11SAndreas Gohr        $this->assertEquals('a80',
1745bc00e11SAndreas Gohr            $tree[3] // green
1755bc00e11SAndreas Gohr            ->getChildren()[0] // car
1765bc00e11SAndreas Gohr            ->getChildren()[0] // audi
1775bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
1785bc00e11SAndreas Gohr            ->getValue(),
1795bc00e11SAndreas Gohr            'green car audi a80 expected'
1805bc00e11SAndreas Gohr        );
1815bc00e11SAndreas Gohr        $this->assertEquals('a80',
1825bc00e11SAndreas Gohr            $tree[5] // yellow
1835bc00e11SAndreas Gohr            ->getChildren()[0] // car
1845bc00e11SAndreas Gohr            ->getChildren()[0] // audi
1855bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
1865bc00e11SAndreas Gohr            ->getValue(),
1875bc00e11SAndreas Gohr            'yellow car audi a80 expected'
1885bc00e11SAndreas Gohr        );
1895bc00e11SAndreas Gohr    }
1905bc00e11SAndreas Gohr
191*1ee0a2dcSAndreas Gohr    /**
192*1ee0a2dcSAndreas Gohr     * Nest by two multi value levels
193*1ee0a2dcSAndreas Gohr     */
194*1ee0a2dcSAndreas Gohr    public function testMultiMultiTwoLevels()
195*1ee0a2dcSAndreas Gohr    {
196*1ee0a2dcSAndreas Gohr        $result = $this->makeResult($this->multiMultiItems);
197*1ee0a2dcSAndreas Gohr        $nestedResult = new NestedResult($result);
198*1ee0a2dcSAndreas Gohr        $root = $nestedResult->getRoot(2);
199*1ee0a2dcSAndreas Gohr        $tree = $root->getChildren(); // nest: material, color, *
200*1ee0a2dcSAndreas Gohr
201*1ee0a2dcSAndreas Gohr        $this->assertCount(3, $tree, '3 root nodes of material expected');
202*1ee0a2dcSAndreas Gohr        $this->assertCount(1, $tree[0]->getChildren()[0]->getResultRows(), '1 metal black row expected');
203*1ee0a2dcSAndreas Gohr    }
2045bc00e11SAndreas Gohr}
205