99
1010namespace ZendTest \InputFilter ;
1111
12- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13- use Zend \Filter ;
1412use Zend \InputFilter \ArrayInput ;
1513use Zend \InputFilter \Exception \InvalidArgumentException ;
16- use Zend \Validator ;
1714
1815/**
1916 * @covers Zend\InputFilter\ArrayInput
@@ -25,12 +22,7 @@ public function setUp()
2522 $ this ->input = new ArrayInput ('foo ' );
2623 }
2724
28- public function testValueIsNullByDefault ()
29- {
30- $ this ->markTestSkipped ('Test is not enabled in ArrayInputTest ' );
31- }
32-
33- public function testValueIsEmptyArrayByDefault ()
25+ public function testDefaultGetValue ()
3426 {
3527 $ this ->assertCount (0 , $ this ->input ->getValue ());
3628 }
@@ -44,137 +36,6 @@ public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
4436 $ this ->input ->setValue ('bar ' );
4537 }
4638
47- public function testValueMayBeInjected ()
48- {
49- $ this ->input ->setValue (['bar ' ]);
50- $ this ->assertEquals (['bar ' ], $ this ->input ->getValue ());
51- }
52-
53- public function testRetrievingValueFiltersTheValue ()
54- {
55- $ this ->input ->setValue (['bar ' ]);
56- $ filter = new Filter \StringToUpper ();
57- $ this ->input ->getFilterChain ()->attach ($ filter );
58- $ this ->assertEquals (['BAR ' ], $ this ->input ->getValue ());
59- }
60-
61- public function testCanRetrieveRawValue ()
62- {
63- $ this ->input ->setValue (['bar ' ]);
64- $ filter = new Filter \StringToUpper ();
65- $ this ->input ->getFilterChain ()->attach ($ filter );
66- $ this ->assertEquals (['bar ' ], $ this ->input ->getRawValue ());
67- }
68-
69- public function testValidationOperatesOnFilteredValue ()
70- {
71- $ this ->input ->setValue ([' 123 ' , ' 123 ' ]);
72- $ filter = new Filter \StringTrim ();
73- $ this ->input ->getFilterChain ()->attach ($ filter );
74- $ validator = new Validator \Digits ();
75- $ this ->input ->getValidatorChain ()->attach ($ validator );
76- $ this ->assertTrue (
77- $ this ->input ->isValid (),
78- 'isValid() value not match. Detail . ' . json_encode ($ this ->input ->getMessages ())
79- );
80- }
81-
82- public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation ()
83- {
84- $ this ->input ->setValue (['bar ' ]);
85- $ validator = new Validator \Digits ();
86- $ this ->input ->getValidatorChain ()->attach ($ validator );
87- $ this ->input ->setErrorMessage ('Please enter only digits ' );
88- $ this ->assertFalse ($ this ->input ->isValid ());
89- $ messages = $ this ->input ->getMessages ();
90- $ this ->assertArrayNotHasKey (Validator \Digits::NOT_DIGITS , $ messages );
91- $ this ->assertContains ('Please enter only digits ' , $ messages );
92- }
93-
94- public function testNotEmptyValidatorAddedWhenIsValidIsCalled ()
95- {
96- $ this ->assertTrue ($ this ->input ->isRequired ());
97- $ this ->input ->setValue (['bar ' , '' ]);
98- $ validatorChain = $ this ->input ->getValidatorChain ();
99- $ this ->assertEquals (0 , count ($ validatorChain ->getValidators ()));
100-
101- $ this ->assertFalse ($ this ->input ->isValid ());
102- $ messages = $ this ->input ->getMessages ();
103- $ this ->assertArrayHasKey ('isEmpty ' , $ messages );
104- $ this ->assertEquals (1 , count ($ validatorChain ->getValidators ()));
105-
106- // Assert that NotEmpty validator wasn't added again
107- $ this ->assertFalse ($ this ->input ->isValid ());
108- $ this ->assertEquals (1 , count ($ validatorChain ->getValidators ()));
109- }
110-
111- public function testRequiredNotEmptyValidatorNotAddedWhenOneExists ()
112- {
113- $ this ->assertTrue ($ this ->input ->isRequired ());
114- $ this ->input ->setValue (['bar ' , '' ]);
115-
116- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
117- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
118- $ notEmptyMock ->expects ($ this ->exactly (1 ))
119- ->method ('isValid ' )
120- ->will ($ this ->returnValue (false ));
121-
122- $ validatorChain = $ this ->input ->getValidatorChain ();
123- $ validatorChain ->prependValidator ($ notEmptyMock );
124- $ this ->assertFalse ($ this ->input ->isValid ());
125-
126- $ validators = $ validatorChain ->getValidators ();
127- $ this ->assertEquals (1 , count ($ validators ));
128- $ this ->assertEquals ($ notEmptyMock , $ validators [0 ]['instance ' ]);
129- }
130-
131- public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain ()
132- {
133- $ this ->assertTrue ($ this ->input ->isRequired ());
134- $ this ->input ->setValue (['bar ' , '' ]);
135-
136- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
137- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
138- $ notEmptyMock ->expects ($ this ->exactly (1 ))
139- ->method ('isValid ' )
140- ->will ($ this ->returnValue (false ));
141-
142- $ validatorChain = $ this ->input ->getValidatorChain ();
143- $ validatorChain ->attach (new Validator \Digits ());
144- $ validatorChain ->attach ($ notEmptyMock );
145- $ this ->assertFalse ($ this ->input ->isValid ());
146-
147- $ validators = $ validatorChain ->getValidators ();
148- $ this ->assertEquals (2 , count ($ validators ));
149- $ this ->assertEquals ($ notEmptyMock , $ validators [1 ]['instance ' ]);
150- }
151-
152- public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid ()
153- {
154- $ this ->input ->setValue (['nonempty ' ])
155- ->getFilterChain ()->attach (new Filter \Callback (function () {
156- return '' ;
157- }));
158- $ this ->assertFalse ($ this ->input ->isValid ());
159- }
160-
161- public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid ()
162- {
163- $ this ->input ->setValue (['' ])
164- ->getFilterChain ()->attach (new Filter \Callback (function () {
165- return 'nonempty ' ;
166- }));
167- $ this ->assertTrue (
168- $ this ->input ->isValid (),
169- 'isValid() value not match. Detail . ' . json_encode ($ this ->input ->getMessages ())
170- );
171- }
172-
173- public function testMerge ($ sourceRawValue = 'bazRawValue ' )
174- {
175- parent ::testMerge ([$ sourceRawValue ]);
176- }
177-
17839 public function fallbackValueVsIsValidProvider ()
17940 {
18041 $ dataSets = parent ::fallbackValueVsIsValidProvider ();
@@ -206,4 +67,43 @@ public function mixedValueProvider()
20667
20768 return $ dataSets ;
20869 }
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+ }
209109}
0 commit comments