ArrayNodeTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Symfony\Tests\Component\Config\Definition;
  3. use Symfony\Component\Config\Definition\ArrayNode;
  4. class ArrayNodeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException
  8. */
  9. public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
  10. {
  11. $node = new ArrayNode('root');
  12. $node->normalize(false);
  13. }
  14. /**
  15. * @expectedException InvalidArgumentException
  16. */
  17. public function testSetDefaultValueThrowsExceptionWhenNotAnArray()
  18. {
  19. $node = new ArrayNode('root');
  20. $node->setDefaultValue('test');
  21. }
  22. /**
  23. * @expectedException RuntimeException
  24. */
  25. public function testSetDefaultValueThrowsExceptionWhenNotAnPrototype()
  26. {
  27. $node = new ArrayNode('root');
  28. $node->setDefaultValue(array ('test'));
  29. }
  30. public function testGetDefaultValueReturnsAnEmptyArrayForPrototypes()
  31. {
  32. $node = new ArrayNode('root');
  33. $prototype = new ArrayNode(null, $node);
  34. $node->setPrototype($prototype);
  35. $this->assertEmpty($node->getDefaultValue());
  36. }
  37. public function testGetDefaultValueReturnsDefaultValueForPrototypes()
  38. {
  39. $node = new ArrayNode('root');
  40. $prototype = new ArrayNode(null, $node);
  41. $node->setPrototype($prototype);
  42. $node->setDefaultValue(array ('test'));
  43. $this->assertEquals(array ('test'), $node->getDefaultValue());
  44. }
  45. // finalizeValue() should protect against child values with no corresponding node
  46. public function testExceptionThrownOnUnrecognizedChild()
  47. {
  48. $this->setExpectedException('Symfony\Component\DependencyInjection\Configuration\Exception\InvalidConfigurationException');
  49. $node = new ArrayNode('root');
  50. $node->finalize(array('foo' => 'bar'));
  51. }
  52. // if unnamedChildren is true, finalize allows them
  53. public function textNoExceptionForUnrecognizedChildWithUnnamedChildren()
  54. {
  55. $node = new ArrayNode('root');
  56. $node->setAllowUnnamedChildren(true);
  57. $finalized = $node->finalize(array('foo' => 'bar'));
  58. $this->assertEquals(array('foo' => 'bar'), $finalized);
  59. }
  60. /**
  61. * normalize() should not strip values that don't have children nodes.
  62. * Validation will take place later in finalizeValue().
  63. */
  64. public function testNormalizeKeepsExtraArrayValues()
  65. {
  66. $node = new ArrayNode('root');
  67. $normalized = $node->normalize(array('foo' => 'bar'));
  68. $this->assertEquals(array('foo' => 'bar'), $normalized);
  69. }
  70. }