9
9
10
10
namespace ZendTest \InputFilter ;
11
11
12
- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13
- use Zend \Filter ;
14
12
use Zend \InputFilter \ArrayInput ;
15
13
use Zend \InputFilter \Exception \InvalidArgumentException ;
16
- use Zend \Validator ;
17
14
18
15
/**
19
16
* @covers Zend\InputFilter\ArrayInput
@@ -30,12 +27,7 @@ public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue($value =
30
27
parent ::testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue ([$ value ]);
31
28
}
32
29
33
- public function testValueIsNullByDefault ()
34
- {
35
- $ this ->markTestSkipped ('Test is not enabled in ArrayInputTest ' );
36
- }
37
-
38
- public function testValueIsEmptyArrayByDefault ()
30
+ public function testDefaultGetValue ()
39
31
{
40
32
$ this ->assertCount (0 , $ this ->input ->getValue ());
41
33
}
@@ -49,116 +41,6 @@ public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
49
41
$ this ->input ->setValue ('bar ' );
50
42
}
51
43
52
- public function testValueMayBeInjected ()
53
- {
54
- $ this ->input ->setValue (['bar ' ]);
55
- $ this ->assertEquals (['bar ' ], $ this ->input ->getValue ());
56
- }
57
-
58
- public function testRetrievingValueFiltersTheValue ()
59
- {
60
- $ this ->input ->setValue (['bar ' ]);
61
- $ filter = new Filter \StringToUpper ();
62
- $ this ->input ->getFilterChain ()->attach ($ filter );
63
- $ this ->assertEquals (['BAR ' ], $ this ->input ->getValue ());
64
- }
65
-
66
- public function testCanRetrieveRawValue ()
67
- {
68
- $ this ->input ->setValue (['bar ' ]);
69
- $ filter = new Filter \StringToUpper ();
70
- $ this ->input ->getFilterChain ()->attach ($ filter );
71
- $ this ->assertEquals (['bar ' ], $ this ->input ->getRawValue ());
72
- }
73
-
74
- public function testValidationOperatesOnFilteredValue ()
75
- {
76
- $ this ->input ->setValue ([' 123 ' , ' 123 ' ]);
77
- $ filter = new Filter \StringTrim ();
78
- $ this ->input ->getFilterChain ()->attach ($ filter );
79
- $ validator = new Validator \Digits ();
80
- $ this ->input ->getValidatorChain ()->attach ($ validator );
81
- $ this ->assertTrue (
82
- $ this ->input ->isValid (),
83
- 'isValid() value not match. Detail . ' . json_encode ($ this ->input ->getMessages ())
84
- );
85
- }
86
-
87
- public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation ()
88
- {
89
- $ this ->input ->setValue (['bar ' ]);
90
- $ validator = new Validator \Digits ();
91
- $ this ->input ->getValidatorChain ()->attach ($ validator );
92
- $ this ->input ->setErrorMessage ('Please enter only digits ' );
93
- $ this ->assertFalse ($ this ->input ->isValid ());
94
- $ messages = $ this ->input ->getMessages ();
95
- $ this ->assertArrayNotHasKey (Validator \Digits::NOT_DIGITS , $ messages );
96
- $ this ->assertContains ('Please enter only digits ' , $ messages );
97
- }
98
-
99
- public function testNotEmptyValidatorAddedWhenIsValidIsCalled ()
100
- {
101
- $ this ->assertTrue ($ this ->input ->isRequired ());
102
- $ this ->input ->setValue (['bar ' , '' ]);
103
- $ validatorChain = $ this ->input ->getValidatorChain ();
104
- $ this ->assertEquals (0 , count ($ validatorChain ->getValidators ()));
105
-
106
- $ this ->assertFalse ($ this ->input ->isValid ());
107
- $ messages = $ this ->input ->getMessages ();
108
- $ this ->assertArrayHasKey ('isEmpty ' , $ messages );
109
- $ this ->assertEquals (1 , count ($ validatorChain ->getValidators ()));
110
-
111
- // Assert that NotEmpty validator wasn't added again
112
- $ this ->assertFalse ($ this ->input ->isValid ());
113
- $ this ->assertEquals (1 , count ($ validatorChain ->getValidators ()));
114
- }
115
-
116
- public function testRequiredNotEmptyValidatorNotAddedWhenOneExists ()
117
- {
118
- $ this ->assertTrue ($ this ->input ->isRequired ());
119
- $ this ->input ->setValue (['bar ' , '' ]);
120
-
121
- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
122
- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
123
- $ notEmptyMock ->expects ($ this ->exactly (1 ))
124
- ->method ('isValid ' )
125
- ->will ($ this ->returnValue (false ));
126
-
127
- $ validatorChain = $ this ->input ->getValidatorChain ();
128
- $ validatorChain ->prependValidator ($ notEmptyMock );
129
- $ this ->assertFalse ($ this ->input ->isValid ());
130
-
131
- $ validators = $ validatorChain ->getValidators ();
132
- $ this ->assertEquals (1 , count ($ validators ));
133
- $ this ->assertEquals ($ notEmptyMock , $ validators [0 ]['instance ' ]);
134
- }
135
-
136
- public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain ()
137
- {
138
- $ this ->assertTrue ($ this ->input ->isRequired ());
139
- $ this ->input ->setValue (['bar ' , '' ]);
140
-
141
- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
142
- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
143
- $ notEmptyMock ->expects ($ this ->exactly (1 ))
144
- ->method ('isValid ' )
145
- ->will ($ this ->returnValue (false ));
146
-
147
- $ validatorChain = $ this ->input ->getValidatorChain ();
148
- $ validatorChain ->attach (new Validator \Digits ());
149
- $ validatorChain ->attach ($ notEmptyMock );
150
- $ this ->assertFalse ($ this ->input ->isValid ());
151
-
152
- $ validators = $ validatorChain ->getValidators ();
153
- $ this ->assertEquals (2 , count ($ validators ));
154
- $ this ->assertEquals ($ notEmptyMock , $ validators [1 ]['instance ' ]);
155
- }
156
-
157
- public function testMerge ($ sourceRawValue = 'bazRawValue ' )
158
- {
159
- parent ::testMerge ([$ sourceRawValue ]);
160
- }
161
-
162
44
public function fallbackValueVsIsValidProvider ()
163
45
{
164
46
$ dataSets = parent ::fallbackValueVsIsValidProvider ();
@@ -190,4 +72,33 @@ public function mixedValueProvider()
190
72
191
73
return $ dataSets ;
192
74
}
75
+
76
+ protected function createFilterChainMock ($ valueRaw = null , $ valueFiltered = null )
77
+ {
78
+ // ArrayInput filters per each array value
79
+ if (is_array ($ valueRaw )) {
80
+ $ valueRaw = current ($ valueRaw );
81
+ }
82
+
83
+ if (is_array ($ valueFiltered )) {
84
+ $ valueFiltered = current ($ valueFiltered );
85
+ }
86
+
87
+ return parent ::createFilterChainMock ($ valueRaw , $ valueFiltered );
88
+ }
89
+
90
+ protected function createValidatorChainMock ($ isValid = null , $ value = null , $ context = null , $ messages = [])
91
+ {
92
+ // ArrayInput validates per each array value
93
+ if (is_array ($ value )) {
94
+ $ value = current ($ value );
95
+ }
96
+
97
+ return parent ::createValidatorChainMock ($ isValid , $ value , $ context , $ messages );
98
+ }
99
+
100
+ protected function getDummyValue ($ raw = true )
101
+ {
102
+ return [parent ::getDummyValue ($ raw )];
103
+ }
193
104
}
0 commit comments