composer update

This commit is contained in:
Manish Verma
2018-12-05 10:50:52 +05:30
parent 9eabcacfa7
commit 4addd1e9c6
3328 changed files with 156676 additions and 138988 deletions

View File

@@ -1,6 +1,12 @@
CHANGELOG
=========
4.2.0
-----
* added support for nested options definition
* added `setDeprecated` and `isDeprecated` methods
3.4.0
-----

View File

@@ -87,4 +87,14 @@ class OptionsResolverIntrospector
{
return \call_user_func($this->get, 'normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option));
}
/**
* @return string|\Closure
*
* @throws NoConfigurationException on no configured deprecation
*/
public function getDeprecationMessage(string $option)
{
return \call_user_func($this->get, 'deprecated', $option, sprintf('No deprecation was set for the "%s" option.', $option));
}
}

View File

@@ -16,6 +16,6 @@ namespace Symfony\Component\OptionsResolver\Exception;
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface ExceptionInterface
interface ExceptionInterface extends \Throwable
{
}

View File

@@ -16,6 +16,8 @@ namespace Symfony\Component\OptionsResolver;
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Tobias Schultze <http://tobion.de>
*
* @method mixed offsetGet(string $option, bool $triggerDeprecation = true)
*/
interface Options extends \ArrayAccess, \Countable
{

View File

@@ -12,6 +12,7 @@
namespace Symfony\Component\OptionsResolver;
use Symfony\Component\OptionsResolver\Exception\AccessException;
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\OptionsResolver\Exception\NoSuchOptionException;
@@ -36,6 +37,13 @@ class OptionsResolver implements Options
*/
private $defaults = array();
/**
* A list of closure for nested options.
*
* @var \Closure[][]
*/
private $nested = array();
/**
* The names of required options.
*/
@@ -75,6 +83,16 @@ class OptionsResolver implements Options
*/
private $calling = array();
/**
* A list of deprecated options.
*/
private $deprecated = array();
/**
* The list of options provided by the user.
*/
private $given = array();
/**
* Whether the instance is locked for reading.
*
@@ -124,6 +142,20 @@ class OptionsResolver implements Options
* is spread across different locations of your code, such as base and
* sub-classes.
*
* If you want to define nested options, you can pass a closure with the
* following signature:
*
* $options->setDefault('database', function (OptionsResolver $resolver) {
* $resolver->setDefined(array('dbname', 'host', 'port', 'user', 'pass'));
* }
*
* To get access to the parent options, add a second argument to the closure's
* signature:
*
* function (OptionsResolver $resolver, Options $parent) {
* // 'default' === $parent['connection']
* }
*
* @param string $option The name of the option
* @param mixed $value The default value of the option
*
@@ -161,15 +193,27 @@ class OptionsResolver implements Options
$this->lazy[$option][] = $value;
$this->defined[$option] = true;
// Make sure the option is processed
unset($this->resolved[$option]);
// Make sure the option is processed and is not nested anymore
unset($this->resolved[$option], $this->nested[$option]);
return $this;
}
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && self::class === $class->name && (!isset($params[1]) || (null !== ($class = $params[1]->getClass()) && Options::class === $class->name))) {
// Store closure for later evaluation
$this->nested[$option][] = $value;
$this->defaults[$option] = array();
$this->defined[$option] = true;
// Make sure the option is processed and is not lazy anymore
unset($this->resolved[$option], $this->lazy[$option]);
return $this;
}
}
// This option is not lazy anymore
unset($this->lazy[$option]);
// This option is not lazy nor nested anymore
unset($this->lazy[$option], $this->nested[$option]);
// Yet undefined options can be marked as resolved, because we only need
// to resolve options with lazy closures, normalizers or validation
@@ -348,16 +392,70 @@ class OptionsResolver implements Options
return array_keys($this->defined);
}
public function isNested(string $option): bool
{
return isset($this->nested[$option]);
}
/**
* Deprecates an option, allowed types or values.
*
* Instead of passing the message, you may also pass a closure with the
* following signature:
*
* function (Options $options, $value): string {
* // ...
* }
*
* The closure receives the value as argument and should return a string.
* Return an empty string to ignore the option deprecation.
*
* The closure is invoked when {@link resolve()} is called. The parameter
* passed to the closure is the value of the option after validating it
* and before normalizing it.
*
* @param string|\Closure $deprecationMessage
*/
public function setDeprecated(string $option, $deprecationMessage = 'The option "%name%" is deprecated.'): self
{
if ($this->locked) {
throw new AccessException('Options cannot be deprecated from a lazy option or normalizer.');
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist, defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
if (!\is_string($deprecationMessage) && !$deprecationMessage instanceof \Closure) {
throw new InvalidArgumentException(sprintf('Invalid type for deprecation message argument, expected string or \Closure, but got "%s".', \gettype($deprecationMessage)));
}
// ignore if empty string
if ('' === $deprecationMessage) {
return $this;
}
$this->deprecated[$option] = $deprecationMessage;
// Make sure the option is processed
unset($this->resolved[$option]);
return $this;
}
public function isDeprecated(string $option): bool
{
return isset($this->deprecated[$option]);
}
/**
* Sets the normalizer for an option.
*
* The normalizer should be a closure with the following signature:
*
* ```php
* function (Options $options, $value) {
* // ...
* }
* ```
* function (Options $options, $value) {
* // ...
* }
*
* The closure is invoked when {@link resolve()} is called. The closure
* has access to the resolved values of other options through the passed
@@ -383,11 +481,7 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
$this->normalizers[$option] = $normalizer;
@@ -426,11 +520,7 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
$this->allowedValues[$option] = \is_array($allowedValues) ? $allowedValues : array($allowedValues);
@@ -471,11 +561,7 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
if (!\is_array($allowedValues)) {
@@ -516,11 +602,7 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
$this->allowedTypes[$option] = (array) $allowedTypes;
@@ -555,11 +637,7 @@ class OptionsResolver implements Options
}
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
if (!isset($this->allowedTypes[$option])) {
@@ -614,12 +692,14 @@ class OptionsResolver implements Options
$this->defined = array();
$this->defaults = array();
$this->nested = array();
$this->required = array();
$this->resolved = array();
$this->lazy = array();
$this->normalizers = array();
$this->allowedTypes = array();
$this->allowedValues = array();
$this->deprecated = array();
return $this;
}
@@ -664,15 +744,12 @@ class OptionsResolver implements Options
ksort($clone->defined);
ksort($diff);
throw new UndefinedOptionsException(sprintf(
(\count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.').' Defined options are: "%s".',
implode('", "', array_keys($diff)),
implode('", "', array_keys($clone->defined))
));
throw new UndefinedOptionsException(sprintf((\count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.').' Defined options are: "%s".', implode('", "', array_keys($diff)), implode('", "', array_keys($clone->defined))));
}
// Override options set by the user
foreach ($options as $option => $value) {
$clone->given[$option] = true;
$clone->defaults[$option] = $value;
unset($clone->resolved[$option], $clone->lazy[$option]);
}
@@ -683,10 +760,7 @@ class OptionsResolver implements Options
if (\count($diff) > 0) {
ksort($diff);
throw new MissingOptionsException(sprintf(
\count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.',
implode('", "', array_keys($diff))
));
throw new MissingOptionsException(sprintf(\count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.', implode('", "', array_keys($diff))));
}
// Lock the container
@@ -704,7 +778,8 @@ class OptionsResolver implements Options
/**
* Returns the resolved value of an option.
*
* @param string $option The option name
* @param string $option The option name
* @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default)
*
* @return mixed The option value
*
@@ -716,44 +791,64 @@ class OptionsResolver implements Options
* @throws OptionDefinitionException If there is a cyclic dependency between
* lazy options and/or normalizers
*/
public function offsetGet($option)
public function offsetGet($option/*, bool $triggerDeprecation = true*/)
{
if (!$this->locked) {
throw new AccessException('Array access is only supported within closures of lazy options and normalizers.');
}
$triggerDeprecation = 1 === \func_num_args() || \func_get_arg(1);
// Shortcut for resolved options
if (array_key_exists($option, $this->resolved)) {
if (isset($this->resolved[$option]) || array_key_exists($option, $this->resolved)) {
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || $this->calling) && \is_string($this->deprecated[$option])) {
@trigger_error(strtr($this->deprecated[$option], array('%name%' => $option)), E_USER_DEPRECATED);
}
return $this->resolved[$option];
}
// Check whether the option is set at all
if (!array_key_exists($option, $this->defaults)) {
if (!isset($this->defaults[$option]) && !array_key_exists($option, $this->defaults)) {
if (!isset($this->defined[$option])) {
throw new NoSuchOptionException(sprintf(
'The option "%s" does not exist. Defined options are: "%s".',
$option,
implode('", "', array_keys($this->defined))
));
throw new NoSuchOptionException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
}
throw new NoSuchOptionException(sprintf(
'The optional option "%s" has no value set. You should make sure it is set with "isset" before reading it.',
$option
));
throw new NoSuchOptionException(sprintf('The optional option "%s" has no value set. You should make sure it is set with "isset" before reading it.', $option));
}
$value = $this->defaults[$option];
// Resolve the option if it is a nested definition
if (isset($this->nested[$option])) {
// If the closure is already being called, we have a cyclic dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
}
if (!\is_array($value)) {
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $option, $this->formatValue($value), $this->formatTypeOf($value)));
}
// The following section must be protected from cyclic calls.
$this->calling[$option] = true;
try {
$resolver = new self();
foreach ($this->nested[$option] as $closure) {
$closure($resolver, $this);
}
$value = $resolver->resolve($value);
} finally {
unset($this->calling[$option]);
}
}
// Resolve the option if the default value is lazily evaluated
if (isset($this->lazy[$option])) {
// If the closure is already being called, we have a cyclic
// dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf(
'The options "%s" have a cyclic dependency.',
implode('", "', array_keys($this->calling))
));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
}
// The following section must be protected from cyclic
@@ -777,7 +872,7 @@ class OptionsResolver implements Options
$invalidTypes = array();
foreach ($this->allowedTypes[$option] as $type) {
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;
$type = self::$typeAliases[$type] ?? $type;
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
break;
@@ -809,7 +904,9 @@ class OptionsResolver implements Options
// Don't include closures in the exception message
continue;
} elseif ($value === $allowedValue) {
}
if ($value === $allowedValue) {
$success = true;
break;
}
@@ -835,15 +932,38 @@ class OptionsResolver implements Options
}
}
// Check whether the option is deprecated
// and it is provided by the user or is being called from a lazy evaluation
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || ($this->calling && \is_string($this->deprecated[$option])))) {
$deprecationMessage = $this->deprecated[$option];
if ($deprecationMessage instanceof \Closure) {
// If the closure is already being called, we have a cyclic dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
}
$this->calling[$option] = true;
try {
if (!\is_string($deprecationMessage = $deprecationMessage($this, $value))) {
throw new InvalidOptionsException(sprintf('Invalid type for deprecation message, expected string but got "%s", return an empty string to ignore.', \gettype($deprecationMessage)));
}
} finally {
unset($this->calling[$option]);
}
}
if ('' !== $deprecationMessage) {
@trigger_error(strtr($deprecationMessage, array('%name%' => $option)), E_USER_DEPRECATED);
}
}
// Normalize the validated option
if (isset($this->normalizers[$option])) {
// If the closure is already being called, we have a cyclic
// dependency
if (isset($this->calling[$option])) {
throw new OptionDefinitionException(sprintf(
'The options "%s" have a cyclic dependency.',
implode('", "', array_keys($this->calling))
));
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', array_keys($this->calling))));
}
$normalizer = $this->normalizers[$option];
@@ -867,60 +987,35 @@ class OptionsResolver implements Options
return $value;
}
private function verifyTypes(string $type, $value, array &$invalidTypes): bool
private function verifyTypes(string $type, $value, array &$invalidTypes, int $level = 0): bool
{
if (\is_array($value) && '[]' === substr($type, -2)) {
return $this->verifyArrayType($type, $value, $invalidTypes);
$type = substr($type, 0, -2);
foreach ($value as $val) {
if (!$this->verifyTypes($type, $val, $invalidTypes, $level + 1)) {
return false;
}
}
return true;
}
if (self::isValueValidType($type, $value)) {
if (('null' === $type && null === $value) || (\function_exists($func = 'is_'.$type) && $func($value)) || $value instanceof $type) {
return true;
}
if (!$invalidTypes) {
$invalidTypes[$this->formatTypeOf($value, null)] = true;
$suffix = '';
while (\strlen($suffix) < $level * 2) {
$suffix .= '[]';
}
$invalidTypes[$this->formatTypeOf($value).$suffix] = true;
}
return false;
}
private function verifyArrayType(string $type, array $value, array &$invalidTypes, int $level = 0): bool
{
$type = substr($type, 0, -2);
$suffix = '[]';
while (\strlen($suffix) <= $level * 2) {
$suffix .= '[]';
}
if ('[]' === substr($type, -2)) {
$success = true;
foreach ($value as $item) {
if (!\is_array($item)) {
$invalidTypes[$this->formatTypeOf($item, null).$suffix] = true;
return false;
}
if (!$this->verifyArrayType($type, $item, $invalidTypes, $level + 1)) {
$success = false;
}
}
return $success;
}
foreach ($value as $item) {
if (!self::isValueValidType($type, $item)) {
$invalidTypes[$this->formatTypeOf($item, $type).$suffix] = $value;
return false;
}
}
return true;
}
/**
* Returns whether a resolved option with the given name exists.
*
@@ -984,40 +1079,13 @@ class OptionsResolver implements Options
/**
* Returns a string representation of the type of the value.
*
* This method should be used if you pass the type of a value as
* message parameter to a constraint violation. Note that such
* parameters should usually not be included in messages aimed at
* non-technical people.
*
* @param mixed $value The value to return the type of
*
* @return string The type of the value
*/
private function formatTypeOf($value, ?string $type): string
private function formatTypeOf($value): string
{
$suffix = '';
if (null !== $type && '[]' === substr($type, -2)) {
$suffix = '[]';
$type = substr($type, 0, -2);
while ('[]' === substr($type, -2)) {
$type = substr($type, 0, -2);
$value = array_shift($value);
if (!\is_array($value)) {
break;
}
$suffix .= '[]';
}
if (\is_array($value)) {
$subTypes = array();
foreach ($value as $val) {
$subTypes[$this->formatTypeOf($val, null)] = true;
}
return implode('|', array_keys($subTypes)).$suffix;
}
}
return (\is_object($value) ? \get_class($value) : \gettype($value)).$suffix;
return \is_object($value) ? \get_class($value) : \gettype($value);
}
/**
@@ -1078,22 +1146,4 @@ class OptionsResolver implements Options
return implode(', ', $values);
}
private static function isValueValidType(string $type, $value): bool
{
return (\function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type;
}
private function getInvalidValues(array $arrayValues, string $type): array
{
$invalidValues = array();
foreach ($arrayValues as $key => $value) {
if (!self::isValueValidType($type, $value)) {
$invalidValues[$key] = $value;
}
}
return $invalidValues;
}
}

View File

@@ -200,4 +200,49 @@ class OptionsResolverIntrospectorTest extends TestCase
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getNormalizer('foo'));
}
public function testGetDeprecationMessage()
{
$resolver = new OptionsResolver();
$resolver->setDefined('foo');
$resolver->setDeprecated('foo', 'The option "foo" is deprecated.');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('The option "foo" is deprecated.', $debug->getDeprecationMessage('foo'));
}
public function testGetClosureDeprecationMessage()
{
$resolver = new OptionsResolver();
$resolver->setDefined('foo');
$resolver->setDeprecated('foo', $closure = function (Options $options, $value) {});
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($closure, $debug->getDeprecationMessage('foo'));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No deprecation was set for the "foo" option.
*/
public function testGetDeprecationMessageThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined('foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getDeprecationMessage('foo'));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetDeprecationMessageThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getDeprecationMessage('foo'));
}
}

View File

@@ -450,6 +450,326 @@ class OptionsResolverTest extends TestCase
$this->assertFalse($this->resolver->isDefined('foo'));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
*/
public function testFailIfSetDeprecatedFromLazyOption()
{
$this->resolver
->setDefault('bar', 'baz')
->setDefault('foo', function (Options $options) {
$options->setDeprecated('bar');
})
->resolve()
;
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
public function testSetDeprecatedFailsIfUnknownOption()
{
$this->resolver->setDeprecated('foo');
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid type for deprecation message argument, expected string or \Closure, but got "boolean".
*/
public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
{
$this->resolver
->setDefined('foo')
->setDeprecated('foo', true)
;
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid type for deprecation message, expected string but got "boolean", return an empty string to ignore.
*/
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
{
$this->resolver
->setDefined('foo')
->setDeprecated('foo', function (Options $options, $value) {
return false;
})
;
$this->resolver->resolve(array('foo' => null));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
* @expectedExceptionMessage The options "foo", "bar" have a cyclic dependency.
*/
public function testFailsIfCyclicDependencyBetweenDeprecation()
{
$this->resolver
->setDefined(array('foo', 'bar'))
->setDeprecated('foo', function (Options $options, $value) {
$options['bar'];
})
->setDeprecated('bar', function (Options $options, $value) {
$options['foo'];
})
;
$this->resolver->resolve(array('foo' => null, 'bar' => null));
}
public function testIsDeprecated()
{
$this->resolver
->setDefined('foo')
->setDeprecated('foo')
;
$this->assertTrue($this->resolver->isDeprecated('foo'));
}
public function testIsNotDeprecatedIfEmptyString()
{
$this->resolver
->setDefined('foo')
->setDeprecated('foo', '')
;
$this->assertFalse($this->resolver->isDeprecated('foo'));
}
/**
* @dataProvider provideDeprecationData
*/
public function testDeprecationMessages(\Closure $configureOptions, array $options, ?array $expectedError, int $expectedCount)
{
$count = 0;
error_clear_last();
set_error_handler(function () use (&$count) {
++$count;
return false;
});
$e = error_reporting(0);
$configureOptions($this->resolver);
$this->resolver->resolve($options);
error_reporting($e);
restore_error_handler();
$lastError = error_get_last();
unset($lastError['file'], $lastError['line']);
$this->assertSame($expectedError, $lastError);
$this->assertSame($expectedCount, $count);
}
public function provideDeprecationData()
{
yield 'It deprecates an option with default message' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefined(array('foo', 'bar'))
->setDeprecated('foo')
;
},
array('foo' => 'baz'),
array(
'type' => E_USER_DEPRECATED,
'message' => 'The option "foo" is deprecated.',
),
1,
);
yield 'It deprecates an option with custom message' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefined('foo')
->setDefault('bar', function (Options $options) {
return $options['foo'];
})
->setDeprecated('foo', 'The option "foo" is deprecated, use "bar" option instead.')
;
},
array('foo' => 'baz'),
array(
'type' => E_USER_DEPRECATED,
'message' => 'The option "foo" is deprecated, use "bar" option instead.',
),
2,
);
yield 'It deprecates an option evaluated in another definition' => array(
function (OptionsResolver $resolver) {
// defined by superclass
$resolver
->setDefault('foo', null)
->setDeprecated('foo')
;
// defined by subclass
$resolver->setDefault('bar', function (Options $options) {
return $options['foo']; // It triggers a deprecation
});
},
array(),
array(
'type' => E_USER_DEPRECATED,
'message' => 'The option "foo" is deprecated.',
),
1,
);
yield 'It deprecates allowed type and value' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefault('foo', null)
->setAllowedTypes('foo', array('null', 'string', \stdClass::class))
->setDeprecated('foo', function (Options $options, $value) {
if ($value instanceof \stdClass) {
return sprintf('Passing an instance of "%s" to option "foo" is deprecated, pass its FQCN instead.', \stdClass::class);
}
return '';
})
;
},
array('foo' => new \stdClass()),
array(
'type' => E_USER_DEPRECATED,
'message' => 'Passing an instance of "stdClass" to option "foo" is deprecated, pass its FQCN instead.',
),
1,
);
yield 'It triggers a deprecation based on the value only if option is provided by the user' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefined('foo')
->setAllowedTypes('foo', array('null', 'bool'))
->setDeprecated('foo', function (Options $options, $value) {
if (!\is_bool($value)) {
return 'Passing a value different than true or false is deprecated.';
}
return '';
})
->setDefault('baz', null)
->setAllowedTypes('baz', array('null', 'int'))
->setDeprecated('baz', function (Options $options, $value) {
if (!\is_int($value)) {
return 'Not passing an integer is deprecated.';
}
return '';
})
->setDefault('bar', function (Options $options) {
$options['baz']; // It does not triggers a deprecation
return $options['foo']; // It does not triggers a deprecation
})
;
},
array('foo' => null), // It triggers a deprecation
array(
'type' => E_USER_DEPRECATED,
'message' => 'Passing a value different than true or false is deprecated.',
),
1,
);
yield 'It ignores a deprecation if closure returns an empty string' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefault('foo', null)
->setDeprecated('foo', function (Options $options, $value) {
return '';
})
;
},
array('foo' => Bar::class),
null,
0,
);
yield 'It deprecates value depending on other option value' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefault('widget', null)
->setDefault('date_format', null)
->setDeprecated('date_format', function (Options $options, $dateFormat) {
if (null !== $dateFormat && 'single_text' === $options['widget']) {
return 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.';
}
return '';
})
;
},
array('widget' => 'single_text', 'date_format' => 2),
array(
'type' => E_USER_DEPRECATED,
'message' => 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.',
),
1,
);
yield 'It triggers a deprecation for each evaluation' => array(
function (OptionsResolver $resolver) {
$resolver
// defined by superclass
->setDefined('foo')
->setDeprecated('foo')
// defined by subclass
->setDefault('bar', function (Options $options) {
return $options['foo']; // It triggers a deprecation
})
->setNormalizer('bar', function (Options $options, $value) {
$options['foo']; // It triggers a deprecation
$options['foo']; // It triggers a deprecation
return $value;
})
;
},
array('foo' => 'baz'), // It triggers a deprecation
array(
'type' => E_USER_DEPRECATED,
'message' => 'The option "foo" is deprecated.',
),
4,
);
yield 'It ignores a deprecation if no option is provided by the user' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefined('foo')
->setDefault('bar', null)
->setDeprecated('foo')
->setDeprecated('bar')
;
},
array(),
null,
0,
);
yield 'It explicitly ignores a depreciation' => array(
function (OptionsResolver $resolver) {
$resolver
->setDefault('baz', function (Options $options) {
return $options->offsetGet('foo', false);
})
->setDefault('foo', null)
->setDeprecated('foo')
->setDefault('bar', function (Options $options) {
return $options->offsetGet('foo', false);
})
;
},
array(),
null,
0,
);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
@@ -1733,4 +2053,414 @@ class OptionsResolverTest extends TestCase
),
));
}
public function testIsNestedOption()
{
$this->resolver->setDefaults(array(
'database' => function (OptionsResolver $resolver) {
$resolver->setDefined(array('host', 'port'));
},
));
$this->assertTrue($this->resolver->isNested('database'));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist. Defined options are: "host", "port".
*/
public function testFailsIfUndefinedNestedOption()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver->setDefined(array('host', 'port'));
},
));
$this->resolver->resolve(array(
'database' => array('foo' => 'bar'),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
* @expectedExceptionMessage The required option "host" is missing.
*/
public function testFailsIfMissingRequiredNestedOption()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver->setRequired('host');
},
));
$this->resolver->resolve(array(
'database' => array(),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "logging" with value null is expected to be of type "bool", but is of type "NULL".
*/
public function testFailsIfInvalidTypeNestedOption()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver
->setDefined('logging')
->setAllowedTypes('logging', 'bool');
},
));
$this->resolver->resolve(array(
'database' => array('logging' => null),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The nested option "database" with value null is expected to be of type array, but is of type "NULL".
*/
public function testFailsIfNotArrayIsGivenForNestedOptions()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver->setDefined('host');
},
));
$this->resolver->resolve(array(
'database' => null,
));
}
public function testResolveNestedOptionsWithoutDefault()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver->setDefined(array('host', 'port'));
},
));
$actualOptions = $this->resolver->resolve();
$expectedOptions = array(
'name' => 'default',
'database' => array(),
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testResolveNestedOptionsWithDefault()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver->setDefaults(array(
'host' => 'localhost',
'port' => 3306,
));
},
));
$actualOptions = $this->resolver->resolve();
$expectedOptions = array(
'name' => 'default',
'database' => array(
'host' => 'localhost',
'port' => 3306,
),
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testResolveMultipleNestedOptions()
{
$this->resolver->setDefaults(array(
'name' => 'default',
'database' => function (OptionsResolver $resolver) {
$resolver
->setRequired(array('dbname', 'host'))
->setDefaults(array(
'port' => 3306,
'slaves' => function (OptionsResolver $resolver) {
$resolver->setDefaults(array(
'host' => 'slave1',
'port' => 3306,
));
},
));
},
));
$actualOptions = $this->resolver->resolve(array(
'name' => 'custom',
'database' => array(
'dbname' => 'test',
'host' => 'localhost',
'port' => null,
'slaves' => array('host' => 'slave2'),
),
));
$expectedOptions = array(
'name' => 'custom',
'database' => array(
'port' => null,
'slaves' => array('port' => 3306, 'host' => 'slave2'),
'dbname' => 'test',
'host' => 'localhost',
),
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testResolveLazyOptionUsingNestedOption()
{
$this->resolver->setDefaults(array(
'version' => function (Options $options) {
return $options['database']['server_version'];
},
'database' => function (OptionsResolver $resolver) {
$resolver->setDefault('server_version', '3.15');
},
));
$actualOptions = $this->resolver->resolve();
$expectedOptions = array(
'database' => array('server_version' => '3.15'),
'version' => '3.15',
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testNormalizeNestedOptionValue()
{
$this->resolver
->setDefaults(array(
'database' => function (OptionsResolver $resolver) {
$resolver->setDefaults(array(
'port' => 3306,
'host' => 'localhost',
'dbname' => 'demo',
));
},
))
->setNormalizer('database', function (Options $options, $value) {
ksort($value);
return $value;
});
$actualOptions = $this->resolver->resolve(array(
'database' => array('dbname' => 'test'),
));
$expectedOptions = array(
'database' => array('dbname' => 'test', 'host' => 'localhost', 'port' => 3306),
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testOverwrittenNestedOptionNotEvaluatedIfLazyDefault()
{
// defined by superclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
Assert::fail('Should not be called');
});
// defined by subclass
$this->resolver->setDefault('foo', function (Options $options) {
return 'lazy';
});
$this->assertSame(array('foo' => 'lazy'), $this->resolver->resolve());
}
public function testOverwrittenNestedOptionNotEvaluatedIfScalarDefault()
{
// defined by superclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
Assert::fail('Should not be called');
});
// defined by subclass
$this->resolver->setDefault('foo', 'bar');
$this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
}
public function testOverwrittenLazyOptionNotEvaluatedIfNestedOption()
{
// defined by superclass
$this->resolver->setDefault('foo', function (Options $options) {
Assert::fail('Should not be called');
});
// defined by subclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver->setDefault('bar', 'baz');
});
$this->assertSame(array('foo' => array('bar' => 'baz')), $this->resolver->resolve());
}
public function testResolveAllNestedOptionDefinitions()
{
// defined by superclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver->setRequired('bar');
});
// defined by subclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver->setDefault('bar', 'baz');
});
// defined by subclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver->setDefault('ping', 'pong');
});
$this->assertSame(array('foo' => array('ping' => 'pong', 'bar' => 'baz')), $this->resolver->resolve());
}
public function testNormalizeNestedValue()
{
// defined by superclass
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver->setDefault('bar', null);
});
// defined by subclass
$this->resolver->setNormalizer('foo', function (Options $options, $resolvedValue) {
if (null === $resolvedValue['bar']) {
$resolvedValue['bar'] = 'baz';
}
return $resolvedValue;
});
$this->assertSame(array('foo' => array('bar' => 'baz')), $this->resolver->resolve());
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
*/
public function testFailsIfCyclicDependencyBetweenSameNestedOption()
{
$this->resolver->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('slaves', $parent['database']);
});
$this->resolver->resolve();
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
*/
public function testFailsIfCyclicDependencyBetweenNestedOptionAndParentLazyOption()
{
$this->resolver->setDefaults(array(
'version' => function (Options $options) {
return $options['database']['server_version'];
},
'database' => function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('server_version', $parent['version']);
},
));
$this->resolver->resolve();
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
*/
public function testFailsIfCyclicDependencyBetweenNormalizerAndNestedOption()
{
$this->resolver
->setDefault('name', 'default')
->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('host', $parent['name']);
})
->setNormalizer('name', function (Options $options, $value) {
$options['database'];
});
$this->resolver->resolve();
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
*/
public function testFailsIfCyclicDependencyBetweenNestedOptions()
{
$this->resolver->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('host', $parent['slave']['host']);
});
$this->resolver->setDefault('slave', function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('host', $parent['database']['host']);
});
$this->resolver->resolve();
}
public function testGetAccessToParentOptionFromNestedOption()
{
$this->resolver->setDefaults(array(
'version' => 3.15,
'database' => function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('server_version', $parent['version']);
},
));
$this->assertSame(array('version' => 3.15, 'database' => array('server_version' => 3.15)), $this->resolver->resolve());
}
public function testNestedClosureWithoutTypeHintNotInvoked()
{
$closure = function ($resolver) {
Assert::fail('Should not be called');
};
$this->resolver->setDefault('foo', $closure);
$this->assertSame(array('foo' => $closure), $this->resolver->resolve());
}
public function testNestedClosureWithoutTypeHint2ndArgumentNotInvoked()
{
$closure = function (OptionsResolver $resolver, $parent) {
Assert::fail('Should not be called');
};
$this->resolver->setDefault('foo', $closure);
$this->assertSame(array('foo' => $closure), $this->resolver->resolve());
}
public function testResolveLazyOptionWithTransitiveDefaultDependency()
{
$this->resolver->setDefaults(array(
'ip' => null,
'database' => function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('host', $parent['ip']);
$resolver->setDefault('master_slave', function (OptionsResolver $resolver, Options $parent) {
$resolver->setDefault('host', $parent['host']);
});
},
'secondary_slave' => function (Options $options) {
return $options['database']['master_slave']['host'];
},
));
$actualOptions = $this->resolver->resolve(array('ip' => '127.0.0.1'));
$expectedOptions = array(
'ip' => '127.0.0.1',
'database' => array(
'host' => '127.0.0.1',
'master_slave' => array('host' => '127.0.0.1'),
),
'secondary_slave' => '127.0.0.1',
);
$this->assertSame($expectedOptions, $actualOptions);
}
public function testAccessToParentOptionFromNestedNormalizerAndLazyOption()
{
$this->resolver->setDefaults(array(
'debug' => true,
'database' => function (OptionsResolver $resolver, Options $parent) {
$resolver
->setDefined('logging')
->setDefault('profiling', function (Options $options) use ($parent) {
return $parent['debug'];
})
->setNormalizer('logging', function (Options $options, $value) use ($parent) {
return false === $parent['debug'] ? true : $value;
});
},
));
$actualOptions = $this->resolver->resolve(array(
'debug' => false,
'database' => array('logging' => false),
));
$expectedOptions = array(
'debug' => false,
'database' => array('profiling' => false, 'logging' => true),
);
$this->assertSame($expectedOptions, $actualOptions);
}
}

View File

@@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "4.1-dev"
"dev-master": "4.2-dev"
}
}
}

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"