|
14 | 14 | use Symfony\Component\Config\Definition\PrototypedArrayNode;
|
15 | 15 | use Symfony\Component\Config\Definition\ArrayNode;
|
16 | 16 | use Symfony\Component\Config\Definition\ScalarNode;
|
| 17 | +use Symfony\Component\Config\Definition\VariableNode; |
17 | 18 |
|
18 | 19 | class PrototypedArrayNodeTest extends \PHPUnit_Framework_TestCase
|
19 | 20 | {
|
@@ -177,4 +178,166 @@ protected function getPrototypeNodeWithDefaultChildren()
|
177 | 178 |
|
178 | 179 | return $node;
|
179 | 180 | }
|
| 181 | + |
| 182 | + /** |
| 183 | + * Tests that when a key attribute is mapped, that key is removed from the array. |
| 184 | + * And if only 'value' element is left in the array, it will replace its wrapper array. |
| 185 | + * |
| 186 | + * <things> |
| 187 | + * <option id="option1" value="value1"> |
| 188 | + * </things> |
| 189 | + * |
| 190 | + * The above should finally be mapped to an array that looks like this |
| 191 | + * (because "id" is the key attribute). |
| 192 | + * |
| 193 | + * array( |
| 194 | + * 'things' => array( |
| 195 | + * 'option1' => 'value1' |
| 196 | + * ) |
| 197 | + * ) |
| 198 | + * |
| 199 | + * It's also possible to mix 'value-only' and 'non-value-only' elements in the array. |
| 200 | + * |
| 201 | + * <things> |
| 202 | + * <option id="option1" value="value1"> |
| 203 | + * <option id="option2" value="value2" foo="foo2"> |
| 204 | + * </things> |
| 205 | + * |
| 206 | + * The above should finally be mapped to an array as follows |
| 207 | + * |
| 208 | + * array( |
| 209 | + * 'things' => array( |
| 210 | + * 'option1' => 'value1', |
| 211 | + * 'option2' => array( |
| 212 | + * 'value' => 'value2', |
| 213 | + * 'foo' => 'foo2' |
| 214 | + * ) |
| 215 | + * ) |
| 216 | + * ) |
| 217 | + * |
| 218 | + * The 'value' element can also be ArrayNode: |
| 219 | + * |
| 220 | + * <things> |
| 221 | + * <option id="option1"> |
| 222 | + * <value> |
| 223 | + * <foo>foo1</foo> |
| 224 | + * <bar>bar1</bar> |
| 225 | + * </value> |
| 226 | + * </option> |
| 227 | + * </things> |
| 228 | + * |
| 229 | + * The above should be finally be mapped to an array as follows |
| 230 | + * |
| 231 | + * array( |
| 232 | + * 'things' => array( |
| 233 | + * 'option1' => array( |
| 234 | + * 'foo' => 'foo1', |
| 235 | + * 'bar' => 'bar1' |
| 236 | + * ) |
| 237 | + * ) |
| 238 | + * ) |
| 239 | + * |
| 240 | + * If using VariableNode for value node, it's also possible to mix different types of value nodes: |
| 241 | + * |
| 242 | + * <things> |
| 243 | + * <option id="option1"> |
| 244 | + * <value> |
| 245 | + * <foo>foo1</foo> |
| 246 | + * <bar>bar1</bar> |
| 247 | + * </value> |
| 248 | + * </option> |
| 249 | + * <option id="option2" value="value2"> |
| 250 | + * </things> |
| 251 | + * |
| 252 | + * The above should be finally mapped to an array as follows |
| 253 | + * |
| 254 | + * array( |
| 255 | + * 'things' => array( |
| 256 | + * 'option1' => array( |
| 257 | + * 'foo' => 'foo1', |
| 258 | + * 'bar' => 'bar1' |
| 259 | + * ), |
| 260 | + * 'option2' => 'value2' |
| 261 | + * ) |
| 262 | + * ) |
| 263 | + * |
| 264 | + * |
| 265 | + * @dataProvider getDataForKeyRemovedLeftValueOnly |
| 266 | + */ |
| 267 | + public function testMappedAttributeKeyIsRemovedLeftValueOnly($value, $children, $expected) |
| 268 | + { |
| 269 | + $node = new PrototypedArrayNode('root'); |
| 270 | + $node->setKeyAttribute('id', true); |
| 271 | + |
| 272 | + // each item under the root is an array, with one scalar item |
| 273 | + $prototype = new ArrayNode(null, $node); |
| 274 | + $prototype->addChild(new ScalarNode('id')); |
| 275 | + $prototype->addChild(new ScalarNode('foo')); |
| 276 | + $prototype->addChild($value); |
| 277 | + $node->setPrototype($prototype); |
| 278 | + |
| 279 | + $normalized = $node->normalize($children); |
| 280 | + $this->assertEquals($expected, $normalized); |
| 281 | + } |
| 282 | + |
| 283 | + public function getDataForKeyRemovedLeftValueOnly() |
| 284 | + { |
| 285 | + $scalarValue = new ScalarNode('value'); |
| 286 | + |
| 287 | + $arrayValue = new ArrayNode('value'); |
| 288 | + $arrayValue->addChild(new ScalarNode('foo')); |
| 289 | + $arrayValue->addChild(new ScalarNode('bar')); |
| 290 | + |
| 291 | + $variableValue = new VariableNode('value'); |
| 292 | + |
| 293 | + return array( |
| 294 | + |
| 295 | + array( |
| 296 | + $scalarValue, |
| 297 | + array( |
| 298 | + array('id' => 'option1', 'value' => 'value1'), |
| 299 | + ), |
| 300 | + array('option1' => 'value1'), |
| 301 | + ), |
| 302 | + |
| 303 | + array( |
| 304 | + $scalarValue, |
| 305 | + array( |
| 306 | + array('id' => 'option1', 'value' => 'value1'), |
| 307 | + array('id' => 'option2', 'value' => 'value2', 'foo' => 'foo2'), |
| 308 | + ), |
| 309 | + array( |
| 310 | + 'option1' => 'value1', |
| 311 | + 'option2' => array('value' => 'value2', 'foo' => 'foo2'), |
| 312 | + ), |
| 313 | + ), |
| 314 | + |
| 315 | + array( |
| 316 | + $arrayValue, |
| 317 | + array( |
| 318 | + array( |
| 319 | + 'id' => 'option1', |
| 320 | + 'value' => array('foo' => 'foo1', 'bar' => 'bar1'), |
| 321 | + ), |
| 322 | + ), |
| 323 | + array( |
| 324 | + 'option1' => array('foo' => 'foo1', 'bar' => 'bar1'), |
| 325 | + ), |
| 326 | + ), |
| 327 | + |
| 328 | + array($variableValue, |
| 329 | + array( |
| 330 | + array( |
| 331 | + 'id' => 'option1', 'value' => array('foo' => 'foo1', 'bar' => 'bar1'), |
| 332 | + ), |
| 333 | + array('id' => 'option2', 'value' => 'value2'), |
| 334 | + ), |
| 335 | + array( |
| 336 | + 'option1' => array('foo' => 'foo1', 'bar' => 'bar1'), |
| 337 | + 'option2' => 'value2', |
| 338 | + ), |
| 339 | + ), |
| 340 | + |
| 341 | + ); |
| 342 | + } |
180 | 343 | }
|
0 commit comments