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', 'inspiron'], 305bc00e11SAndreas Gohr ['laptop', 'dell', 'latitude'], 31*ae522a2dSAndreas Gohr ['laptop', 'bmw', '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 497b7a9290SAndreas Gohr protected $multiHoleItems = [ 507b7a9290SAndreas Gohr [['green', 'yellow'], 'car', 'audi', 'a80'], 517b7a9290SAndreas Gohr [[], 'car', 'audi', 'a4'], 527b7a9290SAndreas Gohr [['black', 'green'], '', 'audi', 'quattro'], 537b7a9290SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'i3'], 547b7a9290SAndreas Gohr [['blue', 'gray'], 'car', 'bmw', 'mini'], 557b7a9290SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'z1'], 567b7a9290SAndreas Gohr [['green', 'blue'], 'laptop', 'apple', 'pro 16'], 577b7a9290SAndreas Gohr [['red', 'blue'], 'laptop', 'apple', 'air'], 587b7a9290SAndreas Gohr [['black', 'red'], 'laptop', 'apple', 'm1'], 597b7a9290SAndreas Gohr [[], 'laptop', 'dell', 'xps'], 607b7a9290SAndreas Gohr [['blue', 'yellow'], '', 'dell', 'inspiron'], 617b7a9290SAndreas Gohr [['gray', 'yellow'], 'laptop', 'dell', 'latitude'], 627b7a9290SAndreas Gohr ]; 637b7a9290SAndreas 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 1101f53b3d5SAndreas Gohr $this->assertCount(0, $root->getChildren(true), '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); 1231f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 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 1291f53b3d5SAndreas Gohr $this->assertCount(0, $tree[0]->getChildren(true), 'no children expected'); 1301f53b3d5SAndreas Gohr $this->assertCount(0, $tree[1]->getChildren(true), '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); 1481f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 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 154*ae522a2dSAndreas Gohr $this->assertCount(2, $tree[0]->getChildren(true), '2 car brands expected'); 155*ae522a2dSAndreas Gohr $this->assertCount(3, $tree[1]->getChildren(true), '3 laptop brands expected'); 1565bc00e11SAndreas Gohr 157*ae522a2dSAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true)[0]->getResultRows(), '3 audis expected'); 158*ae522a2dSAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true)[1]->getResultRows(), '3 bmw expected'); 159*ae522a2dSAndreas Gohr $this->assertCount(3, $tree[1]->getChildren(true)[0]->getResultRows(), '3 apple expected'); 160*ae522a2dSAndreas Gohr $this->assertCount(1, $tree[1]->getChildren(true)[1]->getResultRows(), '1 bmw expected'); 161*ae522a2dSAndreas Gohr $this->assertCount(2, $tree[1]->getChildren(true)[2]->getResultRows(), '2 dell expected'); 1625bc00e11SAndreas Gohr 1635bc00e11SAndreas Gohr 1641f53b3d5SAndreas Gohr $this->assertEquals('a80', $tree[0]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected'); 1651f53b3d5SAndreas Gohr $this->assertEquals('pro 16', $tree[1]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected'); 1665bc00e11SAndreas Gohr } 1675bc00e11SAndreas Gohr 1681ee0a2dcSAndreas Gohr /** 1691ee0a2dcSAndreas Gohr * Nest by three levels, the first one being multi-value 1701ee0a2dcSAndreas Gohr */ 1711ee0a2dcSAndreas Gohr public function testMultiThreeLevels() 1725bc00e11SAndreas Gohr { 1735bc00e11SAndreas Gohr $result = $this->makeResult($this->multiItems); 1745bc00e11SAndreas Gohr $nestedResult = new NestedResult($result); 1751ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(3); 1761f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: color, type, brand -> model 1775bc00e11SAndreas Gohr 1785bc00e11SAndreas Gohr $this->assertCount(6, $tree, '6 root nodes of colors expected'); 1795bc00e11SAndreas Gohr 1805bc00e11SAndreas Gohr // Values on the first level will be multi-values, thus returning arrays 1815bc00e11SAndreas Gohr $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]); 1825bc00e11SAndreas Gohr $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]); 1835bc00e11SAndreas Gohr $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]); 1845bc00e11SAndreas Gohr $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]); 1855bc00e11SAndreas Gohr $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]); 1865bc00e11SAndreas Gohr $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]); 1875bc00e11SAndreas Gohr 1885bc00e11SAndreas Gohr // Results should now show up under multiple top-level nodes 1895bc00e11SAndreas Gohr $this->assertEquals('a80', 1905bc00e11SAndreas Gohr $tree[3] // green 1911f53b3d5SAndreas Gohr ->getChildren(true)[0] // car 1921f53b3d5SAndreas Gohr ->getChildren(true)[0] // audi 1935bc00e11SAndreas Gohr ->getResultRows()[0][0] // a80 1945bc00e11SAndreas Gohr ->getValue(), 1955bc00e11SAndreas Gohr 'green car audi a80 expected' 1965bc00e11SAndreas Gohr ); 1975bc00e11SAndreas Gohr $this->assertEquals('a80', 1985bc00e11SAndreas Gohr $tree[5] // yellow 1991f53b3d5SAndreas Gohr ->getChildren(true)[0] // car 2001f53b3d5SAndreas Gohr ->getChildren(true)[0] // audi 2015bc00e11SAndreas Gohr ->getResultRows()[0][0] // a80 2025bc00e11SAndreas Gohr ->getValue(), 2035bc00e11SAndreas Gohr 'yellow car audi a80 expected' 2045bc00e11SAndreas Gohr ); 2055bc00e11SAndreas Gohr } 2065bc00e11SAndreas Gohr 2077b7a9290SAndreas Gohr public function testMultiHoles() 2087b7a9290SAndreas Gohr { 2097b7a9290SAndreas Gohr $result = $this->makeResult($this->multiHoleItems); 2107b7a9290SAndreas Gohr $nestedResult = new NestedResult($result); 2117b7a9290SAndreas Gohr $root = $nestedResult->getRoot(3); 2121f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: color, type, brand -> model 2137b7a9290SAndreas Gohr $this->assertCount(7, $tree, '6 root nodes of colors + 1 n/a expected'); // should have one n/a node 2141f53b3d5SAndreas Gohr $this->assertCount(2, $tree[6]->getChildren(true), 'top n/a node should have car, laptop'); 2151f53b3d5SAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true), 'black should have car,laptop,n/a'); 2167b7a9290SAndreas Gohr } 2177b7a9290SAndreas Gohr 2181ee0a2dcSAndreas Gohr /** 2191ee0a2dcSAndreas Gohr * Nest by two multi value levels 2201ee0a2dcSAndreas Gohr */ 2211ee0a2dcSAndreas Gohr public function testMultiMultiTwoLevels() 2221ee0a2dcSAndreas Gohr { 2231ee0a2dcSAndreas Gohr $result = $this->makeResult($this->multiMultiItems); 2241ee0a2dcSAndreas Gohr $nestedResult = new NestedResult($result); 2251ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(2); 2261f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: material, color, * 2271ee0a2dcSAndreas Gohr 2281ee0a2dcSAndreas Gohr $this->assertCount(3, $tree, '3 root nodes of material expected'); 2291f53b3d5SAndreas Gohr $this->assertCount(1, $tree[0]->getChildren(true)[0]->getResultRows(), '1 metal black row expected'); 2301ee0a2dcSAndreas Gohr } 231ce44c639SAndreas Gohr 232ce44c639SAndreas Gohr /** 233ce44c639SAndreas Gohr * Nest by two multi value levels with indexing 234ce44c639SAndreas Gohr */ 235ce44c639SAndreas Gohr public function testMultiMultiTwoLevelsIndex() 236ce44c639SAndreas Gohr { 237ce44c639SAndreas Gohr $result = $this->makeResult($this->multiMultiItems); 238ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 239ce44c639SAndreas Gohr $root = $nestedResult->getRoot(2, 1); 2401f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: index, material, color, * 241ce44c639SAndreas Gohr 242ce44c639SAndreas Gohr $this->assertCount(3, $tree, '3 root index nodes expected'); 243ce44c639SAndreas Gohr $this->assertEquals('M', $tree[0]->getValueObject()->getValue(), 'M expected'); 2441f53b3d5SAndreas Gohr $this->assertCount(1, $tree[0]->getChildren(true), '1 metal sub node under M expected'); 245ce44c639SAndreas Gohr } 246ce44c639SAndreas Gohr 247ce44c639SAndreas Gohr /** 248ce44c639SAndreas Gohr * Index a flat result with no multi values 249ce44c639SAndreas Gohr */ 250ce44c639SAndreas Gohr public function testSimpleIndex() 251ce44c639SAndreas Gohr { 252ce44c639SAndreas Gohr $result = $this->makeResult($this->simpleItems); 253ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 254ce44c639SAndreas Gohr $root = $nestedResult->getRoot(0, 2); 2551f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 256ce44c639SAndreas Gohr 257ce44c639SAndreas Gohr $this->assertCount(2, $tree, '2 root index nodes expected'); 258ce44c639SAndreas Gohr $this->assertEquals('CA', $tree[0]->getValueObject()->getValue(), 'CA(r) index expected'); 259ce44c639SAndreas Gohr $this->assertEquals('LA', $tree[1]->getValueObject()->getValue(), 'LA(ptop) index expected'); 260ce44c639SAndreas Gohr 261ce44c639SAndreas Gohr $this->assertCount(6, $tree[0]->getResultRows(), '6 rows under CA expected'); 262ce44c639SAndreas Gohr } 263ce44c639SAndreas Gohr 264ce44c639SAndreas Gohr 265ce44c639SAndreas Gohr /** 266ce44c639SAndreas Gohr * Index a flat result with multi values 267ce44c639SAndreas Gohr */ 268ce44c639SAndreas Gohr public function testMultiIndex() 269ce44c639SAndreas Gohr { 270ce44c639SAndreas Gohr $result = $this->makeResult($this->multiItems); 271ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 272ce44c639SAndreas Gohr $root = $nestedResult->getRoot(0, 2); 2731f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 274ce44c639SAndreas Gohr 275ce44c639SAndreas Gohr $this->assertCount(4, $tree, '4 root index nodes expected'); 276ce44c639SAndreas Gohr $this->assertEquals('BL', $tree[0]->getValueObject()->getValue(), 'BL(ack|blue) index expected'); 277ce44c639SAndreas Gohr 278ce44c639SAndreas Gohr $this->assertCount(4, $tree[0]->getResultRows(), '4 rows under BL expected'); 279ce44c639SAndreas Gohr } 2805bc00e11SAndreas Gohr} 281