15
15
use Zend \Filter \FilterChain ;
16
16
use Zend \InputFilter \Input ;
17
17
use Zend \InputFilter \InputInterface ;
18
- use Zend \Validator ;
19
- use Zend \Validator \NotEmpty ;
18
+ use Zend \Validator \NotEmpty as NotEmptyValidator ;
20
19
use Zend \Validator \ValidatorChain ;
20
+ use Zend \Validator \ValidatorInterface ;
21
21
22
22
/**
23
23
* @covers Zend\InputFilter\Input
@@ -34,18 +34,18 @@ public function setUp()
34
34
$ this ->input = new Input ('foo ' );
35
35
}
36
36
37
- public function assertRequiredValidationErrorMessage ($ input , $ message = '' )
37
+ public function assertRequiredValidationErrorMessage (Input $ input , $ message = '' )
38
38
{
39
39
$ message = $ message ?: 'Expected failure message for required input ' ;
40
40
$ message .= '; ' ;
41
41
42
42
$ messages = $ input ->getMessages ();
43
43
$ this ->assertInternalType ('array ' , $ messages , $ message . ' non-array messages array ' );
44
44
45
- $ notEmpty = new NotEmpty ();
45
+ $ notEmpty = new NotEmptyValidator ();
46
46
$ messageTemplates = $ notEmpty ->getOption ('messageTemplates ' );
47
47
$ this ->assertSame ([
48
- NotEmpty ::IS_EMPTY => $ messageTemplates [NotEmpty ::IS_EMPTY ],
48
+ NotEmptyValidator ::IS_EMPTY => $ messageTemplates [NotEmptyValidator ::IS_EMPTY ],
49
49
], $ messages , $ message . ' missing NotEmpty::IS_EMPTY key and/or contains additional messages ' );
50
50
}
51
51
@@ -64,7 +64,7 @@ public function testInputHasEmptyFilterChainByDefault()
64
64
public function testInputHasEmptyValidatorChainByDefault ()
65
65
{
66
66
$ validators = $ this ->input ->getValidatorChain ();
67
- $ this ->assertInstanceOf (Validator \ ValidatorChain::class, $ validators );
67
+ $ this ->assertInstanceOf (ValidatorChain::class, $ validators );
68
68
$ this ->assertEquals (0 , count ($ validators ));
69
69
}
70
70
@@ -77,7 +77,7 @@ public function testCanInjectFilterChain()
77
77
78
78
public function testCanInjectValidatorChain ()
79
79
{
80
- $ chain = new Validator \ ValidatorChain ();
80
+ $ chain = $ this -> createValidatorChainMock ();
81
81
$ this ->input ->setValidatorChain ($ chain );
82
82
$ this ->assertSame ($ chain , $ this ->input ->getValidatorChain ());
83
83
}
@@ -326,18 +326,6 @@ public function testValidationOperatesOnFilteredValue()
326
326
);
327
327
}
328
328
329
- public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation ()
330
- {
331
- $ this ->input ->setValue ($ this ->getDummyValue ());
332
- $ validator = new Validator \Digits ();
333
- $ this ->input ->getValidatorChain ()->attach ($ validator );
334
- $ this ->input ->setErrorMessage ('Please enter only digits ' );
335
- $ this ->assertFalse ($ this ->input ->isValid ());
336
- $ messages = $ this ->input ->getMessages ();
337
- $ this ->assertArrayNotHasKey (Validator \Digits::NOT_DIGITS , $ messages );
338
- $ this ->assertContains ('Please enter only digits ' , $ messages );
339
- }
340
-
341
329
public function testBreakOnFailureFlagIsOffByDefault ()
342
330
{
343
331
$ this ->assertFalse ($ this ->input ->breakOnFailure ());
@@ -374,14 +362,10 @@ public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value)
374
362
*/
375
363
public function testRequiredNotEmptyValidatorNotAddedWhenOneExists ($ value )
376
364
{
377
- $ this ->assertTrue ( $ this -> input ->isRequired () );
365
+ $ this ->input ->setRequired ( true );
378
366
$ this ->input ->setValue ($ value );
379
367
380
- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
381
- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
382
- $ notEmptyMock ->expects ($ this ->exactly (1 ))
383
- ->method ('isValid ' )
384
- ->will ($ this ->returnValue (false ));
368
+ $ notEmptyMock = $ this ->createNonEmptyValidatorMock (false , $ value );
385
369
386
370
$ validatorChain = $ this ->input ->getValidatorChain ();
387
371
$ validatorChain ->prependValidator ($ notEmptyMock );
@@ -395,20 +379,20 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value)
395
379
/**
396
380
* @dataProvider emptyValueProvider
397
381
*/
398
- public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain ($ value )
382
+ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain ($ valueRaw , $ valueFiltered )
399
383
{
400
- $ this -> assertTrue ( $ this ->input -> isRequired () );
401
- $ this ->input ->setValue ( $ value );
384
+ $ filterChain = $ this ->createFilterChainMock ( $ valueRaw , $ valueFiltered );
385
+ $ validatorChain = $ this ->input ->getValidatorChain ( );
402
386
403
- /** @var Validator\NotEmpty|MockObject $notEmptyMock */
404
- $ notEmptyMock = $ this ->getMock (Validator \NotEmpty::class, ['isValid ' ]);
405
- $ notEmptyMock ->expects ($ this ->exactly (1 ))
406
- ->method ('isValid ' )
407
- ->will ($ this ->returnValue (false ));
387
+ $ this ->input ->setRequired (true );
388
+ $ this ->input ->setFilterChain ($ filterChain );
389
+ $ this ->input ->setValue ($ valueRaw );
408
390
409
- $ validatorChain = $ this ->input ->getValidatorChain ();
410
- $ validatorChain ->attach (new Validator \Digits ());
391
+ $ notEmptyMock = $ this ->createNonEmptyValidatorMock (false , $ valueFiltered );
392
+
393
+ $ validatorChain ->attach ($ this ->createValidatorMock (true ));
411
394
$ validatorChain ->attach ($ notEmptyMock );
395
+
412
396
$ this ->assertFalse ($ this ->input ->isValid ());
413
397
414
398
$ validators = $ validatorChain ->getValidators ();
@@ -836,25 +820,26 @@ protected function createValidatorChainMock($isValid = null, $value = null, $con
836
820
* @param mixed $context
837
821
* @param string[] $messages
838
822
*
839
- * @return Validator\ ValidatorInterface|MockObject
823
+ * @return ValidatorInterface|MockObject
840
824
*/
841
- protected function createValidatorMock ($ isValid , $ value , $ context = null , $ messages = [])
825
+ protected function createValidatorMock ($ isValid , $ value = ' not-set ' , $ context = null , $ messages = [])
842
826
{
843
- /** @var Validator\ ValidatorInterface|MockObject $validator */
844
- $ validator = $ this ->getMock (Validator \ ValidatorInterface::class);
827
+ /** @var ValidatorInterface|MockObject $validator */
828
+ $ validator = $ this ->getMock (ValidatorInterface::class);
845
829
846
830
if (($ isValid === false ) || ($ isValid === true )) {
847
- $ validator ->expects ($ this ->once ())
831
+ $ isValidMethod = $ validator ->expects ($ this ->once ())
848
832
->method ('isValid ' )
849
- ->with ($ value , $ context )
850
833
->willReturn ($ isValid )
851
834
;
852
835
} else {
853
- $ validator ->expects ($ this ->never ())
836
+ $ isValidMethod = $ validator ->expects ($ this ->never ())
854
837
->method ('isValid ' )
855
- ->with ($ value , $ context )
856
838
;
857
839
}
840
+ if ($ value !== 'not-set ' ) {
841
+ $ isValidMethod ->with ($ value , $ context );
842
+ }
858
843
859
844
$ validator ->method ('getMessages ' )
860
845
->willReturn ($ messages )
@@ -863,6 +848,26 @@ protected function createValidatorMock($isValid, $value, $context = null, $messa
863
848
return $ validator ;
864
849
}
865
850
851
+ /**
852
+ * @param bool $isValid
853
+ * @param mixed $value
854
+ * @param mixed $context
855
+ *
856
+ * @return NotEmptyValidator|MockObject
857
+ */
858
+ protected function createNonEmptyValidatorMock ($ isValid , $ value , $ context = null )
859
+ {
860
+ /** @var NotEmptyValidator|MockObject $notEmptyMock */
861
+ $ notEmptyMock = $ this ->getMock (NotEmptyValidator::class, ['isValid ' ]);
862
+ $ notEmptyMock ->expects ($ this ->once ())
863
+ ->method ('isValid ' )
864
+ ->with ($ value , $ context )
865
+ ->willReturn ($ isValid )
866
+ ;
867
+
868
+ return $ notEmptyMock ;
869
+ }
870
+
866
871
protected function getDummyValue ($ raw = true )
867
872
{
868
873
if ($ raw ) {
0 commit comments