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
@@ -25,17 +22,7 @@ public function setUp()
25
22
$ this ->input = new ArrayInput ('foo ' );
26
23
}
27
24
28
- public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue ($ value = '' )
29
- {
30
- parent ::testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue ([$ value ]);
31
- }
32
-
33
- public function testValueIsNullByDefault ()
34
- {
35
- $ this ->markTestSkipped ('Test is not enabled in ArrayInputTest ' );
36
- }
37
-
38
- public function testValueIsEmptyArrayByDefault ()
25
+ public function testDefaultGetValue ()
39
26
{
40
27
$ this ->assertCount (0 , $ this ->input ->getValue ());
41
28
}
@@ -49,116 +36,6 @@ public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
49
36
$ this ->input ->setValue ('bar ' );
50
37
}
51
38
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
39
public function fallbackValueVsIsValidProvider ()
163
40
{
164
41
$ dataSets = parent ::fallbackValueVsIsValidProvider ();
@@ -190,4 +67,43 @@ public function mixedValueProvider()
190
67
191
68
return $ dataSets ;
192
69
}
70
+
71
+ protected function createFilterChainMock ($ valueRaw = null , $ valueFiltered = null )
72
+ {
73
+ // ArrayInput filters per each array value
74
+ if (is_array ($ valueRaw )) {
75
+ $ valueRaw = current ($ valueRaw );
76
+ }
77
+
78
+ if (is_array ($ valueFiltered )) {
79
+ $ valueFiltered = current ($ valueFiltered );
80
+ }
81
+
82
+ return parent ::createFilterChainMock ($ valueRaw , $ valueFiltered );
83
+ }
84
+
85
+ protected function createValidatorChainMock ($ isValid = null , $ value = null , $ context = null , $ messages = [])
86
+ {
87
+ // ArrayInput validates per each array value
88
+ if (is_array ($ value )) {
89
+ $ value = current ($ value );
90
+ }
91
+
92
+ return parent ::createValidatorChainMock ($ isValid , $ value , $ context , $ messages );
93
+ }
94
+
95
+ protected function createNonEmptyValidatorMock ($ isValid , $ value , $ context = null )
96
+ {
97
+ // ArrayInput validates per each array value
98
+ if (is_array ($ value )) {
99
+ $ value = current ($ value );
100
+ }
101
+
102
+ return parent ::createNonEmptyValidatorMock ($ isValid , $ value , $ context );
103
+ }
104
+
105
+ protected function getDummyValue ($ raw = true )
106
+ {
107
+ return [parent ::getDummyValue ($ raw )];
108
+ }
193
109
}
0 commit comments