Merge branch '3.1' into 3.2

* 3.1:
  fixed typo
  fixed composer.json
  always check for all fields to be mapped
  clarify exception when no args are configured
  [PropertyAccess] Handle interfaces in the invalid argument exception
  [DI] Fix defaults overriding empty strings in AutowirePass
  [Debug] Workaround "null" $context
  [Debug] Remove $context arg from handleError(), preparing for PHP 7.2
  [Routing] Fix BC break in AnnotationClassLoader defaults attributes handling
  Fix tests with ICU 57.1
  Fix the condition checking the minimum ICU version
This commit is contained in:
Fabien Potencier 2017-01-27 18:37:08 -08:00
commit 09d5f4eb72
20 changed files with 253 additions and 29 deletions

View File

@ -90,7 +90,8 @@
"symfony/phpunit-bridge": "~3.2",
"symfony/polyfill-apcu": "~1.1",
"symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0"
"phpdocumentor/reflection-docblock": "^3.0",
"sensio/framework-extra-bundle": "^3.0.2"
},
"conflict": {
"phpdocumentor/reflection-docblock": "<3.0",

View File

@ -264,6 +264,23 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
->assertRaised();
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name', 'name2'),
'em' => self::EM_NAME,
'ignoreNull' => true,
));
$entity1 = new SingleIntIdEntity(1, null);
$this->validator->validate($entity1, $constraint);
}
public function testValidateUniquenessWithValidCustomErrorPath()
{
$constraint = new UniqueEntity(array(

View File

@ -86,12 +86,14 @@ class UniqueEntityValidator extends ConstraintValidator
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
}
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
if ($constraint->ignoreNull && null === $criteria[$fieldName]) {
return;
if ($constraint->ignoreNull && null === $fieldValue) {
continue;
}
$criteria[$fieldName] = $fieldValue;
if (null !== $criteria[$fieldName] && $class->hasAssociation($fieldName)) {
/* Ensure the Proxy is initialized before using reflection to
* read its identifiers. This is necessary because the wrapped
@ -101,6 +103,12 @@ class UniqueEntityValidator extends ConstraintValidator
}
}
// skip validation if there are no criteria (this can happen when the
// "ignoreNull" option is enabled and fields to be checked are null
if (empty($criteria)) {
return;
}
if (null !== $constraint->entityClass) {
/* Retrieve repository from given entity name.
* We ensure the retrieved repository can handle the entity

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\Bundle\FrameworkBundle\Tests\Functional;
class AnnotatedControllerTest extends WebTestCase
{
/**
* @dataProvider getRoutes
*/
public function testAnnotatedController($path, $expectedValue)
{
$client = $this->createClient(array('test_case' => 'AnnotatedController', 'root_config' => 'config.yml'));
$client->request('GET', '/annotated'.$path);
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame($expectedValue, $client->getResponse()->getContent());
}
public function getRoutes()
{
return array(
array('/null_request', 'Symfony\Component\HttpFoundation\Request'),
array('/null_argument', ''),
array('/null_argument_with_route_param', ''),
array('/null_argument_with_route_param/value', 'value'),
array('/argument_with_route_param_and_default', 'value'),
array('/argument_with_route_param_and_default/custom', 'custom'),
);
}
}

View File

@ -0,0 +1,51 @@
<?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AnnotatedController
{
/**
* @Route("/null_request", name="null_request")
*/
public function requestDefaultNullAction(Request $request = null)
{
return new Response($request ? get_class($request) : null);
}
/**
* @Route("/null_argument", name="null_argument")
*/
public function argumentDefaultNullWithoutRouteParamAction($value = null)
{
return new Response($value);
}
/**
* @Route("/null_argument_with_route_param/{value}", name="null_argument_with_route_param")
*/
public function argumentDefaultNullWithRouteParamAction($value = null)
{
return new Response($value);
}
/**
* @Route("/argument_with_route_param_and_default/{value}", defaults={"value": "value"}, name="argument_with_route_param_and_default")
*/
public function argumentWithoutDefaultWithRouteParamAndDefaultAction($value)
{
return new Response($value);
}
}

View File

@ -0,0 +1,20 @@
<?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.
*/
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
return array(
new FrameworkBundle(),
new TestBundle(),
new SensioFrameworkExtraBundle(),
);

View File

@ -0,0 +1,2 @@
imports:
- { resource: ../config/default.yml }

View File

@ -0,0 +1,4 @@
annotated_controller:
prefix: /annotated
resource: "@TestBundle/Controller/AnnotatedController.php"
type: annotation

View File

@ -27,7 +27,9 @@
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "~2.8|~3.0",
"symfony/finder": "~2.8|~3.0",
"symfony/routing": "~3.0",
"symfony/routing": "~3.1.10|~3.2.3",
"symfony/security-core": "~2.8|~3.0",
"symfony/security-csrf": "~2.8|~3.0",
"symfony/stopwatch": "~2.8|~3.0",
"doctrine/cache": "~1.0"
},
@ -52,7 +54,8 @@
"symfony/property-info": "~2.8|~3.0",
"doctrine/annotations": "~1.0",
"phpdocumentor/reflection-docblock": "^3.0",
"twig/twig": "~1.26|~2.0"
"twig/twig": "~1.26|~2.0",
"sensio/framework-extra-bundle": "^3.0.2"
},
"conflict": {
"phpdocumentor/reflection-docblock": "<3.0",

View File

@ -352,12 +352,10 @@ class ErrorHandler
/**
* Handles errors by filtering then logging them according to the configured bit fields.
*
* @param int $type One of the E_* constants
* @param int $type One of the E_* constants
* @param string $message
* @param string $file
* @param int $line
* @param array $context
* @param array $backtrace
*
* @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
*
@ -365,7 +363,7 @@ class ErrorHandler
*
* @internal
*/
public function handleError($type, $message, $file, $line, array $context, array $backtrace = null)
public function handleError($type, $message, $file, $line)
{
// Level is the current error reporting level to manage silent error.
// Strong errors are not authorized to be silenced.
@ -377,9 +375,20 @@ class ErrorHandler
if (!$type || (!$log && !$throw)) {
return $type && $log;
}
$scope = $this->scopedErrors & $type;
if (isset($context['GLOBALS']) && ($this->scopedErrors & $type)) {
unset($context['GLOBALS']);
if (4 < $numArgs = func_num_args()) {
$context = $scope ? (func_get_arg(4) ?: array()) : array();
$backtrace = 5 < $numArgs ? func_get_arg(5) : null; // defined on HHVM
} else {
$context = array();
$backtrace = null;
}
if (isset($context['GLOBALS']) && $scope) {
$e = $context; // Whatever the signature of the method,
unset($e['GLOBALS'], $context); // $context is always a reference in 5.3
$context = $e;
}
if (null !== $backtrace && $type & E_ERROR) {
@ -399,7 +408,7 @@ class ErrorHandler
} elseif (!$throw && !($type & $level)) {
$errorAsException = new SilencedErrorContext($type, $file, $line);
} else {
if ($this->scopedErrors & $type) {
if ($scope) {
$errorAsException = new ContextErrorException($logMessage, 0, $type, $file, $line, $context);
} else {
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);

View File

@ -114,8 +114,10 @@ class AutowirePass implements CompilerPassInterface
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
}
// specifically pass the default value
$arguments[$index] = $parameter->getDefaultValue();
if (!array_key_exists($index, $arguments)) {
// specifically pass the default value
$arguments[$index] = $parameter->getDefaultValue();
}
continue;
}

View File

@ -198,6 +198,10 @@ class Definition
*/
public function replaceArgument($index, $argument)
{
if (0 === count($this->arguments)) {
throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
}
if ($index < 0 || $index > count($this->arguments) - 1) {
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
}

View File

@ -494,6 +494,22 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($container->hasDefinition('bar'));
}
public function testEmptyStringIsKept()
{
$container = new ContainerBuilder();
$container->register('a', __NAMESPACE__.'\A');
$container->register('lille', __NAMESPACE__.'\Lille');
$container->register('foo', __NAMESPACE__.'\MultipleArgumentsOptionalScalar')
->setAutowired(true)
->setArguments(array('', ''));
$pass = new AutowirePass();
$pass->process($container);
$this->assertEquals(array(new Reference('a'), '', new Reference('lille')), $container->getDefinition('foo')->getArguments());
}
}
class Foo

View File

@ -257,6 +257,7 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage The index "1" is not in the range [0, 0].
*/
public function testReplaceArgumentShouldCheckBounds()
{
@ -266,6 +267,16 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
$def->replaceArgument(1, 'bar');
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
*/
public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
{
$def = new Definition('stdClass');
$def->replaceArgument(0, 'bar');
}
public function testSetGetProperties()
{
$def = new Definition('stdClass');

View File

@ -69,10 +69,10 @@ class DateTypeTest extends TestCase
public function testSubmitFromSingleTextDateTime()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,
@ -90,10 +90,10 @@ class DateTypeTest extends TestCase
public function testSubmitFromSingleTextString()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,
@ -111,10 +111,10 @@ class DateTypeTest extends TestCase
public function testSubmitFromSingleTextTimestamp()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,
@ -134,10 +134,10 @@ class DateTypeTest extends TestCase
public function testSubmitFromSingleTextRaw()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,
@ -398,10 +398,10 @@ class DateTypeTest extends TestCase
public function testSetDataWithNegativeTimezoneOffsetStringInput()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,
@ -420,10 +420,10 @@ class DateTypeTest extends TestCase
public function testSetDataWithNegativeTimezoneOffsetDateTimeInput()
{
// we test against "de_AT", so we need the full implementation
// we test against "de_DE", so we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');
\Locale::setDefault('de_DE');
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
'format' => \IntlDateFormatter::MEDIUM,

View File

@ -41,7 +41,7 @@ class IntlTestHelper
// * the intl extension is loaded with version Intl::getIcuStubVersion()
// * the intl extension is not loaded
if (($minimumIcuVersion || defined('HHVM_VERSION_ID')) && IcuVersion::compare(Intl::getIcuVersion(), $minimumIcuVersion, '!=', 1)) {
if (($minimumIcuVersion || defined('HHVM_VERSION_ID')) && IcuVersion::compare(Intl::getIcuVersion(), $minimumIcuVersion, '<', 1)) {
$testCase->markTestSkipped('ICU version '.$minimumIcuVersion.' is required.');
}

View File

@ -267,7 +267,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private static function throwInvalidArgumentException($message, $trace, $i)
{
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file']) {
$pos = strpos($message, $delim = 'must be of the type ') ?: strpos($message, $delim = 'must be an instance of ');
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
$pos += strlen($delim);
$type = $trace[$i]['args'][0];
$type = is_object($type) ? get_class($type) : gettype($type);

View File

@ -18,6 +18,11 @@ class TypeHinted
{
private $date;
/**
* @var \Countable
*/
private $countable;
public function setDate(\DateTime $date)
{
$this->date = $date;
@ -27,4 +32,20 @@ class TypeHinted
{
return $this->date;
}
/**
* @return \Countable
*/
public function getCountable()
{
return $this->countable;
}
/**
* @param \Countable $countable
*/
public function setCountable(\Countable $countable)
{
$this->countable = $countable;
}
}

View File

@ -566,4 +566,15 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
$propertyAccessor->setValue($obj, 'publicGetSetter', 'baz');
$this->assertEquals('baz', $propertyAccessor->getValue($obj, 'publicGetSetter'));
}
/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected argument of type "Countable", "string" given
*/
public function testThrowTypeErrorWithInterface()
{
$object = new TypeHinted();
$this->propertyAccessor->setValue($object, 'countable', 'This is a string, \Countable expected.');
}
}

View File

@ -138,6 +138,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
}
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
$options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_merge($globals['schemes'], $annot->getSchemes());