xref: /plugin/struct/_test/NestedResultTest.php (revision f6acb21555da6347e507ae66c2e70a0c506f1bfe)
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
10*f6acb215SAndreas Gohr/**
11*f6acb215SAndreas Gohr * Tests for the NestedResult class
12*f6acb215SAndreas Gohr *
13*f6acb215SAndreas Gohr * @group plugin_struct
14*f6acb215SAndreas Gohr * @group plugins
15*f6acb215SAndreas Gohr *
16*f6acb215SAndreas 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
495bc00e11SAndreas Gohr
505bc00e11SAndreas Gohr    /**
515bc00e11SAndreas Gohr     * Create a result set from a given flat array
525bc00e11SAndreas Gohr     * @param array $rows
535bc00e11SAndreas Gohr     * @return array
545bc00e11SAndreas Gohr     */
555bc00e11SAndreas Gohr    protected function makeResult($rows)
565bc00e11SAndreas Gohr    {
575bc00e11SAndreas Gohr        $result = [];
585bc00e11SAndreas Gohr
595bc00e11SAndreas Gohr        foreach ($rows as $row) {
605bc00e11SAndreas Gohr            $resultRow = [];
615bc00e11SAndreas Gohr            foreach ($row as $cell) {
625bc00e11SAndreas Gohr                $resultRow[] = new Value(
635bc00e11SAndreas Gohr                    new Column(
645bc00e11SAndreas Gohr                        10,
655bc00e11SAndreas Gohr                        new Text(null, '', is_array($cell)),
665bc00e11SAndreas Gohr                        0,
675bc00e11SAndreas Gohr                        true,
685bc00e11SAndreas Gohr                        'test'
695bc00e11SAndreas Gohr                    ),
705bc00e11SAndreas Gohr                    $cell
715bc00e11SAndreas Gohr                );
725bc00e11SAndreas Gohr            }
735bc00e11SAndreas Gohr            $result[] = $resultRow;
745bc00e11SAndreas Gohr        }
755bc00e11SAndreas Gohr
765bc00e11SAndreas Gohr        return $result;
775bc00e11SAndreas Gohr    }
785bc00e11SAndreas Gohr
795bc00e11SAndreas Gohr    /**
805bc00e11SAndreas Gohr     * Don't nest at all
815bc00e11SAndreas Gohr     */
825bc00e11SAndreas Gohr    public function testSimpleZeroLevel()
835bc00e11SAndreas Gohr    {
845bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
855bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
865bc00e11SAndreas Gohr        $root = $nestedResult->getRoot(0);
875bc00e11SAndreas Gohr
885bc00e11SAndreas Gohr        $this->assertCount(0, $root->getChildren(), 'no children expected');
895bc00e11SAndreas Gohr        $this->assertCount(12, $root->getResultRows(), '12 result rows expected');
905bc00e11SAndreas Gohr    }
915bc00e11SAndreas Gohr
925bc00e11SAndreas Gohr
935bc00e11SAndreas Gohr    /**
945bc00e11SAndreas Gohr     * Nest by the first level, no multi values
955bc00e11SAndreas Gohr     */
965bc00e11SAndreas Gohr    public function testSimpleOneLevel()
975bc00e11SAndreas Gohr    {
985bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
995bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1005bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(1)->getChildren();
1015bc00e11SAndreas Gohr
1025bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1035bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1045bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1055bc00e11SAndreas Gohr
1065bc00e11SAndreas Gohr        $this->assertCount(0, $tree[0]->getChildren(), 'no children expected');
1075bc00e11SAndreas Gohr        $this->assertCount(0, $tree[1]->getChildren(), 'no children expected');
1085bc00e11SAndreas Gohr
1095bc00e11SAndreas Gohr        $this->assertCount(6, $tree[0]->getResultRows(), 'result rows');
1105bc00e11SAndreas Gohr        $this->assertCount(6, $tree[1]->getResultRows(), 'result rows');
1115bc00e11SAndreas Gohr
1125bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getResultRows()[0][1]->getValue(), 'Audi 80 expected');
1135bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getResultRows()[0][1]->getValue(), 'Mac Pro 16 expected');
1145bc00e11SAndreas Gohr    }
1155bc00e11SAndreas Gohr
1165bc00e11SAndreas Gohr
1175bc00e11SAndreas Gohr    /**
1185bc00e11SAndreas Gohr     * Nest by two levels, no multi values
1195bc00e11SAndreas Gohr     */
1205bc00e11SAndreas Gohr    public function testSimpleTwoLevels()
1215bc00e11SAndreas Gohr    {
1225bc00e11SAndreas Gohr        $result = $this->makeResult($this->simpleItems);
1235bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1245bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(2)->getChildren();
1255bc00e11SAndreas Gohr
1265bc00e11SAndreas Gohr        $this->assertCount(2, $tree, '2 root nodes expected');
1275bc00e11SAndreas Gohr        $this->assertEquals('car', $tree[0]->getValueObject()->getValue());
1285bc00e11SAndreas Gohr        $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue());
1295bc00e11SAndreas Gohr
1305bc00e11SAndreas Gohr        $this->assertCount(2, $tree[0]->getChildren(), '2 second level nodes expected');
1315bc00e11SAndreas Gohr        $this->assertCount(2, $tree[1]->getChildren(), '2 second level nodes expected');
1325bc00e11SAndreas Gohr
1335bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[0]->getResultRows(), 'result rows');
1345bc00e11SAndreas Gohr        $this->assertCount(3, $tree[0]->getChildren()[1]->getResultRows(), 'result rows');
1355bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[0]->getResultRows(), 'result rows');
1365bc00e11SAndreas Gohr        $this->assertCount(3, $tree[1]->getChildren()[1]->getResultRows(), 'result rows');
1375bc00e11SAndreas Gohr
1385bc00e11SAndreas Gohr
1395bc00e11SAndreas Gohr        $this->assertEquals('a80', $tree[0]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected');
1405bc00e11SAndreas Gohr        $this->assertEquals('pro 16', $tree[1]->getChildren()[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected');
1415bc00e11SAndreas Gohr    }
1425bc00e11SAndreas Gohr
1435bc00e11SAndreas Gohr    public function testMultiTwoLevels()
1445bc00e11SAndreas Gohr    {
1455bc00e11SAndreas Gohr        $result = $this->makeResult($this->multiItems);
1465bc00e11SAndreas Gohr        $nestedResult = new NestedResult($result);
1475bc00e11SAndreas Gohr        $tree = $nestedResult->getRoot(3)->getChildren(); // nest: color, type, brand -> model
1485bc00e11SAndreas Gohr
1495bc00e11SAndreas Gohr        $this->assertCount(6, $tree, '6 root nodes of colors expected');
1505bc00e11SAndreas Gohr
1515bc00e11SAndreas Gohr        // Values on the first level will be multi-values, thus returning arrays
1525bc00e11SAndreas Gohr        $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]);
1535bc00e11SAndreas Gohr        $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]);
1545bc00e11SAndreas Gohr        $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]);
1555bc00e11SAndreas Gohr        $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]);
1565bc00e11SAndreas Gohr        $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]);
1575bc00e11SAndreas Gohr        $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]);
1585bc00e11SAndreas Gohr
1595bc00e11SAndreas Gohr        // Results should now show up under multiple top-level nodes
1605bc00e11SAndreas Gohr        $this->assertEquals('a80',
1615bc00e11SAndreas Gohr            $tree[3] // green
1625bc00e11SAndreas Gohr            ->getChildren()[0] // car
1635bc00e11SAndreas Gohr            ->getChildren()[0] // audi
1645bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
1655bc00e11SAndreas Gohr            ->getValue(),
1665bc00e11SAndreas Gohr            'green car audi a80 expected'
1675bc00e11SAndreas Gohr        );
1685bc00e11SAndreas Gohr        $this->assertEquals('a80',
1695bc00e11SAndreas Gohr            $tree[5] // yellow
1705bc00e11SAndreas Gohr            ->getChildren()[0] // car
1715bc00e11SAndreas Gohr            ->getChildren()[0] // audi
1725bc00e11SAndreas Gohr            ->getResultRows()[0][0] // a80
1735bc00e11SAndreas Gohr            ->getValue(),
1745bc00e11SAndreas Gohr            'yellow car audi a80 expected'
1755bc00e11SAndreas Gohr        );
1765bc00e11SAndreas Gohr    }
1775bc00e11SAndreas Gohr
1785bc00e11SAndreas Gohr}
179