ArrayNodeTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\DependencyInjection\Configuration\Exception\InvalidConfigurationException');
  50. $node = new ArrayNode('root');
  51. $node->finalize(array('foo' => 'bar'));
  52. }
  53. // if unnamedChildren is true, finalize allows them
  54. public function textNoExceptionForUnrecognizedChildWithUnnamedChildren()
  55. {
  56. $node = new ArrayNode('root');
  57. $node->setAllowUnnamedChildren(true);
  58. $finalized = $node->finalize(array('foo' => 'bar'));
  59. $this->assertEquals(array('foo' => 'bar'), $finalized);
  60. }
  61. /**
  62. * normalize() should not strip values that don't have children nodes.
  63. * Validation will take place later in finalizeValue().
  64. */
  65. public function testNormalizeKeepsExtraArrayValues()
  66. {
  67. $node = new ArrayNode('root');
  68. $normalized = $node->normalize(array('foo' => 'bar'));
  69. $this->assertEquals(array('foo' => 'bar'), $normalized);
  70. }
  71. // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used
  72. public function testRemappedKeysAreUnset()
  73. {
  74. $node = new ArrayNode('root');
  75. $remappings = array();
  76. $remappings[] = array('mapping', 'mappings');
  77. $node->setXmlRemappings($remappings);
  78. $normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
  79. $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
  80. }
  81. /**
  82. * Tests that when a key attribute is mapped, that key is removed from the array:
  83. *
  84. * <things>
  85. * <option id="option1" value="foo">
  86. * <option id="option2" value="bar">
  87. * </things>
  88. *
  89. * The above should finally be mapped to an array that looks like this
  90. * (because "id" is the key attribute).
  91. *
  92. * array(
  93. * 'things' => array(
  94. * 'option1' => 'foo',
  95. * 'option2' => 'bar',
  96. * )
  97. * )
  98. */
  99. public function testMappedAttributeKeyIsRemoved()
  100. {
  101. $node = new ArrayNode('root');
  102. $node->setKeyAttribute('id');
  103. $prototype = new ArrayNode(null);
  104. $node->setPrototype($prototype);
  105. $children = array();
  106. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  107. $normalized = $node->normalize($children);
  108. $expected = array();
  109. $expected['item_name'] = array('foo' => 'bar');
  110. $this->assertEquals($expected, $normalized);
  111. }
  112. }