ArrayNodeTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  47. * normalize() should protect against child values with no corresponding node
  48. */
  49. public function testExceptionThrownOnUnrecognizedChild()
  50. {
  51. $node = new ArrayNode('root');
  52. try
  53. {
  54. $node->normalize(array('foo' => 'bar'));
  55. $this->fail('An exception should have been throw for a bad child node');
  56. } catch (\Exception $e) {
  57. $this->assertInstanceOf('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException', $e);
  58. $this->assertEquals('Unrecognized options "foo" under "root"', $e->getMessage());
  59. }
  60. }
  61. /**
  62. * Tests that no exception is thrown for an unrecognized child if the
  63. * ignoreExtraKeys option is set to true.
  64. *
  65. * Related to testExceptionThrownOnUnrecognizedChild
  66. */
  67. public function testIgnoreExtraKeysNoException()
  68. {
  69. $node = new ArrayNode('roo');
  70. $node->setIgnoreExtraKeys(true);
  71. $node->normalize(array('foo' => 'bar'));
  72. $this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
  73. }
  74. // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used
  75. public function testRemappedKeysAreUnset()
  76. {
  77. $node = new ArrayNode('root');
  78. $mappingsNode = new ArrayNode('mappings');
  79. $node->addChild($mappingsNode);
  80. // each item under mappings is just a scalar
  81. $prototype= new ScalarNode(null, $mappingsNode);
  82. $mappingsNode->setPrototype($prototype);
  83. $remappings = array();
  84. $remappings[] = array('mapping', 'mappings');
  85. $node->setXmlRemappings($remappings);
  86. $normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
  87. $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
  88. }
  89. /**
  90. * Tests that when a key attribute is mapped, that key is removed from the array:
  91. *
  92. * <things>
  93. * <option id="option1" value="foo">
  94. * <option id="option2" value="bar">
  95. * </things>
  96. *
  97. * The above should finally be mapped to an array that looks like this
  98. * (because "id" is the key attribute).
  99. *
  100. * array(
  101. * 'things' => array(
  102. * 'option1' => 'foo',
  103. * 'option2' => 'bar',
  104. * )
  105. * )
  106. */
  107. public function testMappedAttributeKeyIsRemoved()
  108. {
  109. $node = new ArrayNode('root');
  110. $node->setKeyAttribute('id', true);
  111. // each item under the root is an array, with one scalar item
  112. $prototype= new ArrayNode(null, $node);
  113. $prototype->addChild(new ScalarNode('foo'));
  114. $node->setPrototype($prototype);
  115. $children = array();
  116. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  117. $normalized = $node->normalize($children);
  118. $expected = array();
  119. $expected['item_name'] = array('foo' => 'bar');
  120. $this->assertEquals($expected, $normalized);
  121. }
  122. /**
  123. * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  124. * the removal can be toggled with an option.
  125. */
  126. public function testMappedAttributeKeyNotRemoved()
  127. {
  128. $node = new ArrayNode('root');
  129. $node->setKeyAttribute('id', false);
  130. // each item under the root is an array, with two scalar items
  131. $prototype= new ArrayNode(null, $node);
  132. $prototype->addChild(new ScalarNode('foo'));
  133. $prototype->addChild(new ScalarNode('id')); // the key attribute will remain
  134. $node->setPrototype($prototype);
  135. $children = array();
  136. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  137. $normalized = $node->normalize($children);
  138. $expected = array();
  139. $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
  140. $this->assertEquals($expected, $normalized);
  141. }
  142. }