Merge branch '2.2' into 2.3

* 2.2:
  [Validator] fixed ConstraintViolation:: incorrect when nested
  handle Optional and Required constraints from XML or YAML sources correctly
  added missing comments to WebTestCase
  Fixed #8455: PhpExecutableFinder::find() does not always return the correct binary
  [DependencyInjection] Fix Container::camelize to convert beginning and ending chars
  [Validator] Fixed groups argument misplace for validateValue method from validator class

Conflicts:
	src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php
	src/Symfony/Component/Validator/Tests/GraphWalkerTest.php
This commit is contained in:
Fabien Potencier 2013-07-21 22:18:49 +02:00
commit b45f18b81d
9 changed files with 100 additions and 6 deletions

View File

@ -13,7 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Test;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* WebTestCase is the base class for functional tests.
@ -23,6 +23,10 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
{
protected static $class;
/**
* @var KernelInterface
*/
protected static $kernel;
/**
@ -147,7 +151,7 @@ abstract class WebTestCase extends \PHPUnit_Framework_TestCase
*
* @param array $options An array of options
*
* @return HttpKernelInterface A HttpKernelInterface instance
* @return KernelInterface A KernelInterface instance
*/
protected static function createKernel(array $options = array())
{

View File

@ -513,7 +513,7 @@ class Container implements IntrospectableContainerInterface
*/
public static function camelize($id)
{
return preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); }, $id);
return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ '))), array(' ' => ''));
}
/**

View File

@ -31,6 +31,29 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
}
/**
* @dataProvider dataForTestCamelize
*/
public function testCamelize($id, $expected)
{
$this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
}
public function dataForTestCamelize()
{
return array(
array('foo_bar', 'FooBar'),
array('foo.bar', 'Foo_Bar'),
array('foo.bar_baz', 'Foo_BarBaz'),
array('foo._bar', 'Foo_Bar'),
array('foo_.bar', 'Foo_Bar'),
array('_foo', 'Foo'),
array('.foo', '_Foo'),
array('foo_', 'Foo'),
array('foo.', 'Foo_'),
);
}
/**
* @covers Symfony\Component\DependencyInjection\Container::compile
*/

View File

@ -34,7 +34,7 @@ class PhpExecutableFinder
public function find()
{
// PHP_BINARY return the current sapi executable
if (defined('PHP_BINARY') && PHP_BINARY && ('cli' === PHP_SAPI)) {
if (defined('PHP_BINARY') && PHP_BINARY && ('cli' === PHP_SAPI) && is_file(PHP_BINARY)) {
return PHP_BINARY;
}

View File

@ -32,7 +32,7 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
$className = $constraint->validatedBy();
if (!isset($this->validators[$className])) {
if (!isset($this->validators[$className]) || $className === 'Symfony\Component\Validator\Constraints\CollectionValidator') {
$this->validators[$className] = new $className();
}

View File

@ -49,6 +49,12 @@ class Collection extends Constraint
}
foreach ($this->fields as $fieldName => $field) {
// the XmlFileLoader and YamlFileLoader pass the field Optional
// and Required constraint as an array with exactly one element
if (is_array($field) && count($field) == 1) {
$this->fields[$fieldName] = $field = $field[0];
}
if (!$field instanceof Optional && !$field instanceof Required) {
$this->fields[$fieldName] = $field = new Required($field);
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Valid;
/**
@ -70,4 +71,42 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
'foo' => new Required(new Valid()),
));
}
public function testAcceptOptionalConstraintAsOneElementArray()
{
$collection1 = new Collection(array(
"fields" => array(
"alternate_email" => array(
new Optional(new Email()),
),
),
));
$collection2 = new Collection(array(
"fields" => array(
"alternate_email" => new Optional(new Email()),
),
));
$this->assertEquals($collection1, $collection2);
}
public function testAcceptRequiredConstraintAsOneElementArray()
{
$collection1 = new Collection(array(
"fields" => array(
"alternate_email" => array(
new Required(new Email()),
),
),
));
$collection2 = new Collection(array(
"fields" => array(
"alternate_email" => new Required(new Email()),
),
));
$this->assertEquals($collection1, $collection2);
}
}

View File

@ -14,6 +14,10 @@ namespace Symfony\Component\Validator\Tests;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\ValidationVisitor;
use Symfony\Component\Validator\ConstraintValidatorFactory;
class ExecutionContextTest extends \PHPUnit_Framework_TestCase
{
@ -272,6 +276,24 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('bam.baz', $this->context->getPropertyPath('bam.baz'));
}
public function testGetPropertyPathWithNestedCollectionsMixed()
{
$constraints = new Collection(array(
'foo' => new Collection(array(
'foo' => new ConstraintA(),
'bar' => new ConstraintA(),
)),
'name' => new ConstraintA()
));
$visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), $this->translator);
$context = new ExecutionContext($visitor, $this->translator, self::TRANS_DOMAIN);
$context->validateValue(array('foo' => array('foo' => 'VALID')), $constraints);
$violations = $context->getViolations();
$this->assertEquals('[name]', $violations[1]->getPropertyPath());
}
}
class ExecutionContextTest_TestClass

View File

@ -186,7 +186,7 @@ class Validator implements ValidatorInterface
);
}
$context->validateValue($value, $constraint, $groups);
$context->validateValue($value, $constraint, '', $groups);
}
return $context->getViolations();