8000 [Form] Fixed ValidatorTypeGuesser to guess unmapped properties not to… · symfony/symfony@3bbd12f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bbd12f

Browse files
committed
[Form] Fixed ValidatorTypeGuesser to guess unmapped properties not to be required
1 parent 9c46043 commit 3bbd12f

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CHANGELOG
2222
* added an optional PropertyAccessorInterface parameter to FormType,
2323
ObjectChoiceList and PropertyPathMapper
2424
* [BC BREAK] PropertyPathMapper and FormType now have a constructor
25+
* [BC BREAK] the validator guesser guesses fields for unmapped properties to not be required now
2526

2627
2.1.0
2728
-----

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ protected function guess($class, $property, \Closure $closure, $defaultValue = n
283283
}
284284
}
285285
}
286+
}
286287

287-
if (null !== $defaultValue) {
288-
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
289-
}
288+
if (null !== $defaultValue) {
289+
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
290290
}
291291

292292
return Guess::getBestGuess($guesses);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Validator;
13+
14+
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
15+
use Symfony\Component\Form\Guess\Guess;
16+
use Symfony\Component\Form\Guess\ValueGuess;
17+
use Symfony\Component\Validator\Constraints\Email;
18+
use Symfony\Component\Validator\Constraints\Length;
19+
use Symfony\Component\Validator\Constraints\NotBlank;
20+
use Symfony\Component\Validator\Constraints\NotNull;
21+
use Symfony\Component\Validator\Constraints\Range;
22+
use Symfony\Component\Validator\Constraints\True;
23+
use Symfony\Component\Validator\Mapping\ClassMetadata;
24+
25+
/**
26+
* @author Bernhard Schussek <bschussek@gmail.com>
27+
*/
28+
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
29+
{
30+
const TEST_CLASS = 'Symfony\Component\Form\Tests\Extension\Validator\ValidatorTypeGuesserTest_TestClass';
31+
32+
const TEST_PROPERTY = 'property';
33+
34+
/**
35+
* @var ValidatorTypeGuesser
36+
*/
37+
private $guesser;
38+
39+
/**
40+
* @var ClassMetadata
41+
*/
42+
private $metadata;
43+
44+
/**
45+
* @var \PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
private $metadataFactory;
48+
49+
protected function setUp()
50+
{
51+
$this->metadata = new ClassMetadata(self::TEST_CLASS);
52+
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
53+
$this->metadataFactory->expects($this->any())
54+
->method('getClassMetadata')
55+
->with(self::TEST_CLASS)
56+
->will($this->returnValue($this->metadata));
57+
$this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
58+
}
59+
60+
public function guessRequiredProvider()
61+
{
62+
return array(
63+
array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
64+
array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
65+
array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
66+
array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
67+
array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
68+
);
69+
}
70+
71+
/**
72+
* @dataProvider guessRequiredProvider
73+
*/
74+
public function testGuessRequired($constraint, $guess)
75+
{
76+
// add distracting constraint
77+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Email());
78+
79+
// add constraint under test
80+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);
81+
82+
$this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
83+
}
84+
85+
public function testGuessRequiredReturnsFalseForUnmappedProperties()
86+
{
87+
$this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
88+
}
89+
90+
}
91+
92+
class ValidatorTypeGuesserTest_TestClass
93+
{
94+
private $property;
95+
}

0 commit comments

Comments
 (0)
0