ArrayNodeTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // if prevent extra keys is false, normalize allows them
  54. public function textNoExceptionForUnrecognizedChildWithUnnamedChildren()
  55. {
  56. $node = new ArrayNode('root');
  57. $node->setPreventExtraKeys(false);
  58. $normalized = $node->normalize(array('foo' => 'bar'));
  59. $this->assertEquals(array('foo' => 'bar'), $normalized);
  60. }
  61. // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used
  62. public function testRemappedKeysAreUnset()
  63. {
  64. $node = new ArrayNode('root');
  65. $mappingsNode = new ArrayNode('mappings');
  66. $mappingsNode->setPreventExtraKeys(false); // just so we can add anything to it
  67. $node->addChild($mappingsNode);
  68. $remappings = array();
  69. $remappings[] = array('mapping', 'mappings');
  70. $node->setXmlRemappings($remappings);
  71. $normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
  72. $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
  73. }
  74. /**
  75. * Tests that when a key attribute is mapped, that key is removed from the array:
  76. *
  77. * <things>
  78. * <option id="option1" value="foo">
  79. * <option id="option2" value="bar">
  80. * </things>
  81. *
  82. * The above should finally be mapped to an array that looks like this
  83. * (because "id" is the key attribute).
  84. *
  85. * array(
  86. * 'things' => array(
  87. * 'option1' => 'foo',
  88. * 'option2' => 'bar',
  89. * )
  90. * )
  91. */
  92. public function testMappedAttributeKeyIsRemoved()
  93. {
  94. $node = new ArrayNode('root');
  95. $node->setKeyAttribute('id');
  96. $prototype = new ArrayNode(null);
  97. $prototype->setPreventExtraKeys(false); // just so it allows anything
  98. $node->setPrototype($prototype);
  99. $children = array();
  100. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  101. $normalized = $node->normalize($children);
  102. $expected = array();
  103. $expected['item_name'] = array('foo' => 'bar');
  104. $this->assertEquals($expected, $normalized);
  105. }
  106. /**
  107. * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  108. * the removal can be toggled with an option.
  109. */
  110. public function testMappedAttributeKeyNotRemoved()
  111. {
  112. $node = new ArrayNode('root');
  113. $node->setKeyAttribute('id');
  114. $node->setKeyAttributeIsRemoved(false);
  115. $prototype = new ArrayNode(null);
  116. $prototype->setPreventExtraKeys(false); // just so it allows anything
  117. $node->setPrototype($prototype);
  118. $children = array();
  119. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  120. $normalized = $node->normalize($children);
  121. $expected = array();
  122. $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
  123. $this->assertEquals($expected, $normalized);
  124. }
  125. }