Merge branch '3.4' into 4.4

* 3.4:
  Fix wrong namespaces
  [Validator] Fixed calling getters before resolving groups
  [HttpKernel][LoggerDataCollector] Prevent keys collisions in the sanitized logs processing
This commit is contained in:
Fabien Potencier 2020-04-01 08:18:20 +02:00
commit f07e60b555
10 changed files with 109 additions and 8 deletions

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests;
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\CustomPathBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests;
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\TestBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;

View File

@ -179,7 +179,7 @@ class LoggerDataCollector extends DataCollector implements LateDataCollectorInte
continue;
}
$message = $log['message'];
$message = '_'.$log['message'];
$exception = $log['context']['exception'];
if ($exception instanceof SilencedErrorContext) {

View File

@ -176,13 +176,15 @@ class LoggerDataCollectorTest extends TestCase
[
['message' => 'foo3', 'context' => ['exception' => new \ErrorException('warning', 0, E_USER_WARNING)], 'priority' => 100, 'priorityName' => 'DEBUG'],
['message' => 'foo3', 'context' => ['exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)], 'priority' => 100, 'priorityName' => 'DEBUG'],
['message' => '0', 'context' => ['exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)], 'priority' => 100, 'priorityName' => 'DEBUG'],
],
[
['message' => 'foo3', 'context' => ['exception' => ['warning', E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG'],
['message' => 'foo3', 'context' => ['exception' => [E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true],
['message' => '0', 'context' => ['exception' => [E_USER_WARNING]], 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true],
],
0,
1,
2,
];
}
}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyInfo\Tests\PhpDocExtractor;
namespace Symfony\Component\PropertyInfo\Tests\Extractor;
use phpDocumentor\Reflection\Types\Collection;
use PHPUnit\Framework\TestCase;

View File

@ -21,6 +21,7 @@ use Symfony\Component\Validator\Mapping\MemberMetadata;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\LazyProperty;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
@ -192,7 +193,7 @@ class ExecutionContext implements ExecutionContextInterface
$parameters,
$this->root,
$this->propertyPath,
$this->value,
$this->getValue(),
null,
null,
$this->constraint
@ -211,7 +212,7 @@ class ExecutionContext implements ExecutionContextInterface
$parameters,
$this->root,
$this->propertyPath,
$this->value,
$this->getValue(),
$this->translator,
$this->translationDomain
);
@ -246,6 +247,10 @@ class ExecutionContext implements ExecutionContextInterface
*/
public function getValue()
{
if ($this->value instanceof LazyProperty) {
return $this->value->getPropertyValue();
}
return $this->value;
}

View File

@ -0,0 +1,27 @@
<?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\Validator\Tests\Fixtures;
class EntityWithGroupedConstraintOnMethods
{
public $bar;
public function isValidInFoo()
{
return false;
}
public function getBar()
{
throw new \Exception('Should not be called');
}
}

View File

@ -14,14 +14,19 @@ namespace Symfony\Component\Validator\Tests\Validator;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
use Symfony\Component\Validator\Validator\RecursiveValidator;
use Symfony\Component\Validator\Validator\ValidatorInterface;
@ -118,6 +123,25 @@ class RecursiveValidatorTest extends AbstractTest
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
}
public function testGroupedMethodConstraintValidateInSequence()
{
$metadata = new ClassMetadata(EntityWithGroupedConstraintOnMethods::class);
$metadata->addPropertyConstraint('bar', new NotNull(['groups' => 'Foo']));
$metadata->addGetterMethodConstraint('validInFoo', 'isValidInFoo', new IsTrue(['groups' => 'Foo']));
$metadata->addGetterMethodConstraint('bar', 'getBar', new NotNull(['groups' => 'Bar']));
$this->metadataFactory->addMetadata($metadata);
$entity = new EntityWithGroupedConstraintOnMethods();
$groups = new GroupSequence(['EntityWithGroupedConstraintOnMethods', 'Foo', 'Bar']);
$violations = $this->validator->validate($entity, null, $groups);
$this->assertCount(2, $violations);
$this->assertInstanceOf(NotNull::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(IsTrue::class, $violations->get(1)->getConstraint());
}
public function testAllConstraintValidateAllGroupsForNestedConstraints()
{
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [

View File

@ -0,0 +1,32 @@
<?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\Validator\Validator;
/**
* A wrapper for a callable initializing a property from a getter.
*
* @internal
*/
class LazyProperty
{
private $propertyValueCallback;
public function __construct(\Closure $propertyValueCallback)
{
$this->propertyValueCallback = $propertyValueCallback;
}
public function getPropertyValue()
{
return \call_user_func($this->propertyValueCallback);
}
}

View File

@ -28,6 +28,7 @@ use Symfony\Component\Validator\Mapping\CascadingStrategy;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\GenericMetadata;
use Symfony\Component\Validator\Mapping\GetterMetadata;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Mapping\TraversalStrategy;
@ -506,7 +507,13 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
throw new UnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".', \is_object($propertyMetadata) ? \get_class($propertyMetadata) : \gettype($propertyMetadata)));
}
$propertyValue = $propertyMetadata->getPropertyValue($object);
if ($propertyMetadata instanceof GetterMetadata) {
$propertyValue = new LazyProperty(static function () use ($propertyMetadata, $object) {
return $propertyMetadata->getPropertyValue($object);
});
} else {
$propertyValue = $propertyMetadata->getPropertyValue($object);
}
$this->validateGenericNode(
$propertyValue,
@ -739,6 +746,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
$validator = $this->validatorFactory->getInstance($constraint);
$validator->initialize($context);
if ($value instanceof LazyProperty) {
$value = $value->getPropertyValue();
}
try {
$validator->validate($value, $constraint);
} catch (UnexpectedValueException $e) {