/** * Tests the opposite of the testMappedAttributeKeyIsRemoved because * the removal can be toggled with an option. */ public function testMappedAttributeKeyNotRemoved() { $node = new PrototypedArrayNode('root'); $node->setKeyAttribute('id', false); // each item under the root is an array, with two scalar items $prototype = new ArrayNode(null, $node); $prototype->addChild(new ScalarNode('foo')); $prototype->addChild(new ScalarNode('id')); // the key attribute will remain $node->setPrototype($prototype); $children = array(); $children[] = array('id' => 'item_name', 'foo' => 'bar'); $normalized = $node->normalize($children); $expected = array(); $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar'); $this->assertEquals($expected, $normalized); }
/** * Tests that when a key attribute is mapped, that key is removed from the array. * And if only 'value' element is left in the array, it will replace its wrapper array. * * <things> * <option id="option1" value="value1"> * </things> * * The above should finally be mapped to an array that looks like this * (because "id" is the key attribute). * * array( * 'things' => array( * 'option1' => 'value1' * ) * ) * * It's also possible to mix 'value-only' and 'non-value-only' elements in the array. * * <things> * <option id="option1" value="value1"> * <option id="option2" value="value2" foo="foo2"> * </things> * * The above should finally be mapped to an array as follows * * array( * 'things' => array( * 'option1' => 'value1', * 'option2' => array( * 'value' => 'value2', * 'foo' => 'foo2' * ) * ) * ) * * The 'value' element can also be ArrayNode: * * <things> * <option id="option1"> * <value> * <foo>foo1</foo> * <bar>bar1</bar> * </value> * </option> * </things> * * The above should be finally be mapped to an array as follows * * array( * 'things' => array( * 'option1' => array( * 'foo' => 'foo1', * 'bar' => 'bar1' * ) * ) * ) * * If using VariableNode for value node, it's also possible to mix different types of value nodes: * * <things> * <option id="option1"> * <value> * <foo>foo1</foo> * <bar>bar1</bar> * </value> * </option> * <option id="option2" value="value2"> * </things> * * The above should be finally mapped to an array as follows * * array( * 'things' => array( * 'option1' => array( * 'foo' => 'foo1', * 'bar' => 'bar1' * ), * 'option2' => 'value2' * ) * ) * * * @dataProvider getDataForKeyRemovedLeftValueOnly */ public function testMappedAttributeKeyIsRemovedLeftValueOnly($value, $children, $expected) { $node = new PrototypedArrayNode('root'); $node->setKeyAttribute('id', true); // each item under the root is an array, with one scalar item $prototype = new ArrayNode(null, $node); $prototype->addChild(new ScalarNode('id')); $prototype->addChild(new ScalarNode('foo')); $prototype->addChild($value); $node->setPrototype($prototype); $normalized = $node->normalize($children); $this->assertEquals($expected, $normalized); }