1313use PHPUnit_Framework_TestCase as TestCase ;
1414use stdClass ;
1515use Zend \Filter ;
16+ use Zend \Filter \FilterChain ;
1617use Zend \InputFilter \Input ;
18+ use Zend \InputFilter \InputInterface ;
1719use Zend \Validator ;
1820use Zend \Validator \ValidatorChain ;
1921
@@ -300,31 +302,6 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
300302 $ this ->assertTrue ($ this ->input ->isValid ());
301303 }
302304
303- public function testMerge ()
304- {
305- $ input = new Input ('foo ' );
306- $ input ->setValue (' 123 ' );
307- $ filter = new Filter \StringTrim ();
308- $ input ->getFilterChain ()->attach ($ filter );
309- $ validator = new Validator \Digits ();
310- $ input ->getValidatorChain ()->attach ($ validator );
311-
312- $ input2 = new Input ('bar ' );
313- $ input2 ->merge ($ input );
314- $ validatorChain = $ input ->getValidatorChain ();
315- $ filterChain = $ input ->getFilterChain ();
316-
317- $ this ->assertEquals (' 123 ' , $ input2 ->getRawValue ());
318- $ this ->assertEquals (1 , $ validatorChain ->count ());
319- $ this ->assertEquals (1 , $ filterChain ->count ());
320-
321- $ validators = $ validatorChain ->getValidators ();
322- $ this ->assertInstanceOf (Validator \Digits::class, $ validators [0 ]['instance ' ]);
323-
324- $ filters = $ filterChain ->getFilters ()->toArray ();
325- $ this ->assertInstanceOf (Filter \StringTrim::class, $ filters [0 ]);
326- }
327-
328305 public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain ()
329306 {
330307 $ this ->assertTrue ($ this ->input ->isRequired ());
@@ -346,31 +323,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
346323 $ this ->assertEquals ($ notEmptyMock , $ validators [1 ]['instance ' ]);
347324 }
348325
349- public function testMergeRetainsContinueIfEmptyFlag ()
350- {
351- $ input = new Input ('foo ' );
352- $ input ->setContinueIfEmpty (true );
353-
354- $ input2 = new Input ('bar ' );
355- $ input2 ->merge ($ input );
356- $ this ->assertTrue ($ input2 ->continueIfEmpty ());
357- }
358-
359- public function testMergeRetainsAllowEmptyFlag ()
360- {
361- $ input = new Input ('foo ' );
362- $ input ->setRequired (true );
363- $ input ->setAllowEmpty (true );
364-
365- $ input2 = new Input ('bar ' );
366- $ input2 ->setRequired (true );
367- $ input2 ->setAllowEmpty (false );
368- $ input2 ->merge ($ input );
369-
370- $ this ->assertTrue ($ input2 ->isRequired ());
371- $ this ->assertTrue ($ input2 ->allowEmpty ());
372- }
373-
374326 /**
375327 * @group 7448
376328 * @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider
@@ -429,6 +381,64 @@ public function testResetValueReturnsInputValueToDefaultValue($value)
429381 $ this ->assertEquals ($ originalInput , $ input , 'Input was not reset to the default value state ' );
430382 }
431383
384+ public function testMerge ($ sourceRawValue = 'bazRawValue ' )
385+ {
386+ $ source = $ this ->createInputInterfaceMock ();
387+ $ source ->method ('getName ' )->willReturn ('bazInput ' );
388+ $ source ->method ('getErrorMessage ' )->willReturn ('bazErrorMessage ' );
389+ $ source ->method ('breakOnFailure ' )->willReturn (true );
390+ $ source ->method ('isRequired ' )->willReturn (true );
391+ $ source ->method ('getRawValue ' )->willReturn ($ sourceRawValue );
392+ $ source ->method ('getFilterChain ' )->willReturn ($ this ->createFilterChainMock ());
393+ $ source ->method ('getValidatorChain ' )->willReturn ($ this ->createValidatorChainMock ());
394+
395+ $ targetFilterChain = $ this ->createFilterChainMock ();
396+ $ targetFilterChain ->expects (TestCase::once ())
397+ ->method ('merge ' )
398+ ->with ($ source ->getFilterChain ())
399+ ;
400+
401+ $ targetValidatorChain = $ this ->createValidatorChainMock ();
402+ $ targetValidatorChain ->expects (TestCase::once ())
403+ ->method ('merge ' )
404+ ->with ($ source ->getValidatorChain ())
405+ ;
406+
407+ $ target = $ this ->input ;
408+ $ target ->setName ('fooInput ' );
409+ $ target ->setErrorMessage ('fooErrorMessage ' );
410+ $ target ->setBreakOnFailure (false );
411+ $ target ->setRequired (false );
412+ $ target ->setFilterChain ($ targetFilterChain );
413+ $ target ->setValidatorChain ($ targetValidatorChain );
414+
415+ $ return = $ target ->merge ($ source );
416+ $ this ->assertSame ($ target , $ return , 'merge() must return it self ' );
417+
418+ $ this ->assertEquals ('bazInput ' , $ target ->getName (), 'getName() value not match ' );
419+ $ this ->assertEquals ('bazErrorMessage ' , $ target ->getErrorMessage (), 'getErrorMessage() value not match ' );
420+ $ this ->assertEquals (true , $ target ->breakOnFailure (), 'breakOnFailure() value not match ' );
421+ $ this ->assertEquals (true , $ target ->isRequired (), 'isRequired() value not match ' );
422+ $ this ->assertEquals ($ sourceRawValue , $ target ->getRawValue (), 'getRawValue() value not match ' );
423+ }
424+
425+ /**
426+ * Specific Input::merge extras
427+ */
428+ public function testInputMerge ()
429+ {
430+ $ source = new Input ();
431+ $ source ->setContinueIfEmpty (true );
432+
433+ $ target = $ this ->input ;
434+ $ target ->setContinueIfEmpty (false );
435+
436+ $ return = $ target ->merge ($ source );
437+ $ this ->assertSame ($ target , $ return , 'merge() must return it self ' );
438+
439+ $ this ->assertEquals (true , $ target ->continueIfEmpty (), 'continueIfEmpty() value not match ' );
440+ }
441+
432442 public function fallbackValueVsIsValidProvider ()
433443 {
434444 $ required = true ;
@@ -558,6 +568,28 @@ public function mixedValueProvider()
558568 ];
559569 }
560570
571+ /**
572+ * @return InputInterface|MockObject|
573+ */
574+ protected function createInputInterfaceMock ()
575+ {
576+ /** @var InputInterface|MockObject $source */
577+ $ source = $ this ->getMock (InputInterface::class);
578+
579+ return $ source ;
580+ }
581+
582+ /**
583+ * @return FilterChain|MockObject
584+ */
585+ protected function createFilterChainMock ()
586+ {
587+ /** @var FilterChain|MockObject $filterChain */
588+ $ filterChain = $ this ->getMock (FilterChain::class);
589+
590+ return $ filterChain ;
591+ }
592+
561593 /**
562594 * @param null|bool $isValid If set stub isValid method for return the argument value.
563595 *
0 commit comments