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