13
13
14
14
use Symfony \Component \Form \Extension \Validator \ValidatorTypeGuesser ;
15
15
use Symfony \Component \Form \Guess \Guess ;
16
+ use Symfony \Component \Form \Guess \ValueGuess ;
17
+ use Symfony \Component \Validator \Constraints \Email ;
16
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 ;
17
23
use Symfony \Component \Validator \Constraints \Type ;
24
+ use Symfony \Component \Validator \Mapping \ClassMetadata ;
18
25
19
26
/**
20
- * @author franek <franek@chicour.net>
21
- */
27
+ * @author franek <franek@chicour.net>
28
+ * @author Bernhard Schussek <bschussek@gmail.com>
29
+ */
22
30
class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
23
31
{
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 ;
25
50
26
- public function setUp ()
51
+ protected function setUp ()
27
52
{
28
53
if (!class_exists ('Symfony\Component\Validator\Constraint ' )) {
29
54
$ this ->markTestSkipped ('The "Validator" component is not available ' );
30
55
}
31
56
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 );
33
87
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 ));
35
94
}
36
95
37
96
public function testGuessMaxLengthForConstraintWithMaxValue ()
38
97
{
39
98
$ constraint = new Length (array ('max ' => '2 ' ));
40
99
41
- $ result = $ this ->typeGuesser ->guessMaxLengthForConstraint ($ constraint );
100
+ $ result = $ this ->guesser ->guessMaxLengthForConstraint ($ constraint );
42
101
$ this ->assertInstanceOf ('Symfony\Component\Form\Guess\ValueGuess ' , $ result );
43
102
$ this ->assertEquals (2 , $ result ->getValue ());
44
103
$ this ->assertEquals (Guess::HIGH_CONFIDENCE , $ result ->getConfidence ());
@@ -48,30 +107,35 @@ public function testGuessMaxLengthForConstraintWithMinValue()
48
107
{
49
108
$ constraint = new Length (array ('min ' => '2 ' ));
50
109
51
- $ result = $ this ->typeGuesser ->guessMaxLengthForConstraint ($ constraint );
110
+ $ result = $ this ->guesser ->guessMaxLengthForConstraint ($ constraint );
52
111
$ this ->assertNull ($ result );
53
112
}
54
113
114
+ public function maxLengthTypeProvider ()
115
+ {
116
+ return array (
117
+ array ('double ' ),
118
+ array ('float ' ),
119
+ array ('numeric ' ),
120
+ array ('real ' ),
121
+ );
122
+ }
123
+
55
124
/**
56
- * @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
57
- */
125
+ * @dataProvider maxLengthTypeProvider
126
+ */
58
127
public function testGuessMaxLengthForConstraintWithType ($ type )
59
128
{
60
129
$ constraint = new Type ($ type );
61
130
62
- $ result = $ this ->typeGuesser ->guessMaxLengthForConstraint ($ constraint );
131
+ $ result = $ this ->guesser ->guessMaxLengthForConstraint ($ constraint );
63
132
$ this ->assertInstanceOf ('Symfony\Component\Form\Guess\ValueGuess ' , $ result );
64
- $ this ->assertEquals ( null , $ result ->getValue ());
133
+ $ this ->assertNull ( $ result ->getValue ());
65
134
$ this ->assertEquals (Guess::MEDIUM_CONFIDENCE , $ result ->getConfidence ());
66
135
}
136
+ }
67
137
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 ;
77
141
}
0 commit comments