Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -0,0 +1,251 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\ScalarNode;
class ArrayNodeTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
{
$node = new ArrayNode('root');
$node->normalize(false);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Unrecognized option "foo" under "root"
*/
public function testExceptionThrownOnUnrecognizedChild()
{
$node = new ArrayNode('root');
$node->normalize(array('foo' => 'bar'));
}
public function ignoreAndRemoveMatrixProvider()
{
$unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
return array(
array(true, true, array(), 'no exception is thrown for an unrecognized child if the ignoreExtraKeys option is set to true'),
array(true, false, array('foo' => 'bar'), 'extra keys are not removed when ignoreExtraKeys second option is set to false'),
array(false, true, $unrecognizedOptionException),
array(false, false, $unrecognizedOptionException),
);
}
/**
* @dataProvider ignoreAndRemoveMatrixProvider
*/
public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
{
if ($expected instanceof \Exception) {
if (method_exists($this, 'expectException')) {
$this->expectException(\get_class($expected));
$this->expectExceptionMessage($expected->getMessage());
} else {
$this->setExpectedException(\get_class($expected), $expected->getMessage());
}
}
$node = new ArrayNode('root');
$node->setIgnoreExtraKeys($ignore, $remove);
$result = $node->normalize(array('foo' => 'bar'));
$this->assertSame($expected, $result, $message);
}
/**
* @dataProvider getPreNormalizationTests
*/
public function testPreNormalize($denormalized, $normalized)
{
$node = new ArrayNode('foo');
$r = new \ReflectionMethod($node, 'preNormalize');
$r->setAccessible(true);
$this->assertSame($normalized, $r->invoke($node, $denormalized));
}
public function getPreNormalizationTests()
{
return array(
array(
array('foo-bar' => 'foo'),
array('foo_bar' => 'foo'),
),
array(
array('foo-bar_moo' => 'foo'),
array('foo-bar_moo' => 'foo'),
),
array(
array('anything-with-dash-and-no-underscore' => 'first', 'no_dash' => 'second'),
array('anything_with_dash_and_no_underscore' => 'first', 'no_dash' => 'second'),
),
array(
array('foo-bar' => null, 'foo_bar' => 'foo'),
array('foo-bar' => null, 'foo_bar' => 'foo'),
),
);
}
/**
* @dataProvider getZeroNamedNodeExamplesData
*/
public function testNodeNameCanBeZero($denormalized, $normalized)
{
$zeroNode = new ArrayNode(0);
$zeroNode->addChild(new ScalarNode('name'));
$fiveNode = new ArrayNode(5);
$fiveNode->addChild(new ScalarNode(0));
$fiveNode->addChild(new ScalarNode('new_key'));
$rootNode = new ArrayNode('root');
$rootNode->addChild($zeroNode);
$rootNode->addChild($fiveNode);
$rootNode->addChild(new ScalarNode('string_key'));
$r = new \ReflectionMethod($rootNode, 'normalizeValue');
$r->setAccessible(true);
$this->assertSame($normalized, $r->invoke($rootNode, $denormalized));
}
public function getZeroNamedNodeExamplesData()
{
return array(
array(
array(
0 => array(
'name' => 'something',
),
5 => array(
0 => 'this won\'t work too',
'new_key' => 'some other value',
),
'string_key' => 'just value',
),
array(
0 => array(
'name' => 'something',
),
5 => array(
0 => 'this won\'t work too',
'new_key' => 'some other value',
),
'string_key' => 'just value',
),
),
);
}
/**
* @dataProvider getPreNormalizedNormalizedOrderedData
*/
public function testChildrenOrderIsMaintainedOnNormalizeValue($prenormalized, $normalized)
{
$scalar1 = new ScalarNode('1');
$scalar2 = new ScalarNode('2');
$scalar3 = new ScalarNode('3');
$node = new ArrayNode('foo');
$node->addChild($scalar1);
$node->addChild($scalar3);
$node->addChild($scalar2);
$r = new \ReflectionMethod($node, 'normalizeValue');
$r->setAccessible(true);
$this->assertSame($normalized, $r->invoke($node, $prenormalized));
}
public function getPreNormalizedNormalizedOrderedData()
{
return array(
array(
array('2' => 'two', '1' => 'one', '3' => 'three'),
array('2' => 'two', '1' => 'one', '3' => 'three'),
),
);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Child nodes must be named.
*/
public function testAddChildEmptyName()
{
$node = new ArrayNode('root');
$childNode = new ArrayNode('');
$node->addChild($childNode);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage A child node named "foo" already exists.
*/
public function testAddChildNameAlreadyExists()
{
$node = new ArrayNode('root');
$childNode = new ArrayNode('foo');
$node->addChild($childNode);
$childNodeWithSameName = new ArrayNode('foo');
$node->addChild($childNodeWithSameName);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The node at path "foo" has no default value.
*/
public function testGetDefaultValueWithoutDefaultValue()
{
$node = new ArrayNode('foo');
$node->getDefaultValue();
}
public function testSetDeprecated()
{
$childNode = new ArrayNode('foo');
$childNode->setDeprecated('"%node%" is deprecated');
$this->assertTrue($childNode->isDeprecated());
$this->assertSame('"foo" is deprecated', $childNode->getDeprecationMessage($childNode->getName(), $childNode->getPath()));
$node = new ArrayNode('root');
$node->addChild($childNode);
$deprecationTriggered = false;
$deprecationHandler = function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecationTriggered) {
if (E_USER_DEPRECATED === $level) {
return $deprecationTriggered = true;
}
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
};
$prevErrorHandler = set_error_handler($deprecationHandler);
$node->finalize(array());
restore_error_handler();
$this->assertFalse($deprecationTriggered, '->finalize() should not trigger if the deprecated node is not set');
$prevErrorHandler = set_error_handler($deprecationHandler);
$node->finalize(array('foo' => array()));
restore_error_handler();
$this->assertTrue($deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
}
}

View file

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\BooleanNode;
class BooleanNodeTest extends TestCase
{
/**
* @dataProvider getValidValues
*/
public function testNormalize($value)
{
$node = new BooleanNode('test');
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param bool $value
*/
public function testValidNonEmptyValues($value)
{
$node = new BooleanNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(
array(false),
array(true),
);
}
/**
* @dataProvider getInvalidValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new BooleanNode('test');
$node->normalize($value);
}
public function getInvalidValues()
{
return array(
array(null),
array(''),
array('foo'),
array(0),
array(1),
array(0.0),
array(0.1),
array(array()),
array(array('foo' => 'bar')),
array(new \stdClass()),
);
}
}

View file

@ -0,0 +1,347 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
use Symfony\Component\Config\Definition\Processor;
class ArrayNodeDefinitionTest extends TestCase
{
public function testAppendingSomeNode()
{
$parent = new ArrayNodeDefinition('root');
$child = new ScalarNodeDefinition('child');
$parent
->children()
->scalarNode('foo')->end()
->scalarNode('bar')->end()
->end()
->append($child);
$this->assertCount(3, $this->getField($parent, 'children'));
$this->assertContains($child, $this->getField($parent, 'children'));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
* @dataProvider providePrototypeNodeSpecificCalls
*/
public function testPrototypeNodeSpecificOption($method, $args)
{
$node = new ArrayNodeDefinition('root');
\call_user_func_array(array($node, $method), $args);
$node->getNode();
}
public function providePrototypeNodeSpecificCalls()
{
return array(
array('defaultValue', array(array())),
array('addDefaultChildrenIfNoneSet', array()),
array('requiresAtLeastOneElement', array()),
array('useAttributeAsKey', array('foo')),
);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
*/
public function testConcreteNodeSpecificOption()
{
$node = new ArrayNodeDefinition('root');
$node
->addDefaultsIfNotSet()
->prototype('array')
;
$node->getNode();
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
*/
public function testPrototypeNodesCantHaveADefaultValueWhenUsingDefaultChildren()
{
$node = new ArrayNodeDefinition('root');
$node
->defaultValue(array())
->addDefaultChildrenIfNoneSet('foo')
->prototype('array')
;
$node->getNode();
}
public function testPrototypedArrayNodeDefaultWhenUsingDefaultChildren()
{
$node = new ArrayNodeDefinition('root');
$node
->addDefaultChildrenIfNoneSet()
->prototype('array')
;
$tree = $node->getNode();
$this->assertEquals(array(array()), $tree->getDefaultValue());
}
/**
* @dataProvider providePrototypedArrayNodeDefaults
*/
public function testPrototypedArrayNodeDefault($args, $shouldThrowWhenUsingAttrAsKey, $shouldThrowWhenNotUsingAttrAsKey, $defaults)
{
$node = new ArrayNodeDefinition('root');
$node
->addDefaultChildrenIfNoneSet($args)
->prototype('array')
;
try {
$tree = $node->getNode();
$this->assertFalse($shouldThrowWhenNotUsingAttrAsKey);
$this->assertEquals($defaults, $tree->getDefaultValue());
} catch (InvalidDefinitionException $e) {
$this->assertTrue($shouldThrowWhenNotUsingAttrAsKey);
}
$node = new ArrayNodeDefinition('root');
$node
->useAttributeAsKey('attr')
->addDefaultChildrenIfNoneSet($args)
->prototype('array')
;
try {
$tree = $node->getNode();
$this->assertFalse($shouldThrowWhenUsingAttrAsKey);
$this->assertEquals($defaults, $tree->getDefaultValue());
} catch (InvalidDefinitionException $e) {
$this->assertTrue($shouldThrowWhenUsingAttrAsKey);
}
}
public function providePrototypedArrayNodeDefaults()
{
return array(
array(null, true, false, array(array())),
array(2, true, false, array(array(), array())),
array('2', false, true, array('2' => array())),
array('foo', false, true, array('foo' => array())),
array(array('foo'), false, true, array('foo' => array())),
array(array('foo', 'bar'), false, true, array('foo' => array(), 'bar' => array())),
);
}
public function testNestedPrototypedArrayNodes()
{
$nodeDefinition = new ArrayNodeDefinition('root');
$nodeDefinition
->addDefaultChildrenIfNoneSet()
->prototype('array')
->prototype('array')
;
$node = $nodeDefinition->getNode();
$this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node);
$this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node->getPrototype());
}
public function testEnabledNodeDefaults()
{
$node = new ArrayNodeDefinition('root');
$node
->canBeEnabled()
->children()
->scalarNode('foo')->defaultValue('bar')->end()
;
$this->assertEquals(array('enabled' => false, 'foo' => 'bar'), $node->getNode()->getDefaultValue());
}
/**
* @dataProvider getEnableableNodeFixtures
*/
public function testTrueEnableEnabledNode($expected, $config, $message)
{
$processor = new Processor();
$node = new ArrayNodeDefinition('root');
$node
->canBeEnabled()
->children()
->scalarNode('foo')->defaultValue('bar')->end()
;
$this->assertEquals(
$expected,
$processor->process($node->getNode(), $config),
$message
);
}
public function testCanBeDisabled()
{
$node = new ArrayNodeDefinition('root');
$node->canBeDisabled();
$this->assertTrue($this->getField($node, 'addDefaults'));
$this->assertEquals(array('enabled' => false), $this->getField($node, 'falseEquivalent'));
$this->assertEquals(array('enabled' => true), $this->getField($node, 'trueEquivalent'));
$this->assertEquals(array('enabled' => true), $this->getField($node, 'nullEquivalent'));
$nodeChildren = $this->getField($node, 'children');
$this->assertArrayHasKey('enabled', $nodeChildren);
$enabledNode = $nodeChildren['enabled'];
$this->assertTrue($this->getField($enabledNode, 'default'));
$this->assertTrue($this->getField($enabledNode, 'defaultValue'));
}
public function testIgnoreExtraKeys()
{
$node = new ArrayNodeDefinition('root');
$this->assertFalse($this->getField($node, 'ignoreExtraKeys'));
$result = $node->ignoreExtraKeys();
$this->assertEquals($node, $result);
$this->assertTrue($this->getField($node, 'ignoreExtraKeys'));
}
public function testNormalizeKeys()
{
$node = new ArrayNodeDefinition('root');
$this->assertTrue($this->getField($node, 'normalizeKeys'));
$result = $node->normalizeKeys(false);
$this->assertEquals($node, $result);
$this->assertFalse($this->getField($node, 'normalizeKeys'));
}
public function testPrototypeVariable()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('variable'), $node->variablePrototype());
}
public function testPrototypeScalar()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('scalar'), $node->scalarPrototype());
}
public function testPrototypeBoolean()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('boolean'), $node->booleanPrototype());
}
public function testPrototypeInteger()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('integer'), $node->integerPrototype());
}
public function testPrototypeFloat()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('float'), $node->floatPrototype());
}
public function testPrototypeArray()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('array'), $node->arrayPrototype());
}
public function testPrototypeEnum()
{
$node = new ArrayNodeDefinition('root');
$this->assertEquals($node->prototype('enum'), $node->enumPrototype());
}
public function getEnableableNodeFixtures()
{
return array(
array(array('enabled' => true, 'foo' => 'bar'), array(true), 'true enables an enableable node'),
array(array('enabled' => true, 'foo' => 'bar'), array(null), 'null enables an enableable node'),
array(array('enabled' => true, 'foo' => 'bar'), array(array('enabled' => true)), 'An enableable node can be enabled'),
array(array('enabled' => true, 'foo' => 'baz'), array(array('foo' => 'baz')), 'any configuration enables an enableable node'),
array(array('enabled' => false, 'foo' => 'baz'), array(array('foo' => 'baz', 'enabled' => false)), 'An enableable node can be disabled'),
array(array('enabled' => false, 'foo' => 'bar'), array(false), 'false disables an enableable node'),
);
}
public function testRequiresAtLeastOneElement()
{
$node = new ArrayNodeDefinition('root');
$node
->requiresAtLeastOneElement()
->integerPrototype();
$node->getNode()->finalize(array(1));
$this->addToAssertionCount(1);
}
/**
* @group legacy
* @expectedDeprecation Using Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition::cannotBeEmpty() at path "root" has no effect, consider requiresAtLeastOneElement() instead. In 4.0 both methods will behave the same.
*/
public function testCannotBeEmpty()
{
$node = new ArrayNodeDefinition('root');
$node
->cannotBeEmpty()
->integerPrototype();
$node->getNode()->finalize(array());
}
public function testSetDeprecated()
{
$node = new ArrayNodeDefinition('root');
$node
->children()
->arrayNode('foo')->setDeprecated('The "%path%" node is deprecated.')->end()
->end()
;
$deprecatedNode = $node->getNode()->getChildren()['foo'];
$this->assertTrue($deprecatedNode->isDeprecated());
$this->assertSame('The "root.foo" node is deprecated.', $deprecatedNode->getDeprecationMessage($deprecatedNode->getName(), $deprecatedNode->getPath()));
}
/**
* @group legacy
* @expectedDeprecation ->cannotBeEmpty() is not applicable to concrete nodes at path "root". In 4.0 it will throw an exception.
*/
public function testCannotBeEmptyOnConcreteNode()
{
$node = new ArrayNodeDefinition('root');
$node->cannotBeEmpty();
$node->getNode()->finalize(array());
}
protected function getField($object, $field)
{
$reflection = new \ReflectionProperty($object, $field);
$reflection->setAccessible(true);
return $reflection->getValue($object);
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
class BooleanNodeDefinitionTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
* @expectedExceptionMessage ->cannotBeEmpty() is not applicable to BooleanNodeDefinition.
*/
public function testCannotBeEmptyThrowsAnException()
{
$def = new BooleanNodeDefinition('foo');
$def->cannotBeEmpty();
}
public function testSetDeprecated()
{
$def = new BooleanNodeDefinition('foo');
$def->setDeprecated('The "%path%" node is deprecated.');
$node = $def->getNode();
$this->assertTrue($node->isDeprecated());
$this->assertSame('The "foo" node is deprecated.', $node->getDeprecationMessage($node->getName(), $node->getPath()));
}
}

View file

@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\EnumNodeDefinition;
class EnumNodeDefinitionTest extends TestCase
{
public function testWithOneValue()
{
$def = new EnumNodeDefinition('foo');
$def->values(array('foo'));
$node = $def->getNode();
$this->assertEquals(array('foo'), $node->getValues());
}
public function testWithOneDistinctValue()
{
$def = new EnumNodeDefinition('foo');
$def->values(array('foo', 'foo'));
$node = $def->getNode();
$this->assertEquals(array('foo'), $node->getValues());
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must call ->values() on enum nodes.
*/
public function testNoValuesPassed()
{
$def = new EnumNodeDefinition('foo');
$def->getNode();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage ->values() must be called with at least one value.
*/
public function testWithNoValues()
{
$def = new EnumNodeDefinition('foo');
$def->values(array());
}
public function testGetNode()
{
$def = new EnumNodeDefinition('foo');
$def->values(array('foo', 'bar'));
$node = $def->getNode();
$this->assertEquals(array('foo', 'bar'), $node->getValues());
}
public function testSetDeprecated()
{
$def = new EnumNodeDefinition('foo');
$def->values(array('foo', 'bar'));
$def->setDeprecated('The "%path%" node is deprecated.');
$node = $def->getNode();
$this->assertTrue($node->isDeprecated());
$this->assertSame('The "foo" node is deprecated.', $def->getNode()->getDeprecationMessage($node->getName(), $node->getPath()));
}
}

View file

@ -0,0 +1,270 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class ExprBuilderTest extends TestCase
{
public function testAlwaysExpression()
{
$test = $this->getTestBuilder()
->always($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
}
public function testIfTrueExpression()
{
$test = $this->getTestBuilder()
->ifTrue()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, array('key' => true));
$test = $this->getTestBuilder()
->ifTrue(function ($v) { return true; })
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
$test = $this->getTestBuilder()
->ifTrue(function ($v) { return false; })
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}
public function testIfStringExpression()
{
$test = $this->getTestBuilder()
->ifString()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
$test = $this->getTestBuilder()
->ifString()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs(45, $test, array('key' => 45));
}
public function testIfNullExpression()
{
$test = $this->getTestBuilder()
->ifNull()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, array('key' => null));
$test = $this->getTestBuilder()
->ifNull()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}
public function testIfEmptyExpression()
{
$test = $this->getTestBuilder()
->ifEmpty()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
$test = $this->getTestBuilder()
->ifEmpty()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}
public function testIfArrayExpression()
{
$test = $this->getTestBuilder()
->ifArray()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
$test = $this->getTestBuilder()
->ifArray()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}
public function testIfInArrayExpression()
{
$test = $this->getTestBuilder()
->ifInArray(array('foo', 'bar', 'value'))
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
$test = $this->getTestBuilder()
->ifInArray(array('foo', 'bar'))
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}
public function testIfNotInArrayExpression()
{
$test = $this->getTestBuilder()
->ifNotInArray(array('foo', 'bar'))
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
$test = $this->getTestBuilder()
->ifNotInArray(array('foo', 'bar', 'value_from_config'))
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);
}
public function testThenEmptyArrayExpression()
{
$test = $this->getTestBuilder()
->ifString()
->thenEmptyArray()
->end();
$this->assertFinalizedValueIs(array(), $test);
}
/**
* @dataProvider castToArrayValues
*/
public function testcastToArrayExpression($configValue, $expectedValue)
{
$test = $this->getTestBuilder()
->castToArray()
->end();
$this->assertFinalizedValueIs($expectedValue, $test, array('key' => $configValue));
}
public function castToArrayValues()
{
yield array('value', array('value'));
yield array(-3.14, array(-3.14));
yield array(null, array(null));
yield array(array('value'), array('value'));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testThenInvalid()
{
$test = $this->getTestBuilder()
->ifString()
->thenInvalid('Invalid value')
->end();
$this->finalizeTestBuilder($test);
}
public function testThenUnsetExpression()
{
$test = $this->getTestBuilder()
->ifString()
->thenUnset()
->end();
$this->assertEquals(array(), $this->finalizeTestBuilder($test));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must specify an if part.
*/
public function testEndIfPartNotSpecified()
{
$this->getTestBuilder()->end();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must specify a then part.
*/
public function testEndThenPartNotSpecified()
{
$builder = $this->getTestBuilder();
$builder->ifPart = 'test';
$builder->end();
}
/**
* Create a test treebuilder with a variable node, and init the validation.
*
* @return TreeBuilder
*/
protected function getTestBuilder()
{
$builder = new TreeBuilder();
return $builder
->root('test')
->children()
->variableNode('key')
->validate()
;
}
/**
* Close the validation process and finalize with the given config.
*
* @param TreeBuilder $testBuilder The tree builder to finalize
* @param array $config The config you want to use for the finalization, if nothing provided
* a simple array('key'=>'value') will be used
*
* @return array The finalized config values
*/
protected function finalizeTestBuilder($testBuilder, $config = null)
{
return $testBuilder
->end()
->end()
->end()
->buildTree()
->finalize(null === $config ? array('key' => 'value') : $config)
;
}
/**
* Return a closure that will return the given value.
*
* @param mixed $val The value that the closure must return
*
* @return \Closure
*/
protected function returnClosure($val)
{
return function ($v) use ($val) {
return $val;
};
}
/**
* Assert that the given test builder, will return the given value.
*
* @param mixed $value The value to test
* @param TreeBuilder $treeBuilder The tree builder to finalize
* @param mixed $config The config values that new to be finalized
*/
protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
{
$this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\NodeBuilder as BaseNodeBuilder;
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition as BaseVariableNodeDefinition;
class NodeBuilderTest extends TestCase
{
/**
* @expectedException \RuntimeException
*/
public function testThrowsAnExceptionWhenTryingToCreateANonRegisteredNodeType()
{
$builder = new BaseNodeBuilder();
$builder->node('', 'foobar');
}
/**
* @expectedException \RuntimeException
*/
public function testThrowsAnExceptionWhenTheNodeClassIsNotFound()
{
$builder = new BaseNodeBuilder();
$builder
->setNodeClass('noclasstype', '\\foo\\bar\\noclass')
->node('', 'noclasstype');
}
public function testAddingANewNodeType()
{
$class = __NAMESPACE__.'\\SomeNodeDefinition';
$builder = new BaseNodeBuilder();
$node = $builder
->setNodeClass('newtype', $class)
->node('', 'newtype');
$this->assertInstanceOf($class, $node);
}
public function testOverridingAnExistingNodeType()
{
$class = __NAMESPACE__.'\\SomeNodeDefinition';
$builder = new BaseNodeBuilder();
$node = $builder
->setNodeClass('variable', $class)
->node('', 'variable');
$this->assertInstanceOf($class, $node);
}
public function testNodeTypesAreNotCaseSensitive()
{
$builder = new BaseNodeBuilder();
$node1 = $builder->node('', 'VaRiAbLe');
$node2 = $builder->node('', 'variable');
$this->assertInstanceOf(\get_class($node1), $node2);
$builder->setNodeClass('CuStOm', __NAMESPACE__.'\\SomeNodeDefinition');
$node1 = $builder->node('', 'CUSTOM');
$node2 = $builder->node('', 'custom');
$this->assertInstanceOf(\get_class($node1), $node2);
}
public function testNumericNodeCreation()
{
$builder = new BaseNodeBuilder();
$node = $builder->integerNode('foo')->min(3)->max(5);
$this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition', $node);
$node = $builder->floatNode('bar')->min(3.0)->max(5.0);
$this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\FloatNodeDefinition', $node);
}
}
class SomeNodeDefinition extends BaseVariableNodeDefinition
{
}

View file

@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\FloatNodeDefinition;
use Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition;
use Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition as NumericNodeDefinition;
class NumericNodeDefinitionTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You cannot define a min(4) as you already have a max(3)
*/
public function testIncoherentMinAssertion()
{
$def = new NumericNodeDefinition('foo');
$def->max(3)->min(4);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You cannot define a max(2) as you already have a min(3)
*/
public function testIncoherentMaxAssertion()
{
$node = new NumericNodeDefinition('foo');
$node->min(3)->max(2);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The value 4 is too small for path "foo". Should be greater than or equal to 5
*/
public function testIntegerMinAssertion()
{
$def = new IntegerNodeDefinition('foo');
$def->min(5)->getNode()->finalize(4);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The value 4 is too big for path "foo". Should be less than or equal to 3
*/
public function testIntegerMaxAssertion()
{
$def = new IntegerNodeDefinition('foo');
$def->max(3)->getNode()->finalize(4);
}
public function testIntegerValidMinMaxAssertion()
{
$def = new IntegerNodeDefinition('foo');
$node = $def->min(3)->max(7)->getNode();
$this->assertEquals(4, $node->finalize(4));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The value 400 is too small for path "foo". Should be greater than or equal to 500
*/
public function testFloatMinAssertion()
{
$def = new FloatNodeDefinition('foo');
$def->min(5E2)->getNode()->finalize(4e2);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The value 4.3 is too big for path "foo". Should be less than or equal to 0.3
*/
public function testFloatMaxAssertion()
{
$def = new FloatNodeDefinition('foo');
$def->max(0.3)->getNode()->finalize(4.3);
}
public function testFloatValidMinMaxAssertion()
{
$def = new FloatNodeDefinition('foo');
$node = $def->min(3.0)->max(7e2)->getNode();
$this->assertEquals(4.5, $node->finalize(4.5));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
* @expectedExceptionMessage ->cannotBeEmpty() is not applicable to NumericNodeDefinition.
*/
public function testCannotBeEmptyThrowsAnException()
{
$def = new NumericNodeDefinition('foo');
$def->cannotBeEmpty();
}
}

View file

@ -0,0 +1,134 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder as CustomNodeBuilder;
class TreeBuilderTest extends TestCase
{
public function testUsingACustomNodeBuilder()
{
$builder = new TreeBuilder();
$root = $builder->root('custom', 'array', new CustomNodeBuilder());
$nodeBuilder = $root->children();
$this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
$nodeBuilder = $nodeBuilder->arrayNode('deeper')->children();
$this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
}
public function testOverrideABuiltInNodeType()
{
$builder = new TreeBuilder();
$root = $builder->root('override', 'array', new CustomNodeBuilder());
$definition = $root->children()->variableNode('variable');
$this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\VariableNodeDefinition', $definition);
}
public function testAddANodeType()
{
$builder = new TreeBuilder();
$root = $builder->root('override', 'array', new CustomNodeBuilder());
$definition = $root->children()->barNode('variable');
$this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\BarNodeDefinition', $definition);
}
public function testCreateABuiltInNodeTypeWithACustomNodeBuilder()
{
$builder = new TreeBuilder();
$root = $builder->root('builtin', 'array', new CustomNodeBuilder());
$definition = $root->children()->booleanNode('boolean');
$this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition', $definition);
}
public function testPrototypedArrayNodeUseTheCustomNodeBuilder()
{
$builder = new TreeBuilder();
$root = $builder->root('override', 'array', new CustomNodeBuilder());
$root->prototype('bar')->end();
$this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\BarNode', $root->getNode(true)->getPrototype());
}
public function testAnExtendedNodeBuilderGetsPropagatedToTheChildren()
{
$builder = new TreeBuilder();
$builder->root('propagation')
->children()
->setNodeClass('extended', 'Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition')
->node('foo', 'extended')->end()
->arrayNode('child')
->children()
->node('foo', 'extended')
->end()
->end()
->end()
->end();
$node = $builder->buildTree();
$children = $node->getChildren();
$this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $children['foo']);
$childChildren = $children['child']->getChildren();
$this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $childChildren['foo']);
}
public function testDefinitionInfoGetsTransferredToNode()
{
$builder = new TreeBuilder();
$builder->root('test')->info('root info')
->children()
->node('child', 'variable')->info('child info')->defaultValue('default')
->end()
->end();
$tree = $builder->buildTree();
$children = $tree->getChildren();
$this->assertEquals('root info', $tree->getInfo());
$this->assertEquals('child info', $children['child']->getInfo());
}
public function testDefinitionExampleGetsTransferredToNode()
{
$builder = new TreeBuilder();
$builder->root('test')
->example(array('key' => 'value'))
->children()
->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
->end()
->end();
$tree = $builder->buildTree();
$children = $tree->getChildren();
$this->assertInternalType('array', $tree->getExample());
$this->assertEquals('example', $children['child']->getExample());
}
}

View file

@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Dumper\XmlReferenceDumper;
use Symfony\Component\Config\Tests\Fixtures\Configuration\ExampleConfiguration;
class XmlReferenceDumperTest extends TestCase
{
public function testDumper()
{
$configuration = new ExampleConfiguration();
$dumper = new XmlReferenceDumper();
$this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
}
public function testNamespaceDumper()
{
$configuration = new ExampleConfiguration();
$dumper = new XmlReferenceDumper();
$this->assertEquals(str_replace('http://example.org/schema/dic/acme_root', 'http://symfony.com/schema/dic/symfony', $this->getConfigurationAsString()), $dumper->dump($configuration, 'http://symfony.com/schema/dic/symfony'));
}
private function getConfigurationAsString()
{
return str_replace("\n", PHP_EOL, <<<'EOL'
<!-- Namespace: http://example.org/schema/dic/acme_root -->
<!-- scalar-required: Required -->
<!-- scalar-deprecated: Deprecated (The child node "scalar_deprecated" at path "acme_root" is deprecated.) -->
<!-- scalar-deprecated-with-message: Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root") -->
<!-- enum-with-default: One of "this"; "that" -->
<!-- enum: One of "this"; "that" -->
<config
boolean="true"
scalar-empty=""
scalar-null="null"
scalar-true="true"
scalar-false="false"
scalar-default="default"
scalar-array-empty=""
scalar-array-defaults="elem1,elem2"
scalar-required=""
scalar-deprecated=""
scalar-deprecated-with-message=""
node-with-a-looong-name=""
enum-with-default="this"
enum=""
>
<!-- some info -->
<!--
child3: this is a long
multi-line info text
which should be indented;
Example: example setting
-->
<array
child1=""
child2=""
child3=""
/>
<!-- prototype -->
<scalar-prototyped>scalar value</scalar-prototyped>
<!-- prototype: Parameter name -->
<parameter name="parameter name">scalar value</parameter>
<!-- prototype -->
<connection
user=""
pass=""
/>
<!-- prototype -->
<cms-page page="cms page page">
<!-- prototype -->
<!-- title: Required -->
<!-- path: Required -->
<page
locale="page locale"
title=""
path=""
/>
</cms-page>
<!-- prototype -->
<pipou name="pipou name">
<!-- prototype -->
<name didou="" />
</pipou>
</config>
EOL
);
}
}

View file

@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
use Symfony\Component\Config\Tests\Fixtures\Configuration\ExampleConfiguration;
class YamlReferenceDumperTest extends TestCase
{
public function testDumper()
{
$configuration = new ExampleConfiguration();
$dumper = new YamlReferenceDumper();
$this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
}
public function provideDumpAtPath()
{
return array(
'Regular node' => array('scalar_true', <<<EOL
scalar_true: true
EOL
),
'Array node' => array('array', <<<EOL
# some info
array:
child1: ~
child2: ~
# this is a long
# multi-line info text
# which should be indented
child3: ~ # Example: example setting
EOL
),
'Regular nested' => array('array.child2', <<<EOL
child2: ~
EOL
),
'Prototype' => array('cms_pages.page', <<<EOL
# Prototype
page:
# Prototype
locale:
title: ~ # Required
path: ~ # Required
EOL
),
'Nested prototype' => array('cms_pages.page.locale', <<<EOL
# Prototype
locale:
title: ~ # Required
path: ~ # Required
EOL
),
);
}
/**
* @dataProvider provideDumpAtPath
*/
public function testDumpAtPath($path, $expected)
{
$configuration = new ExampleConfiguration();
$dumper = new YamlReferenceDumper();
$this->assertSame(trim($expected), trim($dumper->dumpAtPath($configuration, $path)));
}
private function getConfigurationAsString()
{
return <<<'EOL'
acme_root:
boolean: true
scalar_empty: ~
scalar_null: null
scalar_true: true
scalar_false: false
scalar_default: default
scalar_array_empty: []
scalar_array_defaults:
# Defaults:
- elem1
- elem2
scalar_required: ~ # Required
scalar_deprecated: ~ # Deprecated (The child node "scalar_deprecated" at path "acme_root" is deprecated.)
scalar_deprecated_with_message: ~ # Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root")
node_with_a_looong_name: ~
enum_with_default: this # One of "this"; "that"
enum: ~ # One of "this"; "that"
# some info
array:
child1: ~
child2: ~
# this is a long
# multi-line info text
# which should be indented
child3: ~ # Example: example setting
scalar_prototyped: []
parameters:
# Prototype: Parameter name
name: ~
connections:
# Prototype
-
user: ~
pass: ~
cms_pages:
# Prototype
page:
# Prototype
locale:
title: ~ # Required
path: ~ # Required
pipou:
# Prototype
name: []
EOL;
}
}

View file

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\EnumNode;
class EnumNodeTest extends TestCase
{
public function testFinalizeValue()
{
$node = new EnumNode('foo', null, array('foo', 'bar'));
$this->assertSame('foo', $node->finalize('foo'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $values must contain at least one element.
*/
public function testConstructionWithNoValues()
{
new EnumNode('foo', null, array());
}
public function testConstructionWithOneValue()
{
$node = new EnumNode('foo', null, array('foo'));
$this->assertSame('foo', $node->finalize('foo'));
}
public function testConstructionWithOneDistinctValue()
{
$node = new EnumNode('foo', null, array('foo', 'foo'));
$this->assertSame('foo', $node->finalize('foo'));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The value "foobar" is not allowed for path "foo". Permissible values: "foo", "bar"
*/
public function testFinalizeWithInvalidValue()
{
$node = new EnumNode('foo', null, array('foo', 'bar'));
$node->finalize('foobar');
}
}

View file

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\Processor;
class FinalizationTest extends TestCase
{
public function testUnsetKeyWithDeepHierarchy()
{
$tb = new TreeBuilder();
$tree = $tb
->root('config', 'array')
->children()
->node('level1', 'array')
->canBeUnset()
->children()
->node('level2', 'array')
->canBeUnset()
->children()
->node('somevalue', 'scalar')->end()
->node('anothervalue', 'scalar')->end()
->end()
->end()
->node('level1_scalar', 'scalar')->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$a = array(
'level1' => array(
'level2' => array(
'somevalue' => 'foo',
'anothervalue' => 'bar',
),
'level1_scalar' => 'foo',
),
);
$b = array(
'level1' => array(
'level2' => false,
),
);
$this->assertEquals(array(
'level1' => array(
'level1_scalar' => 'foo',
),
), $this->process($tree, array($a, $b)));
}
protected function process(NodeInterface $tree, array $configs)
{
$processor = new Processor();
return $processor->process($tree, $configs);
}
}

View file

@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\FloatNode;
class FloatNodeTest extends TestCase
{
/**
* @dataProvider getValidValues
*/
public function testNormalize($value)
{
$node = new FloatNode('test');
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param int $value
*/
public function testValidNonEmptyValues($value)
{
$node = new FloatNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(
array(1798.0),
array(-678.987),
array(12.56E45),
array(0.0),
// Integer are accepted too, they will be cast
array(17),
array(-10),
array(0),
);
}
/**
* @dataProvider getInvalidValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new FloatNode('test');
$node->normalize($value);
}
public function getInvalidValues()
{
return array(
array(null),
array(''),
array('foo'),
array(true),
array(false),
array(array()),
array(array('foo' => 'bar')),
array(new \stdClass()),
);
}
}

View file

@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\IntegerNode;
class IntegerNodeTest extends TestCase
{
/**
* @dataProvider getValidValues
*/
public function testNormalize($value)
{
$node = new IntegerNode('test');
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param int $value
*/
public function testValidNonEmptyValues($value)
{
$node = new IntegerNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(
array(1798),
array(-678),
array(0),
);
}
/**
* @dataProvider getInvalidValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new IntegerNode('test');
$node->normalize($value);
}
public function getInvalidValues()
{
return array(
array(null),
array(''),
array('foo'),
array(true),
array(false),
array(0.0),
array(0.1),
array(array()),
array(array('foo' => 'bar')),
array(new \stdClass()),
);
}
}

View file

@ -0,0 +1,196 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class MergeTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException
*/
public function testForbiddenOverwrite()
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->children()
->node('foo', 'scalar')
->cannotBeOverwritten()
->end()
->end()
->end()
->buildTree()
;
$a = array(
'foo' => 'bar',
);
$b = array(
'foo' => 'moo',
);
$tree->merge($a, $b);
}
public function testUnsetKey()
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->children()
->node('foo', 'scalar')->end()
->node('bar', 'scalar')->end()
->node('unsettable', 'array')
->canBeUnset()
->children()
->node('foo', 'scalar')->end()
->node('bar', 'scalar')->end()
->end()
->end()
->node('unsetted', 'array')
->canBeUnset()
->prototype('scalar')->end()
->end()
->end()
->end()
->buildTree()
;
$a = array(
'foo' => 'bar',
'unsettable' => array(
'foo' => 'a',
'bar' => 'b',
),
'unsetted' => false,
);
$b = array(
'foo' => 'moo',
'bar' => 'b',
'unsettable' => false,
'unsetted' => array('a', 'b'),
);
$this->assertEquals(array(
'foo' => 'moo',
'bar' => 'b',
'unsettable' => false,
'unsetted' => array('a', 'b'),
), $tree->merge($a, $b));
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testDoesNotAllowNewKeysInSubsequentConfigs()
{
$tb = new TreeBuilder();
$tree = $tb
->root('config', 'array')
->children()
->node('test', 'array')
->disallowNewKeysInSubsequentConfigs()
->useAttributeAsKey('key')
->prototype('array')
->children()
->node('value', 'scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree();
$a = array(
'test' => array(
'a' => array('value' => 'foo'),
),
);
$b = array(
'test' => array(
'b' => array('value' => 'foo'),
),
);
$tree->merge($a, $b);
}
public function testPerformsNoDeepMerging()
{
$tb = new TreeBuilder();
$tree = $tb
->root('config', 'array')
->children()
->node('no_deep_merging', 'array')
->performNoDeepMerging()
->children()
->node('foo', 'scalar')->end()
->node('bar', 'scalar')->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$a = array(
'no_deep_merging' => array(
'foo' => 'a',
'bar' => 'b',
),
);
$b = array(
'no_deep_merging' => array(
'c' => 'd',
),
);
$this->assertEquals(array(
'no_deep_merging' => array(
'c' => 'd',
),
), $tree->merge($a, $b));
}
public function testPrototypeWithoutAKeyAttribute()
{
$tb = new TreeBuilder();
$tree = $tb
->root('config', 'array')
->children()
->arrayNode('append_elements')
->prototype('scalar')->end()
->end()
->end()
->end()
->buildTree()
;
$a = array(
'append_elements' => array('a', 'b'),
);
$b = array(
'append_elements' => array('c', 'd'),
);
$this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
}
}

View file

@ -0,0 +1,230 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\NodeInterface;
class NormalizationTest extends TestCase
{
/**
* @dataProvider getEncoderTests
*/
public function testNormalizeEncoders($denormalized)
{
$tb = new TreeBuilder();
$tree = $tb
->root('root_name', 'array')
->fixXmlConfig('encoder')
->children()
->node('encoders', 'array')
->useAttributeAsKey('class')
->prototype('array')
->beforeNormalization()->ifString()->then(function ($v) { return array('algorithm' => $v); })->end()
->children()
->node('algorithm', 'scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = array(
'encoders' => array(
'foo' => array('algorithm' => 'plaintext'),
),
);
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getEncoderTests()
{
$configs = array();
// XML
$configs[] = array(
'encoder' => array(
array('class' => 'foo', 'algorithm' => 'plaintext'),
),
);
// XML when only one element of this type
$configs[] = array(
'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
array('class' => 'foo', 'algorithm' => 'plaintext'),
),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
'foo' => 'plaintext',
),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
'foo' => array('algorithm' => 'plaintext'),
),
);
return array_map(function ($v) {
return array($v);
}, $configs);
}
/**
* @dataProvider getAnonymousKeysTests
*/
public function testAnonymousKeysArray($denormalized)
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->children()
->node('logout', 'array')
->fixXmlConfig('handler')
->children()
->node('handlers', 'array')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getAnonymousKeysTests()
{
$configs = array();
$configs[] = array(
'logout' => array(
'handlers' => array('a', 'b', 'c'),
),
);
$configs[] = array(
'logout' => array(
'handler' => array('a', 'b', 'c'),
),
);
return array_map(function ($v) { return array($v); }, $configs);
}
/**
* @dataProvider getNumericKeysTests
*/
public function testNumericKeysAsAttributes($denormalized)
{
$normalized = array(
'thing' => array(42 => array('foo', 'bar'), 1337 => array('baz', 'qux')),
);
$this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
}
public function getNumericKeysTests()
{
$configs = array();
$configs[] = array(
'thing' => array(
42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
),
);
$configs[] = array(
'thing' => array(
array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
),
);
return array_map(function ($v) { return array($v); }, $configs);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The attribute "id" must be set for path "root.thing".
*/
public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
{
$denormalized = array(
'thing' => array(
array('foo', 'bar'), array('baz', 'qux'),
),
);
$this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
}
public function testAssociativeArrayPreserveKeys()
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->prototype('array')
->children()
->node('foo', 'scalar')->end()
->end()
->end()
->end()
->buildTree()
;
$data = array('first' => array('foo' => 'bar'));
$this->assertNormalized($tree, $data, $data);
}
public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
{
self::assertSame($normalized, $tree->normalize($denormalized));
}
private function getNumericKeysTestTree()
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->children()
->node('thing', 'array')
->useAttributeAsKey('id')
->prototype('array')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->buildTree()
;
return $tree;
}
}

View file

@ -0,0 +1,342 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;
class PrototypedArrayNodeTest extends TestCase
{
public function testGetDefaultValueReturnsAnEmptyArrayForPrototypes()
{
$node = new PrototypedArrayNode('root');
$prototype = new ArrayNode(null, $node);
$node->setPrototype($prototype);
$this->assertEmpty($node->getDefaultValue());
}
public function testGetDefaultValueReturnsDefaultValueForPrototypes()
{
$node = new PrototypedArrayNode('root');
$prototype = new ArrayNode(null, $node);
$node->setPrototype($prototype);
$node->setDefaultValue(array('test'));
$this->assertEquals(array('test'), $node->getDefaultValue());
}
// a remapped key (e.g. "mapping" -> "mappings") should be unset after being used
public function testRemappedKeysAreUnset()
{
$node = new ArrayNode('root');
$mappingsNode = new PrototypedArrayNode('mappings');
$node->addChild($mappingsNode);
// each item under mappings is just a scalar
$prototype = new ScalarNode(null, $mappingsNode);
$mappingsNode->setPrototype($prototype);
$remappings = array();
$remappings[] = array('mapping', 'mappings');
$node->setXmlRemappings($remappings);
$normalized = $node->normalize(array('mapping' => array('foo', 'bar')));
$this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized);
}
/**
* Tests that when a key attribute is mapped, that key is removed from the array.
*
* <things>
* <option id="option1" value="foo">
* <option id="option2" value="bar">
* </things>
*
* The above should finally be mapped to an array that looks like this
* (because "id" is the key attribute).
*
* array(
* 'things' => array(
* 'option1' => 'foo',
* 'option2' => 'bar',
* )
* )
*/
public function testMappedAttributeKeyIsRemoved()
{
$node = new PrototypedArrayNode('root');
$node->setKeyAttribute('id', true);
// each item under the root is an array, with one scalar item
$prototype = new ArrayNode(null, $node);
$prototype->addChild(new ScalarNode('foo'));
$node->setPrototype($prototype);
$children = array();
$children[] = array('id' => 'item_name', 'foo' => 'bar');
$normalized = $node->normalize($children);
$expected = array();
$expected['item_name'] = array('foo' => 'bar');
$this->assertEquals($expected, $normalized);
}
/**
* Tests the opposite of the testMappedAttributeKeyIsRemoved because
* the removal can be toggled with an option.
*/
public function testMappedAttributeKeyNotRemoved()
{
$node = new PrototypedArrayNode('root');
$node->setKeyAttribute('id', false);
// each item under the root is an array, with two scalar items
$prototype = new ArrayNode(null, $node);
$prototype->addChild(new ScalarNode('foo'));
$prototype->addChild(new ScalarNode('id')); // the key attribute will remain
$node->setPrototype($prototype);
$children = array();
$children[] = array('id' => 'item_name', 'foo' => 'bar');
$normalized = $node->normalize($children);
$expected = array();
$expected['item_name'] = array('id' => 'item_name', 'foo' => 'bar');
$this->assertEquals($expected, $normalized);
}
public function testAddDefaultChildren()
{
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setAddChildrenIfNoneSet();
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array(array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setKeyAttribute('foobar');
$node->setAddChildrenIfNoneSet();
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array('defaults' => array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setKeyAttribute('foobar');
$node->setAddChildrenIfNoneSet('defaultkey');
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array('defaultkey' => array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setKeyAttribute('foobar');
$node->setAddChildrenIfNoneSet(array('defaultkey'));
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array('defaultkey' => array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setKeyAttribute('foobar');
$node->setAddChildrenIfNoneSet(array('dk1', 'dk2'));
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array('dk1' => array('foo' => 'bar'), 'dk2' => array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setAddChildrenIfNoneSet(array(5, 6));
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array(0 => array('foo' => 'bar'), 1 => array('foo' => 'bar')), $node->getDefaultValue());
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setAddChildrenIfNoneSet(2);
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array(array('foo' => 'bar'), array('foo' => 'bar')), $node->getDefaultValue());
}
public function testDefaultChildrenWinsOverDefaultValue()
{
$node = $this->getPrototypeNodeWithDefaultChildren();
$node->setAddChildrenIfNoneSet();
$node->setDefaultValue(array('bar' => 'foo'));
$this->assertTrue($node->hasDefaultValue());
$this->assertEquals(array(array('foo' => 'bar')), $node->getDefaultValue());
}
protected function getPrototypeNodeWithDefaultChildren()
{
$node = new PrototypedArrayNode('root');
$prototype = new ArrayNode(null, $node);
$child = new ScalarNode('foo');
$child->setDefaultValue('bar');
$prototype->addChild($child);
$prototype->setAddIfNotSet(true);
$node->setPrototype($prototype);
return $node;
}
/**
* Tests that when a key attribute is mapped, that key is removed from the array.
* And if only 'value' element is left in the array, it will replace its wrapper array.
*
* <things>
* <option id="option1" value="value1">
* </things>
*
* The above should finally be mapped to an array that looks like this
* (because "id" is the key attribute).
*
* array(
* 'things' => array(
* 'option1' => 'value1'
* )
* )
*
* It's also possible to mix 'value-only' and 'non-value-only' elements in the array.
*
* <things>
* <option id="option1" value="value1">
* <option id="option2" value="value2" foo="foo2">
* </things>
*
* The above should finally be mapped to an array as follows
*
* array(
* 'things' => array(
* 'option1' => 'value1',
* 'option2' => array(
* 'value' => 'value2',
* 'foo' => 'foo2'
* )
* )
* )
*
* The 'value' element can also be ArrayNode:
*
* <things>
* <option id="option1">
* <value>
* <foo>foo1</foo>
* <bar>bar1</bar>
* </value>
* </option>
* </things>
*
* The above should be finally be mapped to an array as follows
*
* array(
* 'things' => array(
* 'option1' => array(
* 'foo' => 'foo1',
* 'bar' => 'bar1'
* )
* )
* )
*
* If using VariableNode for value node, it's also possible to mix different types of value nodes:
*
* <things>
* <option id="option1">
* <value>
* <foo>foo1</foo>
* <bar>bar1</bar>
* </value>
* </option>
* <option id="option2" value="value2">
* </things>
*
* The above should be finally mapped to an array as follows
*
* array(
* 'things' => array(
* 'option1' => array(
* 'foo' => 'foo1',
* 'bar' => 'bar1'
* ),
* 'option2' => 'value2'
* )
* )
*
*
* @dataProvider getDataForKeyRemovedLeftValueOnly
*/
public function testMappedAttributeKeyIsRemovedLeftValueOnly($value, $children, $expected)
{
$node = new PrototypedArrayNode('root');
$node->setKeyAttribute('id', true);
// each item under the root is an array, with one scalar item
$prototype = new ArrayNode(null, $node);
$prototype->addChild(new ScalarNode('id'));
$prototype->addChild(new ScalarNode('foo'));
$prototype->addChild($value);
$node->setPrototype($prototype);
$normalized = $node->normalize($children);
$this->assertEquals($expected, $normalized);
}
public function getDataForKeyRemovedLeftValueOnly()
{
$scalarValue = new ScalarNode('value');
$arrayValue = new ArrayNode('value');
$arrayValue->addChild(new ScalarNode('foo'));
$arrayValue->addChild(new ScalarNode('bar'));
$variableValue = new VariableNode('value');
return array(
array(
$scalarValue,
array(
array('id' => 'option1', 'value' => 'value1'),
),
array('option1' => 'value1'),
),
array(
$scalarValue,
array(
array('id' => 'option1', 'value' => 'value1'),
array('id' => 'option2', 'value' => 'value2', 'foo' => 'foo2'),
),
array(
'option1' => 'value1',
'option2' => array('value' => 'value2', 'foo' => 'foo2'),
),
),
array(
$arrayValue,
array(
array(
'id' => 'option1',
'value' => array('foo' => 'foo1', 'bar' => 'bar1'),
),
),
array(
'option1' => array('foo' => 'foo1', 'bar' => 'bar1'),
),
),
array($variableValue,
array(
array(
'id' => 'option1', 'value' => array('foo' => 'foo1', 'bar' => 'bar1'),
),
array('id' => 'option2', 'value' => 'value2'),
),
array(
'option1' => array('foo' => 'foo1', 'bar' => 'bar1'),
'option2' => 'value2',
),
),
);
}
}

View file

@ -0,0 +1,169 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
class ScalarNodeTest extends TestCase
{
/**
* @dataProvider getValidValues
*/
public function testNormalize($value)
{
$node = new ScalarNode('test');
$this->assertSame($value, $node->normalize($value));
}
public function getValidValues()
{
return array(
array(false),
array(true),
array(null),
array(''),
array('foo'),
array(0),
array(1),
array(0.0),
array(0.1),
);
}
public function testSetDeprecated()
{
$childNode = new ScalarNode('foo');
$childNode->setDeprecated('"%node%" is deprecated');
$this->assertTrue($childNode->isDeprecated());
$this->assertSame('"foo" is deprecated', $childNode->getDeprecationMessage($childNode->getName(), $childNode->getPath()));
$node = new ArrayNode('root');
$node->addChild($childNode);
$deprecationTriggered = 0;
$deprecationHandler = function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecationTriggered) {
if (E_USER_DEPRECATED === $level) {
return ++$deprecationTriggered;
}
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
};
$prevErrorHandler = set_error_handler($deprecationHandler);
$node->finalize(array());
restore_error_handler();
$this->assertSame(0, $deprecationTriggered, '->finalize() should not trigger if the deprecated node is not set');
$prevErrorHandler = set_error_handler($deprecationHandler);
$node->finalize(array('foo' => ''));
restore_error_handler();
$this->assertSame(1, $deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
}
/**
* @dataProvider getInvalidValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new ScalarNode('test');
$node->normalize($value);
}
public function getInvalidValues()
{
return array(
array(array()),
array(array('foo' => 'bar')),
array(new \stdClass()),
);
}
public function testNormalizeThrowsExceptionWithoutHint()
{
$node = new ScalarNode('test');
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
$this->expectExceptionMessage('Invalid type for path "test". Expected scalar, but got array.');
} else {
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
}
$node->normalize(array());
}
public function testNormalizeThrowsExceptionWithErrorMessage()
{
$node = new ScalarNode('test');
$node->setInfo('"the test value"');
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
$this->expectExceptionMessage("Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
} else {
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
}
$node->normalize(array());
}
/**
* @dataProvider getValidNonEmptyValues
*
* @param mixed $value
*/
public function testValidNonEmptyValues($value)
{
$node = new ScalarNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidNonEmptyValues()
{
return array(
array(false),
array(true),
array('foo'),
array(0),
array(1),
array(0.0),
array(0.1),
);
}
/**
* @dataProvider getEmptyValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*
* @param mixed $value
*/
public function testNotAllowedEmptyValuesThrowException($value)
{
$node = new ScalarNode('test');
$node->setAllowEmptyValue(false);
$node->finalize($value);
}
public function getEmptyValues()
{
return array(
array(null),
array(''),
);
}
}