8000 [Form] Fixed ValidatorTypeGuesser to guess properties without constra… · symfony/symfony@2571828 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2571828

Browse files
committed
[Form] Fixed ValidatorTypeGuesser to guess properties without constraints not to be required
1 parent c3feed6 commit 2571828

File tree

3 files changed

+94
-25
lines changed

3 files changed

+94
-25
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
CHANGELOG
22
=========
33

4+
2.3.20
5+
------
6+
7+
* the validator guesser guesses fields for properties without any constraints
8+
to not be required now
49

510
2.3.0
6-
------
11+
-----
712

813
* deprecated FormPerformanceTestCase and FormIntegrationTestCase in the Symfony\Component\Form\Tests namespace and moved them to the Symfony\Component\Form\Test namespace
914
* deprecated TypeTestCase in the Symfony\Component\Form\Tests\Extension\Core\Type namespace and moved it to the Symfony\Component\Form\Test namespace

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ protected function guess($class, $property, \Closure $closure, $defaultValue = n
276276
}
277277
}
278278
}
279+
}
279280

280-
if (null !== $defaultValue) {
281-
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
282-
}
281+
if (null !== $defaultValue) {
282+
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
283283
}
284284

285285
return Guess::getBestGuess($guesses);

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,91 @@
1313

1414
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
1515
use Symfony\Component\Form\Guess\Guess;
16+
use Symfony\Component\Form\Guess\ValueGuess;
17+
use Symfony\Component\Validator\Constraints\Email;
1618
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;
1723
use Symfony\Component\Validator\Constraints\Type;
24+
use Symfony\Component\Validator\Mapping\ClassMetadata;
1825

1926
/**
20-
* @author franek <franek@chicour.net>
21-
*/
27+
* @author franek <franek@chicour.net>
28+
* @author Bernhard Schussek <bschussek@gmail.com>
29+
*/
2230
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
2331
{
24-
private $typeGuesser;
32+
const TEST_CLASS = 'Symfony\Component\Form\Tests\Extension\Validator\ValidatorTypeGuesserTest_TestClass';
33+
34+
const TEST_PROPERTY = 'property';
35+
36+
/**
37+
* @var ValidatorTypeGuesser
38+
*/
39+
private $guesser;
40+
41+
/**
42+
* @var ClassMetadata
43+
*/
44+
private $metadata;
45+
46+
/**
47+
* @var \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $metadataFactory;
2550

26-
public function setUp()
51+
protected function setUp()
2752
{
2853
if (!class_exists('Symfony\Component\Validator\Constraint')) {
2954
$this->markTestSkipped('The "Validator" component is not available');
3055
}
3156

32-
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
57+
$this->metadata = new ClassMetadata(self::TEST_CLASS);
58+
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
59+
$this->metadataFactory->expects($this->any())
60+
->method('getMetadataFor')
61+
->with(self::TEST_CLASS)
62+
->will($this->returnValue($this->metadata));
63+
$this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
64+
}
65+
66+
public function guessRequiredProvider()
67+
{
68+
return array(
69+
array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
70+
array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
71+
array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
72+
array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
73+
array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
74+
);
75+
}
76+
77+
/**
78+
* @dataProvider guessRequiredProvider
79+
*/
80+
public function testGuessRequired($constraint, $guess)
81+
{
82+
// add distracting constraint
83+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Email());
84+
85+
// add constraint under test
86+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);
3387

34-
$this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
88+
$this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
89+
}
90+
91+
public function testGuessRequiredReturnsFalseForUnmappedProperties()
92+
{
93+
$this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
3594
}
3695

3796
public function testGuessMaxLengthForConstraintWithMaxValue()
3897
{
3998
$constraint = new Length(array('max' => '2'));
4099

41-
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
100+
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
42101
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
43102
$this->assertEquals(2, $result->getValue());
44103
$this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
@@ -48,30 +107,35 @@ public function testGuessMaxLengthForConstraintWithMinValue()
48107
{
49108
$constraint = new Length(array('min' => '2'));
50109

51-
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
110+
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
52111
$this->assertNull($result);
53112
}
54113

114+
public function maxLengthTypeProvider()
115+
{
116+
return array (
117+
array('double'),
118+
array('float'),
119+
array('numeric'),
120+
array('real'),
121+
);
122+
}
123+
55124
/**
56-
* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
57-
*/
125+
* @dataProvider maxLengthTypeProvider
126+
*/
58127
public function testGuessMaxLengthForConstraintWithType($type)
59128
{
60129
$constraint = new Type($type);
61130

62-
$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
131+
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
63132
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
64-
$this->assertEquals(null, $result->getValue());
133+
$this->assertNull($result->getValue());
65134
$this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
66135
}
136+
}
67137

68-
public static function dataProviderTestGuessMaxLengthForConstraintWithType()
69-
{
70-
return array (
71-
array('double'),
72-
array('float'),
73-
array('numeric'),
74-
array('real'),
75-
);
76-
}
138+
class ValidatorTypeGuesserTest_TestClass
139+
{
140+
private $property;
77141
}

0 commit comments

Comments
 (0)
0