Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -1,6 +1,12 @@
CHANGELOG
=========
3.4.0
-----
* added `OptionsResolverIntrospector` to inspect options definitions inside an `OptionsResolver` instance
* added array of types support in allowed types (e.g int[])
2.6.0
-----
@@ -25,7 +31,7 @@ CHANGELOG
* deprecated OptionsResolver::isKnown() in favor of isDefined()
* [BC BREAK] OptionsResolver::isRequired() returns true now if a required
option has a default value set
* [BC BREAK] merged Options into OptionsResolver and turned Options into an
* [BC BREAK] merged Options into OptionsResolver and turned Options into an
interface
* deprecated Options::overload() (now in OptionsResolver)
* deprecated Options::set() (now in OptionsResolver)
@@ -36,7 +42,7 @@ CHANGELOG
lazy option/normalizer closures now
* [BC BREAK] removed Traversable interface from Options since using within
lazy option/normalizer closures resulted in exceptions
* [BC BREAK] removed Options::all() since using within lazy option/normalizer
* [BC BREAK] removed Options::all() since using within lazy option/normalizer
closures resulted in exceptions
* [BC BREAK] OptionDefinitionException now extends LogicException instead of
RuntimeException

View File

@@ -0,0 +1,90 @@
<?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\OptionsResolver\Debug;
use Symfony\Component\OptionsResolver\Exception\NoConfigurationException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @final
*/
class OptionsResolverIntrospector
{
private $get;
public function __construct(OptionsResolver $optionsResolver)
{
$this->get = \Closure::bind(function ($property, $option, $message) {
/** @var OptionsResolver $this */
if (!$this->isDefined($option)) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist.', $option));
}
if (!array_key_exists($option, $this->{$property})) {
throw new NoConfigurationException($message);
}
return $this->{$property}[$option];
}, $optionsResolver, $optionsResolver);
}
/**
* @return mixed
*
* @throws NoConfigurationException on no configured value
*/
public function getDefault(string $option)
{
return \call_user_func($this->get, 'defaults', $option, sprintf('No default value was set for the "%s" option.', $option));
}
/**
* @return \Closure[]
*
* @throws NoConfigurationException on no configured closures
*/
public function getLazyClosures(string $option): array
{
return \call_user_func($this->get, 'lazy', $option, sprintf('No lazy closures were set for the "%s" option.', $option));
}
/**
* @return string[]
*
* @throws NoConfigurationException on no configured types
*/
public function getAllowedTypes(string $option): array
{
return \call_user_func($this->get, 'allowedTypes', $option, sprintf('No allowed types were set for the "%s" option.', $option));
}
/**
* @return mixed[]
*
* @throws NoConfigurationException on no configured values
*/
public function getAllowedValues(string $option): array
{
return \call_user_func($this->get, 'allowedValues', $option, sprintf('No allowed values were set for the "%s" option.', $option));
}
/**
* @throws NoConfigurationException on no configured normalizer
*/
public function getNormalizer(string $option): \Closure
{
return \call_user_func($this->get, 'normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option));
}
}

View File

@@ -0,0 +1,26 @@
<?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\OptionsResolver\Exception;
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
/**
* Thrown when trying to introspect an option definition property
* for which no value was configured inside the OptionsResolver instance.
*
* @see OptionsResolverIntrospector
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class NoConfigurationException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -1,4 +1,4 @@
Copyright (c) 2004-2016 Fabien Potencier
Copyright (c) 2004-2018 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -28,29 +28,21 @@ class OptionsResolver implements Options
{
/**
* The names of all defined options.
*
* @var array
*/
private $defined = array();
/**
* The default option values.
*
* @var array
*/
private $defaults = array();
/**
* The names of required options.
*
* @var array
*/
private $required = array();
/**
* The resolved option values.
*
* @var array
*/
private $resolved = array();
@@ -63,22 +55,16 @@ class OptionsResolver implements Options
/**
* A list of accepted values for each option.
*
* @var array
*/
private $allowedValues = array();
/**
* A list of accepted types for each option.
*
* @var array
*/
private $allowedTypes = array();
/**
* A list of closures for evaluating lazy options.
*
* @var array
*/
private $lazy = array();
@@ -86,8 +72,6 @@ class OptionsResolver implements Options
* A list of lazy options whose closure is currently being called.
*
* This list helps detecting circular dependencies between lazy options.
*
* @var array
*/
private $calling = array();
@@ -98,8 +82,6 @@ class OptionsResolver implements Options
* necessary in order to avoid inconsistencies during the resolving
* process. If any option is changed after being read, all evaluated
* lazy options that depend on this option would become invalid.
*
* @var bool
*/
private $locked = false;
@@ -145,7 +127,7 @@ class OptionsResolver implements Options
* @param string $option The name of the option
* @param mixed $value The default value of the option
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -208,7 +190,7 @@ class OptionsResolver implements Options
*
* @param array $defaults The default values to set
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -241,7 +223,7 @@ class OptionsResolver implements Options
*
* @param string|string[] $optionNames One or more option names
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -322,7 +304,7 @@ class OptionsResolver implements Options
*
* @param string|string[] $optionNames One or more option names
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -389,7 +371,7 @@ class OptionsResolver implements Options
* @param string $option The option name
* @param \Closure $normalizer The normalizer
*
* @return OptionsResolver This instance
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
@@ -432,7 +414,7 @@ class OptionsResolver implements Options
* @param string $option The option name
* @param mixed $allowedValues One or more acceptable values/closures
*
* @return OptionsResolver This instance
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
@@ -451,7 +433,7 @@ class OptionsResolver implements Options
));
}
$this->allowedValues[$option] = is_array($allowedValues) ? $allowedValues : array($allowedValues);
$this->allowedValues[$option] = \is_array($allowedValues) ? $allowedValues : array($allowedValues);
// Make sure the option is processed
unset($this->resolved[$option]);
@@ -477,7 +459,7 @@ class OptionsResolver implements Options
* @param string $option The option name
* @param mixed $allowedValues One or more acceptable values/closures
*
* @return OptionsResolver This instance
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
@@ -496,7 +478,7 @@ class OptionsResolver implements Options
));
}
if (!is_array($allowedValues)) {
if (!\is_array($allowedValues)) {
$allowedValues = array($allowedValues);
}
@@ -522,7 +504,7 @@ class OptionsResolver implements Options
* @param string $option The option name
* @param string|string[] $allowedTypes One or more accepted types
*
* @return OptionsResolver This instance
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
@@ -561,7 +543,7 @@ class OptionsResolver implements Options
* @param string $option The option name
* @param string|string[] $allowedTypes One or more accepted types
*
* @return OptionsResolver This instance
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
@@ -599,7 +581,7 @@ class OptionsResolver implements Options
*
* @param string|string[] $optionNames One or more option names
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -620,7 +602,7 @@ class OptionsResolver implements Options
/**
* Removes all options.
*
* @return OptionsResolver This instance
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
@@ -678,12 +660,12 @@ class OptionsResolver implements Options
// Make sure that no unknown options are passed
$diff = array_diff_key($options, $clone->defined);
if (count($diff) > 0) {
if (\count($diff) > 0) {
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".',
(\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))
));
@@ -698,11 +680,11 @@ class OptionsResolver implements Options
// Check whether any required option is missing
$diff = array_diff_key($clone->required, $clone->defaults);
if (count($diff) > 0) {
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.',
\count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.',
implode('", "', array_keys($diff))
));
}
@@ -792,34 +774,24 @@ class OptionsResolver implements Options
// Validate the type of the resolved option
if (isset($this->allowedTypes[$option])) {
$valid = false;
$invalidTypes = array();
foreach ($this->allowedTypes[$option] as $type) {
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;
if (function_exists($isFunction = 'is_'.$type)) {
if ($isFunction($value)) {
$valid = true;
break;
}
continue;
}
if ($value instanceof $type) {
$valid = true;
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
break;
}
}
if (!$valid) {
throw new InvalidOptionsException(sprintf(
'The option "%s" with value %s is expected to be of type '.
'"%s", but is of type "%s".',
$option,
$this->formatValue($value),
implode('" or "', $this->allowedTypes[$option]),
$this->formatTypeOf($value)
));
$keys = array_keys($invalidTypes);
if (1 === \count($keys) && '[]' === substr($keys[0], -2)) {
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but one of the elements is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), $keys[0]));
}
throw new InvalidOptionsException(sprintf('The option "%s" with value %s is expected to be of type "%s", but is of type "%s".', $option, $this->formatValue($value), implode('" or "', $this->allowedTypes[$option]), implode('|', array_keys($invalidTypes))));
}
}
@@ -852,7 +824,7 @@ class OptionsResolver implements Options
$this->formatValue($value)
);
if (count($printableAllowedValues) > 0) {
if (\count($printableAllowedValues) > 0) {
$message .= sprintf(
' Accepted values are: %s.',
$this->formatValues($printableAllowedValues)
@@ -895,6 +867,60 @@ class OptionsResolver implements Options
return $value;
}
private function verifyTypes(string $type, $value, array &$invalidTypes): bool
{
if (\is_array($value) && '[]' === substr($type, -2)) {
return $this->verifyArrayType($type, $value, $invalidTypes);
}
if (self::isValueValidType($type, $value)) {
return true;
}
if (!$invalidTypes) {
$invalidTypes[$this->formatTypeOf($value, null)] = 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.
*
@@ -952,7 +978,7 @@ class OptionsResolver implements Options
throw new AccessException('Counting is only supported within closures of lazy options and normalizers.');
}
return count($this->defaults);
return \count($this->defaults);
}
/**
@@ -964,12 +990,34 @@ class OptionsResolver implements Options
* non-technical people.
*
* @param mixed $value The value to return the type of
*
* @return string The type of the value
*/
private function formatTypeOf($value)
private function formatTypeOf($value, ?string $type): string
{
return is_object($value) ? get_class($value) : gettype($value);
$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;
}
/**
@@ -980,24 +1028,22 @@ class OptionsResolver implements Options
* in double quotes (").
*
* @param mixed $value The value to format as string
*
* @return string The string representation of the passed value
*/
private function formatValue($value)
private function formatValue($value): string
{
if (is_object($value)) {
return get_class($value);
if (\is_object($value)) {
return \get_class($value);
}
if (is_array($value)) {
if (\is_array($value)) {
return 'array';
}
if (is_string($value)) {
if (\is_string($value)) {
return '"'.$value.'"';
}
if (is_resource($value)) {
if (\is_resource($value)) {
return 'resource';
}
@@ -1022,13 +1068,9 @@ class OptionsResolver implements Options
* Each of the values is converted to a string using
* {@link formatValue()}. The values are then concatenated with commas.
*
* @param array $values A list of values
*
* @return string The string representation of the value list
*
* @see formatValue()
*/
private function formatValues(array $values)
private function formatValues(array $values): string
{
foreach ($values as $key => $value) {
$values[$key] = $this->formatValue($value);
@@ -1036,4 +1078,22 @@ 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

@@ -0,0 +1,203 @@
<?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\OptionsResolver\Tests\Debug;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OptionsResolverIntrospectorTest extends TestCase
{
public function testGetDefault()
{
$resolver = new OptionsResolver();
$resolver->setDefault($option = 'foo', 'bar');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getDefault($option));
}
public function testGetDefaultNull()
{
$resolver = new OptionsResolver();
$resolver->setDefault($option = 'foo', null);
$debug = new OptionsResolverIntrospector($resolver);
$this->assertNull($debug->getDefault($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No default value was set for the "foo" option.
*/
public function testGetDefaultThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getDefault($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetDefaultThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getDefault('foo'));
}
public function testGetLazyClosures()
{
$resolver = new OptionsResolver();
$closures = array();
$resolver->setDefault($option = 'foo', $closures[] = function (Options $options) {});
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($closures, $debug->getLazyClosures($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No lazy closures were set for the "foo" option.
*/
public function testGetLazyClosuresThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getLazyClosures($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetLazyClosuresThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getLazyClosures('foo'));
}
public function testGetAllowedTypes()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$resolver->setAllowedTypes($option = 'foo', $allowedTypes = array('string', 'bool'));
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($allowedTypes, $debug->getAllowedTypes($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No allowed types were set for the "foo" option.
*/
public function testGetAllowedTypesThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getAllowedTypes($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetAllowedTypesThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getAllowedTypes('foo'));
}
public function testGetAllowedValues()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$resolver->setAllowedValues($option = 'foo', $allowedValues = array('bar', 'baz'));
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($allowedValues, $debug->getAllowedValues($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No allowed values were set for the "foo" option.
*/
public function testGetAllowedValuesThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getAllowedValues($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetAllowedValuesThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getAllowedValues('foo'));
}
public function testGetNormalizer()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$resolver->setNormalizer($option = 'foo', $normalizer = function () {});
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame($normalizer, $debug->getNormalizer($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
* @expectedExceptionMessage No normalizer was set for the "foo" option.
*/
public function testGetNormalizerThrowsOnNoConfiguredValue()
{
$resolver = new OptionsResolver();
$resolver->setDefined($option = 'foo');
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getNormalizer($option));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist.
*/
public function testGetNormalizerThrowsOnNotDefinedOption()
{
$resolver = new OptionsResolver();
$debug = new OptionsResolverIntrospector($resolver);
$this->assertSame('bar', $debug->getNormalizer('foo'));
}
}

View File

@@ -11,11 +11,13 @@
namespace Symfony\Component\OptionsResolver\Tests;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OptionsResolverTest extends \PHPUnit_Framework_TestCase
class OptionsResolverTest extends TestCase
{
/**
* @var OptionsResolver
@@ -27,10 +29,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver = new OptionsResolver();
}
////////////////////////////////////////////////////////////////////////////
// resolve()
////////////////////////////////////////////////////////////////////////////
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @expectedExceptionMessage The option "foo" does not exist. Defined options are: "a", "z".
@@ -67,10 +65,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->resolve();
}
////////////////////////////////////////////////////////////////////////////
// setDefault()/hasDefault()
////////////////////////////////////////////////////////////////////////////
public function testSetDefaultReturnsThis()
{
$this->assertSame($this->resolver, $this->resolver->setDefault('foo', 'bar'));
@@ -113,10 +107,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($this->resolver->hasDefault('foo'));
}
////////////////////////////////////////////////////////////////////////////
// lazy setDefault()
////////////////////////////////////////////////////////////////////////////
public function testSetLazyReturnsThis()
{
$this->assertSame($this->resolver, $this->resolver->setDefault('foo', function (Options $options) {}));
@@ -134,7 +124,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
public function testClosureWithoutTypeHintNotInvoked()
{
$closure = function ($options) {
\PHPUnit_Framework_Assert::fail('Should not be called');
Assert::fail('Should not be called');
};
$this->resolver->setDefault('foo', $closure);
@@ -145,7 +135,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
public function testClosureWithoutParametersNotInvoked()
{
$closure = function () {
\PHPUnit_Framework_Assert::fail('Should not be called');
Assert::fail('Should not be called');
};
$this->resolver->setDefault('foo', $closure);
@@ -160,7 +150,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
// defined by subclass
$this->resolver->setDefault('foo', function (Options $options, $previousValue) {
\PHPUnit_Framework_Assert::assertEquals('bar', $previousValue);
Assert::assertEquals('bar', $previousValue);
return 'lazy';
});
@@ -177,7 +167,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
// defined by subclass
$this->resolver->setDefault('foo', function (Options $options, $previousValue) {
\PHPUnit_Framework_Assert::assertEquals('bar', $previousValue);
Assert::assertEquals('bar', $previousValue);
return 'lazy';
});
@@ -189,7 +179,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
{
// defined by superclass
$this->resolver->setDefault('foo', function () {
\PHPUnit_Framework_Assert::fail('Should not be called');
Assert::fail('Should not be called');
});
// defined by subclass, no $previousValue argument defined!
@@ -203,7 +193,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
public function testOverwrittenLazyOptionNotEvaluated()
{
$this->resolver->setDefault('foo', function (Options $options) {
\PHPUnit_Framework_Assert::fail('Should not be called');
Assert::fail('Should not be called');
});
$this->resolver->setDefault('foo', 'bar');
@@ -216,13 +206,13 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$calls = 0;
$this->resolver->setDefault('lazy1', function (Options $options) use (&$calls) {
\PHPUnit_Framework_Assert::assertSame(1, ++$calls);
Assert::assertSame(1, ++$calls);
$options['lazy2'];
});
$this->resolver->setDefault('lazy2', function (Options $options) use (&$calls) {
\PHPUnit_Framework_Assert::assertSame(2, ++$calls);
Assert::assertSame(2, ++$calls);
});
$this->resolver->resolve();
@@ -230,10 +220,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertSame(2, $calls);
}
////////////////////////////////////////////////////////////////////////////
// setRequired()/isRequired()/getRequiredOptions()
////////////////////////////////////////////////////////////////////////////
public function testSetRequiredReturnsThis()
{
$this->assertSame($this->resolver, $this->resolver->setRequired('foo'));
@@ -328,10 +314,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('foo', 'bar'), $this->resolver->getRequiredOptions());
}
////////////////////////////////////////////////////////////////////////////
// isMissing()/getMissingOptions()
////////////////////////////////////////////////////////////////////////////
public function testIsMissingIfNotSet()
{
$this->assertFalse($this->resolver->isMissing('foo'));
@@ -371,10 +353,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('bar'), $this->resolver->getMissingOptions());
}
////////////////////////////////////////////////////////////////////////////
// setDefined()/isDefined()/getDefinedOptions()
////////////////////////////////////////////////////////////////////////////
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
*/
@@ -472,10 +450,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->resolver->isDefined('foo'));
}
////////////////////////////////////////////////////////////////////////////
// setAllowedTypes()
////////////////////////////////////////////////////////////////////////////
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
@@ -484,6 +458,15 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setAllowedTypes('foo', 'string');
}
public function testResolveTypedArray()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[]');
$options = $this->resolver->resolve(array('foo' => array('bar', 'baz')));
$this->assertSame(array('foo' => array('bar', 'baz')), $options);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
*/
@@ -498,6 +481,65 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->resolve();
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime[]".
*/
public function testResolveFailsIfInvalidTypedArray()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');
$this->resolver->resolve(array('foo' => array(new \DateTime())));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value "bar" is expected to be of type "int[]", but is of type "string".
*/
public function testResolveFailsWithNonArray()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');
$this->resolver->resolve(array('foo' => 'bar'));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass[]".
*/
public function testResolveFailsIfTypedArrayContainsInvalidTypes()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[]');
$values = range(1, 5);
$values[] = new \stdClass();
$values[] = array();
$values[] = new \DateTime();
$values[] = 123;
$this->resolver->resolve(array('foo' => $values));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double[][]".
*/
public function testResolveFailsWithCorrectLevelsButWrongScalar()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->resolver->resolve(
array(
'foo' => array(
array(1.2),
),
)
);
}
/**
* @dataProvider provideInvalidTypes
*/
@@ -505,7 +547,14 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
{
$this->resolver->setDefined('option');
$this->resolver->setAllowedTypes('option', $allowedType);
$this->setExpectedException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException', $exceptionMessage);
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->setExpectedException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException', $exceptionMessage);
}
$this->resolver->resolve(array('option' => $actualType));
}
@@ -559,9 +608,31 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertNotEmpty($this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// addAllowedTypes()
////////////////////////////////////////////////////////////////////////////
public function testResolveSucceedsIfTypedArray()
{
$this->resolver->setDefault('foo', null);
$this->resolver->setAllowedTypes('foo', array('null', 'DateTime[]'));
$data = array(
'foo' => array(
new \DateTime(),
new \DateTime(),
),
);
$result = $this->resolver->resolve($data);
$this->assertEquals($data, $result);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testResolveFailsIfNotInstanceOfClass()
{
$this->resolver->setDefault('foo', 'bar');
$this->resolver->setAllowedTypes('foo', '\stdClass');
$this->resolver->resolve();
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
@@ -645,10 +716,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertNotEmpty($this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// setAllowedValues()
////////////////////////////////////////////////////////////////////////////
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
@@ -800,10 +867,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// addAllowedValues()
////////////////////////////////////////////////////////////////////////////
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
@@ -920,10 +983,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// setNormalizer()
////////////////////////////////////////////////////////////////////////////
public function testSetNormalizerReturnsThis()
{
$this->resolver->setDefault('foo', 'bar');
@@ -996,7 +1055,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setAllowedTypes('foo', 'int');
$this->resolver->setNormalizer('foo', function () {
\PHPUnit_Framework_Assert::fail('Should not be called.');
Assert::fail('Should not be called.');
});
$this->resolver->resolve();
@@ -1012,7 +1071,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setAllowedValues('foo', 'baz');
$this->resolver->setNormalizer('foo', function () {
\PHPUnit_Framework_Assert::fail('Should not be called.');
Assert::fail('Should not be called.');
});
$this->resolver->resolve();
@@ -1024,8 +1083,8 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefault('norm', 'baz');
$this->resolver->setNormalizer('norm', function (Options $options) {
/* @var \PHPUnit_Framework_TestCase $test */
\PHPUnit_Framework_Assert::assertSame('bar', $options['default']);
/* @var TestCase $test */
Assert::assertSame('bar', $options['default']);
return 'normalized';
});
@@ -1044,8 +1103,8 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefault('norm', 'baz');
$this->resolver->setNormalizer('norm', function (Options $options) {
/* @var \PHPUnit_Framework_TestCase $test */
\PHPUnit_Framework_Assert::assertEquals('bar', $options['lazy']);
/* @var TestCase $test */
Assert::assertEquals('bar', $options['lazy']);
return 'normalized';
});
@@ -1093,7 +1152,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->resolve();
}
public function testCatchedExceptionFromNormalizerDoesNotCrashOptionResolver()
public function testCaughtExceptionFromNormalizerDoesNotCrashOptionResolver()
{
$throw = true;
@@ -1107,7 +1166,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
}
});
$this->resolver->setNormalizer('thrower', function (Options $options) use (&$throw) {
$this->resolver->setNormalizer('thrower', function () use (&$throw) {
if ($throw) {
$throw = false;
throw new \UnexpectedValueException('throwing');
@@ -1116,10 +1175,10 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
return true;
});
$this->resolver->resolve();
$this->assertSame(array('catcher' => false, 'thrower' => true), $this->resolver->resolve());
}
public function testCatchedExceptionFromLazyDoesNotCrashOptionResolver()
public function testCaughtExceptionFromLazyDoesNotCrashOptionResolver()
{
$throw = true;
@@ -1140,7 +1199,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
return true;
});
$this->resolver->resolve();
$this->assertSame(array('catcher' => false, 'thrower' => true), $this->resolver->resolve());
}
public function testInvokeEachNormalizerOnlyOnce()
@@ -1151,12 +1210,12 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefault('norm2', 'baz');
$this->resolver->setNormalizer('norm1', function ($options) use (&$calls) {
\PHPUnit_Framework_Assert::assertSame(1, ++$calls);
Assert::assertSame(1, ++$calls);
$options['norm2'];
});
$this->resolver->setNormalizer('norm2', function () use (&$calls) {
\PHPUnit_Framework_Assert::assertSame(2, ++$calls);
Assert::assertSame(2, ++$calls);
});
$this->resolver->resolve();
@@ -1169,16 +1228,12 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefined('norm');
$this->resolver->setNormalizer('norm', function () {
\PHPUnit_Framework_Assert::fail('Should not be called.');
Assert::fail('Should not be called.');
});
$this->assertEmpty($this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// setDefaults()
////////////////////////////////////////////////////////////////////////////
public function testSetDefaultsReturnsThis()
{
$this->assertSame($this->resolver, $this->resolver->setDefaults(array('foo', 'bar')));
@@ -1213,10 +1268,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->resolve();
}
////////////////////////////////////////////////////////////////////////////
// remove()
////////////////////////////////////////////////////////////////////////////
public function testRemoveReturnsThis()
{
$this->resolver->setDefault('foo', 'bar');
@@ -1305,10 +1356,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertNotNull($this->resolver->remove('foo'));
}
////////////////////////////////////////////////////////////////////////////
// clear()
////////////////////////////////////////////////////////////////////////////
public function testClearReturnsThis()
{
$this->assertSame($this->resolver, $this->resolver->clear());
@@ -1395,10 +1442,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->assertEmpty($this->resolver->resolve());
}
////////////////////////////////////////////////////////////////////////////
// ArrayAccess
////////////////////////////////////////////////////////////////////////////
public function testArrayAccess()
{
$this->resolver->setDefault('default1', 0);
@@ -1410,17 +1453,17 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
});
$this->resolver->setDefault('lazy2', function (Options $options) {
\PHPUnit_Framework_Assert::assertTrue(isset($options['default1']));
\PHPUnit_Framework_Assert::assertTrue(isset($options['default2']));
\PHPUnit_Framework_Assert::assertTrue(isset($options['required']));
\PHPUnit_Framework_Assert::assertTrue(isset($options['lazy1']));
\PHPUnit_Framework_Assert::assertTrue(isset($options['lazy2']));
\PHPUnit_Framework_Assert::assertFalse(isset($options['defined']));
Assert::assertArrayHasKey('default1', $options);
Assert::assertArrayHasKey('default2', $options);
Assert::assertArrayHasKey('required', $options);
Assert::assertArrayHasKey('lazy1', $options);
Assert::assertArrayHasKey('lazy2', $options);
Assert::assertArrayNotHasKey('defined', $options);
\PHPUnit_Framework_Assert::assertSame(0, $options['default1']);
\PHPUnit_Framework_Assert::assertSame(42, $options['default2']);
\PHPUnit_Framework_Assert::assertSame('value', $options['required']);
\PHPUnit_Framework_Assert::assertSame('lazy', $options['lazy1']);
Assert::assertSame(0, $options['default1']);
Assert::assertSame(42, $options['default2']);
Assert::assertSame('value', $options['required']);
Assert::assertSame('lazy', $options['lazy1']);
// Obviously $options['lazy'] and $options['defined'] cannot be
// accessed
@@ -1513,10 +1556,6 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->resolve();
}
////////////////////////////////////////////////////////////////////////////
// Countable
////////////////////////////////////////////////////////////////////////////
public function testCount()
{
$this->resolver->setDefault('default', 0);
@@ -1525,7 +1564,7 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefault('lazy1', function () {});
$this->resolver->setDefault('lazy2', function (Options $options) {
\PHPUnit_Framework_Assert::assertCount(4, $options);
Assert::assertCount(4, $options);
});
$this->assertCount(4, $this->resolver->resolve(array('required' => 'value')));
@@ -1545,6 +1584,153 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
$this->resolver->setDefined('bar');
$this->resolver->setDefault('lazy1', function () {});
count($this->resolver);
\count($this->resolver);
}
public function testNestedArrays()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->assertEquals(array(
'foo' => array(
array(
1, 2,
),
),
), $this->resolver->resolve(
array(
'foo' => array(
array(1, 2),
),
)
));
}
public function testNested2Arrays()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][][][]');
$this->assertEquals(array(
'foo' => array(
array(
array(
array(
1, 2,
),
),
),
),
), $this->resolver->resolve(
array(
'foo' => array(
array(
array(
array(1, 2),
),
),
),
)
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer[][][][]".
*/
public function testNestedArraysException()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'float[][][][]');
$this->resolver->resolve(
array(
'foo' => array(
array(
array(
array(1, 2),
),
),
),
)
);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
*/
public function testNestedArrayException1()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->resolver->resolve(array(
'foo' => array(
array(1, true, 'str', array(2, 3)),
),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
*/
public function testNestedArrayException2()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'int[][]');
$this->resolver->resolve(array(
'foo' => array(
array(true, 'str', array(2, 3)),
),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string[][]".
*/
public function testNestedArrayException3()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[][][]');
$this->resolver->resolve(array(
'foo' => array(
array('str', array(1, 2)),
),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer[][][]".
*/
public function testNestedArrayException4()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[][][]');
$this->resolver->resolve(array(
'foo' => array(
array(
array('str'), array(1, 2), ),
),
));
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array[]".
*/
public function testNestedArrayException5()
{
$this->resolver->setDefined('foo');
$this->resolver->setAllowedTypes('foo', 'string[]');
$this->resolver->resolve(array(
'foo' => array(
array(
array('str'), array(1, 2), ),
),
));
}
}

View File

@@ -16,7 +16,7 @@
}
],
"require": {
"php": ">=5.5.9"
"php": "^7.1.3"
},
"autoload": {
"psr-4": { "Symfony\\Component\\OptionsResolver\\": "" },
@@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
"dev-master": "4.1-dev"
}
}
}

View File

@@ -5,6 +5,8 @@
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />