ArrayNodeTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Tests\Definition;
  11. use Symfony\Component\Config\Definition\ArrayNode;
  12. class ArrayNodeTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
  16. */
  17. public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
  18. {
  19. $node = new ArrayNode('root');
  20. $node->normalize(false);
  21. }
  22. /**
  23. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  24. * @expectedExceptionMessage Unrecognized options "foo" under "root"
  25. */
  26. public function testExceptionThrownOnUnrecognizedChild()
  27. {
  28. $node = new ArrayNode('root');
  29. $node->normalize(array('foo' => 'bar'));
  30. }
  31. /**
  32. * Tests that no exception is thrown for an unrecognized child if the
  33. * ignoreExtraKeys option is set to true.
  34. *
  35. * Related to testExceptionThrownOnUnrecognizedChild
  36. */
  37. public function testIgnoreExtraKeysNoException()
  38. {
  39. $node = new ArrayNode('roo');
  40. $node->setIgnoreExtraKeys(true);
  41. $node->normalize(array('foo' => 'bar'));
  42. $this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
  43. }
  44. /**
  45. * @dataProvider getPreNormalizationTests
  46. */
  47. public function testPreNormalize($denormalized, $normalized)
  48. {
  49. $node = new ArrayNode('foo');
  50. $r = new \ReflectionMethod($node, 'preNormalize');
  51. $r->setAccessible(true);
  52. $this->assertSame($normalized, $r->invoke($node, $denormalized));
  53. }
  54. public function getPreNormalizationTests()
  55. {
  56. return array(
  57. array(
  58. array('foo-bar' => 'foo'),
  59. array('foo_bar' => 'foo'),
  60. ),
  61. array(
  62. array('foo-bar_moo' => 'foo'),
  63. array('foo-bar_moo' => 'foo'),
  64. ),
  65. array(
  66. array('foo-bar' => null, 'foo_bar' => 'foo'),
  67. array('foo-bar' => null, 'foo_bar' => 'foo'),
  68. )
  69. );
  70. }
  71. }