ArrayNodeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // 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. $node->addChild($mappingsNode);
  67. // each item under mappings is just a scalar
  68. $prototype= new ScalarNode(null, $mappingsNode);
  69. $mappingsNode->setPrototype($prototype);
  70. $remappings = array();
  71. $remappings[] = array('mapping', 'mappings');
  72. $node->setXmlRemappings($remappings);
  73. $normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
  74. $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
  75. }
  76. /**
  77. * Tests that when a key attribute is mapped, that key is removed from the array:
  78. *
  79. * <things>
  80. * <option id="option1" value="foo">
  81. * <option id="option2" value="bar">
  82. * </things>
  83. *
  84. * The above should finally be mapped to an array that looks like this
  85. * (because "id" is the key attribute).
  86. *
  87. * array(
  88. * 'things' => array(
  89. * 'option1' => 'foo',
  90. * 'option2' => 'bar',
  91. * )
  92. * )
  93. */
  94. public function testMappedAttributeKeyIsRemoved()
  95. {
  96. $node = new ArrayNode('root');
  97. $node->setKeyAttribute('id', true);
  98. // each item under the root is an array, with one scalar item
  99. $prototype= new ArrayNode(null, $node);
  100. $prototype->addChild(new ScalarNode('foo'));
  101. $node->setPrototype($prototype);
  102. $children = array();
  103. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  104. $normalized = $node->normalize($children);
  105. $expected = array();
  106. $expected['item_name'] = array('foo' => 'bar');
  107. $this->assertEquals($expected, $normalized);
  108. }
  109. /**
  110. * Tests the opposite of the testMappedAttributeKeyIsRemoved because
  111. * the removal can be toggled with an option.
  112. */
  113. public function testMappedAttributeKeyNotRemoved()
  114. {
  115. $node = new ArrayNode('root');
  116. $node->setKeyAttribute('id', false);
  117. // each item under the root is an array, with two scalar items
  118. $prototype= new ArrayNode(null, $node);
  119. $prototype->addChild(new ScalarNode('foo'));
  120. $prototype->addChild(new ScalarNode('id')); // the key attribute will remain
  121. $node->setPrototype($prototype);
  122. $children = array();
  123. $children[] = array('id' => 'item_name', 'foo' => 'bar');
  124. $normalized = $node->normalize($children);
  125. $expected = array();
  126. $expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
  127. $this->assertEquals($expected, $normalized);
  128. }
  129. }