13
13
14
14
use Symfony \Component \Validator \Constraints \Positive ;
15
15
use Symfony \Component \Validator \Constraints \PositiveValidator ;
16
- use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
17
16
18
17
/**
19
18
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
20
19
*/
21
- class PositiveValidatorTest extends ConstraintValidatorTestCase
20
+ class PositiveValidatorTest extends AbstractComparisonValidatorTestCase
22
21
{
23
22
protected function createValidator ()
24
23
{
25
24
return new PositiveValidator ();
26
25
}
27
26
28
- /**
29
- * @dataProvider getValidValues
30
- */
31
- public function testValidValues ($ value )
27
+ protected function createConstraint (array $ options = null )
32
28
{
33
- $ this ->validator ->validate ($ value , new Positive ());
34
-
35
- $ this ->assertNoViolation ();
29
+ return new Positive ($ options );
36
30
}
37
31
38
- public function getValidValues ()
32
+ protected function getErrorCode ()
39
33
{
40
- return array (
41
- array (1 ),
42
- array (1.1 ),
43
- array ('1.1 ' ),
44
- );
34
+ return Positive::IS_NEGATIVE_ERROR ;
45
35
}
46
36
47
37
/**
48
- * @dataProvider getInvalidValues
38
+ * {@inheritdoc}
49
39
*/
50
- public function testInvalidValues ($ value )
51
- {
52
- $ constraint = new Positive (array (
53
- 'message ' => 'myMessage ' ,
54
- ));
55
-
56
- $ this ->validator ->validate ($ value , $ constraint );
57
-
58
- $ this ->buildViolation ('myMessage ' )
59
- ->setParameter ('{{ value }} ' , $ value )
60
- ->setCode (Positive::IS_NEGATIVE_ERROR )
61
- ->assertRaised ();
62
- }
63
-
64
- public function getInvalidValues ()
40
+ public function provideValidComparisons ()
65
41
{
66
42
return array (
67
- array (- 1 ),
68
- array (- 1.1 ),
69
- array ('- 1.1 ' ),
43
+ array (1 , 0 ),
44
+ array (1.1 , 0 ),
45
+ array ('1.1 ' , 0 ),
70
46
);
71
47
}
72
48
73
49
/**
74
- * @dataProvider getZeroValues
50
+ * {@inheritdoc}
75
51
*/
76
- public function testZeroIsInvalid ($ value )
77
- {
78
- $ constraint = new Positive (array (
79
- 'message ' => 'myMessage ' ,
80
- ));
81
-
82
- $ this ->validator ->validate ($ value , $ constraint );
83
-
84
- $ this ->buildViolation ('myMessage ' )
85
- ->setParameter ('{{ value }} ' , $ value )
86
- ->setCode (Positive::IS_ZERO_ERROR )
87
- ->assertRaised ();
88
- }
89
-
90
- public function getZeroValues ()
52
+ public function provideValidComparisonsToPropertyPath ()
91
53
{
92
54
return array (
93
- array (0 ),
94
- array (0.0 ),
95
- array (.0 ),
55
+ array (4 ),
96
56
);
97
57
}
98
58
99
59
/**
100
- * @dataProvider getWrongTypeValues
101
- * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
60
+ * {@inheritdoc}
102
61
*/
103
- public function testItShouldThrowExceptionForWrongType ( $ value )
62
+ public function provideInvalidComparisons ( )
104
63
{
105
- $ this ->validator ->validate ($ value , new Positive ());
64
+ return array (
65
+ array (-1 , '-1 ' , 0 , '0 ' , 'integer ' ),
66
+ array (-1.1 , '-1.1 ' , 0 , '0 ' , 'integer ' ),
67
+ array ('-1.1 ' , '"-1.1" ' , '0 ' , '"0" ' , 'string ' ),
68
+ );
106
69
}
107
70
108
71
public function getWrongTypeValues ()
@@ -115,9 +78,18 @@ public function getWrongTypeValues()
115
78
);
116
79
}
117
80
81
+ /**
82
+ * @dataProvider getWrongTypeValues
83
+ * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
84
+ */
85
+ public function testItShouldThrowExceptionForWrongType ($ value )
86
+ {
87
+ $ this ->validator ->validate ($ value , new Positive (array ('value ' => 0 )));
88
+ }
89
+
118
90
public function testItShouldDoNothingForNullValue ()
119
91
{
120
- $ this ->validator ->validate (null , new Positive ());
92
+ $ this ->validator ->validate (null , new Positive (array ( ' value ' => 0 ) ));
121
93
122
94
$ this ->assertNoViolation ();
123
95
}
0 commit comments