| Total Complexity | 4 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | class UtilitiesTest extends UnitTestCase |
||
| 14 | { |
||
| 15 | public function testFlatToNested() |
||
| 16 | { |
||
| 17 | $flatTree = array( |
||
| 18 | array( |
||
| 19 | 'id' => '1', |
||
| 20 | 'parent' => null, |
||
| 21 | 'level' => 0, |
||
| 22 | ), |
||
| 23 | array( |
||
| 24 | 'id' => '1.1', |
||
| 25 | 'parent' => '1', |
||
| 26 | 'level' => 1, |
||
| 27 | ), |
||
| 28 | array( |
||
| 29 | 'id' => '1.2', |
||
| 30 | 'parent' => '1', |
||
| 31 | 'level' => 1, |
||
| 32 | ), |
||
| 33 | ); |
||
| 34 | $result = Utilities::flatToNested($flatTree); |
||
| 35 | |||
| 36 | $this->assertEquals( |
||
| 37 | array( |
||
| 38 | array( |
||
| 39 | 'id' => '1', |
||
| 40 | 'parent' => null, |
||
| 41 | 'level' => 0, |
||
| 42 | '_children' => array( |
||
| 43 | array( |
||
| 44 | 'id' => '1.1', |
||
| 45 | 'parent' => '1', |
||
| 46 | 'level' => 1, |
||
| 47 | '_children' => array(), |
||
| 48 | ), |
||
| 49 | array( |
||
| 50 | 'id' => '1.2', |
||
| 51 | 'parent' => '1', |
||
| 52 | 'level' => 1, |
||
| 53 | '_children' => array(), |
||
| 54 | ), |
||
| 55 | ), |
||
| 56 | ), |
||
| 57 | ), |
||
| 58 | $result |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testFlatToNestedMultiRootNode() |
||
| 63 | { |
||
| 64 | $flatTree = array( |
||
| 65 | array( |
||
| 66 | 'id' => '1', |
||
| 67 | 'parent' => null, |
||
| 68 | 'level' => 1, |
||
| 69 | ), |
||
| 70 | array( |
||
| 71 | 'id' => '1.2', |
||
| 72 | 'parent' => '1', |
||
| 73 | 'level' => 2, |
||
| 74 | ), |
||
| 75 | array( |
||
| 76 | 'id' => '2', |
||
| 77 | 'parent' => null, |
||
| 78 | 'level' => 1, |
||
| 79 | ), |
||
| 80 | array( |
||
| 81 | 'id' => '2.1', |
||
| 82 | 'parent' => '2', |
||
| 83 | 'level' => 2, |
||
| 84 | ), |
||
| 85 | ); |
||
| 86 | $result = Utilities::flatToNested($flatTree); |
||
| 87 | |||
| 88 | $this->assertEquals( |
||
| 89 | array( |
||
| 90 | array( |
||
| 91 | 'id' => '1', |
||
| 92 | 'parent' => null, |
||
| 93 | 'level' => 1, |
||
| 94 | '_children' => array( |
||
| 95 | array( |
||
| 96 | 'id' => '1.2', |
||
| 97 | 'parent' => '1', |
||
| 98 | 'level' => 2, |
||
| 99 | '_children' => array(), |
||
| 100 | ), |
||
| 101 | ), |
||
| 102 | ), |
||
| 103 | array( |
||
| 104 | 'id' => '2', |
||
| 105 | 'parent' => null, |
||
| 106 | 'level' => 1, |
||
| 107 | '_children' => array( |
||
| 108 | array( |
||
| 109 | 'id' => '2.1', |
||
| 110 | 'parent' => '2', |
||
| 111 | 'level' => 2, |
||
| 112 | '_children' => array(), |
||
| 113 | ), |
||
| 114 | ), |
||
| 115 | ), |
||
| 116 | ), |
||
| 117 | $result |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testFlatToNestedComplexTree() |
||
| 122 | { |
||
| 123 | $flatTree = array( |
||
| 124 | array( |
||
| 125 | 'id' => '1', |
||
| 126 | 'parent' => null, |
||
| 127 | 'level' => 1, |
||
| 128 | ), |
||
| 129 | array( |
||
| 130 | 'id' => '1.2', |
||
| 131 | 'parent' => '1', |
||
| 132 | 'level' => 2, |
||
| 133 | ), |
||
| 134 | array( |
||
| 135 | 'id' => '1.2.1', |
||
| 136 | 'parent' => '1.2', |
||
| 137 | 'level' => 3, |
||
| 138 | ), |
||
| 139 | array( |
||
| 140 | 'id' => '1.2.1.1', |
||
| 141 | 'parent' => '1.2.1', |
||
| 142 | 'level' => 4, |
||
| 143 | ), |
||
| 144 | array( |
||
| 145 | 'id' => '2.1', |
||
| 146 | 'parent' => '1', |
||
| 147 | 'level' => 2, |
||
| 148 | ), |
||
| 149 | array( |
||
| 150 | 'id' => '2.1.1', |
||
| 151 | 'parent' => '2.1', |
||
| 152 | 'level' => 3, |
||
| 153 | ), |
||
| 154 | array( |
||
| 155 | 'id' => '2.1.2', |
||
| 156 | 'parent' => '2.1', |
||
| 157 | 'level' => 3, |
||
| 158 | ), |
||
| 159 | array( |
||
| 160 | 'id' => '2.1.3', |
||
| 161 | 'parent' => '2.1', |
||
| 162 | 'level' => 3, |
||
| 163 | ), |
||
| 164 | array( |
||
| 165 | 'id' => '2', |
||
| 166 | 'parent' => null, |
||
| 167 | 'level' => 1, |
||
| 168 | ), |
||
| 169 | array( |
||
| 170 | 'id' => '2.1', |
||
| 171 | 'parent' => '2', |
||
| 172 | 'level' => 2, |
||
| 173 | ), |
||
| 174 | array( |
||
| 175 | 'id' => '2.1.1', |
||
| 176 | 'parent' => '2.1', |
||
| 177 | 'level' => 3, |
||
| 178 | ), |
||
| 179 | array( |
||
| 180 | 'id' => '2.2', |
||
| 181 | 'parent' => '2', |
||
| 182 | 'level' => 2, |
||
| 183 | ), |
||
| 184 | ); |
||
| 185 | $result = Utilities::flatToNested($flatTree); |
||
| 186 | |||
| 187 | $this->assertEquals( |
||
| 188 | array( |
||
| 189 | array( |
||
| 190 | 'id' => '1', |
||
| 191 | 'parent' => null, |
||
| 192 | 'level' => 1, |
||
| 193 | '_children' => array( |
||
| 194 | array( |
||
| 195 | 'id' => '1.2', |
||
| 196 | 'parent' => '1', |
||
| 197 | 'level' => 2, |
||
| 198 | '_children' => array( |
||
| 199 | array( |
||
| 200 | 'id' => '1.2.1', |
||
| 201 | 'parent' => '1.2', |
||
| 202 | 'level' => 3, |
||
| 203 | '_children' => array( |
||
| 204 | array( |
||
| 205 | 'id' => '1.2.1.1', |
||
| 206 | 'parent' => '1.2.1', |
||
| 207 | 'level' => 4, |
||
| 208 | '_children' => array(), |
||
| 209 | ), |
||
| 210 | ), |
||
| 211 | ), |
||
| 212 | ), |
||
| 213 | ), |
||
| 214 | array( |
||
| 215 | 'id' => '2.1', |
||
| 216 | 'parent' => '1', |
||
| 217 | 'level' => 2, |
||
| 218 | '_children' => array( |
||
| 219 | array( |
||
| 220 | 'id' => '2.1.1', |
||
| 221 | 'parent' => '2.1', |
||
| 222 | 'level' => 3, |
||
| 223 | '_children' => array(), |
||
| 224 | ), |
||
| 225 | array( |
||
| 226 | 'id' => '2.1.2', |
||
| 227 | 'parent' => '2.1', |
||
| 228 | 'level' => 3, |
||
| 229 | '_children' => array(), |
||
| 230 | ), |
||
| 231 | array( |
||
| 232 | 'id' => '2.1.3', |
||
| 233 | 'parent' => '2.1', |
||
| 234 | 'level' => 3, |
||
| 235 | '_children' => array(), |
||
| 236 | ), |
||
| 237 | ), |
||
| 238 | ), |
||
| 239 | ), |
||
| 240 | ), |
||
| 241 | array( |
||
| 242 | 'id' => '2', |
||
| 243 | 'parent' => null, |
||
| 244 | 'level' => 1, |
||
| 245 | '_children' => array( |
||
| 246 | array( |
||
| 247 | 'id' => '2.1', |
||
| 248 | 'parent' => '2', |
||
| 249 | 'level' => 2, |
||
| 250 | '_children' => array( |
||
| 251 | array( |
||
| 252 | 'id' => '2.1.1', |
||
| 253 | 'parent' => '2.1', |
||
| 254 | 'level' => 3, |
||
| 255 | '_children' => array(), |
||
| 256 | ), |
||
| 257 | ), |
||
| 258 | ), |
||
| 259 | array( |
||
| 260 | 'id' => '2.2', |
||
| 261 | 'parent' => '2', |
||
| 262 | 'level' => 2, |
||
| 263 | '_children' => array(), |
||
| 264 | ), |
||
| 265 | ), |
||
| 266 | ), |
||
| 267 | ), |
||
| 268 | $result |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function testFlatToNestedChangeLevelName() |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 |