ArrayNodeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Symfony\Tests\Component\Config\Definition;
  3. use Symfony\Component\Config\Definition\ArrayNode;
  4. use Symfony\Component\Config\Definition\ScalarNode;
  5. class ArrayNodeTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException
  9. */
  10. public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
  11. {
  12. $node = new ArrayNode('root');
  13. $node->normalize(false);
  14. }
  15. /**
  16. * @expectedException InvalidArgumentException
  17. */
  18. public function testSetDefaultValueThrowsExceptionWhenNotAnArray()
  19. {
  20. $node = new ArrayNode('root');
  21. $node->setDefaultValue('test');
  22. }
  23. /**
  24. * @expectedException RuntimeException
  25. */
  26. public function testSetDefaultValueThrowsExceptionWhenNotAnPrototype()
  27. {
  28. $node = new ArrayNode('root');
  29. $node->setDefaultValue(array ('test'));
  30. }
  31. public function testGetDefaultValueReturnsAnEmptyArrayForPrototypes()
  32. {
  33. $node = new ArrayNode('root');
  34. $prototype = new ArrayNode(null, $node);
  35. $node->setPrototype($prototype);
  36. $this->assertEmpty($node->getDefaultValue());
  37. }
  38. public function testGetDefaultValueReturnsDefaultValueForPrototypes()
  39. {
  40. $node = new ArrayNode('root');
  41. $prototype = new ArrayNode(null, $node);
  42. $node->setPrototype($prototype);
  43. $node->setDefaultValue(array ('test'));
  44. $this->assertEquals(array ('test'), $node->getDefaultValue());
  45. }
  46. // finalizeValue() should protect against child values with no corresponding node
  47. public function testExceptionThrownOnUnrecognizedChild()
  48. {
  49. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException');
  50. $node = new ArrayNode('root');
  51. $node->normalize(array('foo' => 'bar'));
  52. }
  53. // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used
  54. public function testRemappedKeysAreUnset()
  55. {
  56. $node = new ArrayNode('root');
  57. $mappingsNode = new ArrayNode('mappings');
  58. $node->addChild($mappingsNode);
  59. // each item under mappings is just a scalar
  60. $prototype= new ScalarNode(null, $mappingsNode);
  61. $mappingsNode->setPrototype($prototype);
  62. $remappings = array();
  63. $remappings[] = array('mapping', 'mappings');
  64. $node->setXmlRemappings($remappings);
  65. $normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
  66. $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
  67. }
  68. /**
  69. * Tests that when a key attribute is mapped, that key is removed from the array:
  70. *
  71. * <things>
  72. * <option id="option1" value="foo">
  73. * <option id="option2" value="bar">
  74. * </things>
  75. *
  76. * The above should finally be mapped to an array that looks like this
  77. * (because "id" is the key attribute).
  78. *
  79. * array(
  80. * 'things' => array(
  81. * 'option1' => 'foo',
  82. * 'option2' => 'bar',
  83. * )
  84. * )
  85. */
  86. public function testMappedAttributeKeyIsRemoved()
  87. {
  88. $node = new ArrayNode('root');
  89. $node->setKeyAttribute('id', true);
  90. // each item under the root is an array, with one scalar item
  91. $prototype= new ArrayNode(null, $node);
  92. $prototype->addChild(new ScalarNode('foo'));
  93. $node->setPrototype($prototype);
  94. $children = array();
  95. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  96. $normalized = $node->normalize($children);
  97. $expected = array();
  98. $expected['item_name'] = array('foo' => 'bar');
  99. $this->assertEquals($expected, $normalized);
  100. }
  101. /**
  102. * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  103. * the removal can be toggled with an option.
  104. */
  105. public function testMappedAttributeKeyNotRemoved()
  106. {
  107. $node = new ArrayNode('root');
  108. $node->setKeyAttribute('id', false);
  109. // each item under the root is an array, with two scalar items
  110. $prototype= new ArrayNode(null, $node);
  111. $prototype->addChild(new ScalarNode('foo'));
  112. $prototype->addChild(new ScalarNode('id')); // the key attribute will remain
  113. $node->setPrototype($prototype);
  114. $children = array();
  115. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  116. $normalized = $node->normalize($children);
  117. $expected = array();
  118. $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
  119. $this->assertEquals($expected, $normalized);
  120. }
  121. }