Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 921aa2e

Browse files
committed
Merge pull request #44 from Maks3w/feature/improve-Input-merge-tests
Consolidate of InputInterface::merge and Fix unsafe merge
2 parents 4aa7af1 + b62b4dc commit 921aa2e

File tree

4 files changed

+126
-105
lines changed

4 files changed

+126
-105
lines changed

src/Input.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ public function clearFallbackValue()
361361
public function merge(InputInterface $input)
362362
{
363363
$this->setBreakOnFailure($input->breakOnFailure());
364-
$this->setContinueIfEmpty($input->continueIfEmpty());
364+
if ($input instanceof Input) {
365+
$this->setContinueIfEmpty($input->continueIfEmpty());
366+
}
365367
$this->setErrorMessage($input->getErrorMessage());
366368
$this->setName($input->getName());
367369
$this->setRequired($input->isRequired());

test/ArrayInputTest.php

+21-25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Zend\Filter;
1414
use Zend\InputFilter\ArrayInput;
1515
use Zend\InputFilter\Exception\InvalidArgumentException;
16+
use Zend\InputFilter\Input;
1617
use Zend\Validator;
1718

1819
/**
@@ -122,31 +123,6 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
122123
$this->assertEquals($notEmptyMock, $validators[0]['instance']);
123124
}
124125

125-
public function testMerge()
126-
{
127-
$input = new ArrayInput('foo');
128-
$input->setValue([' 123 ']);
129-
$filter = new Filter\StringTrim();
130-
$input->getFilterChain()->attach($filter);
131-
$validator = new Validator\Digits();
132-
$input->getValidatorChain()->attach($validator);
133-
134-
$input2 = new ArrayInput('bar');
135-
$input2->merge($input);
136-
$validatorChain = $input->getValidatorChain();
137-
$filterChain = $input->getFilterChain();
138-
139-
$this->assertEquals([' 123 '], $input2->getRawValue());
140-
$this->assertEquals(1, $validatorChain->count());
141-
$this->assertEquals(1, $filterChain->count());
142-
143-
$validators = $validatorChain->getValidators();
144-
$this->assertInstanceOf(Validator\Digits::class, $validators[0]['instance']);
145-
146-
$filters = $filterChain->getFilters()->toArray();
147-
$this->assertInstanceOf(Filter\StringTrim::class, $filters[0]);
148-
}
149-
150126
public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
151127
{
152128
$this->assertTrue($this->input->isRequired());
@@ -186,6 +162,26 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
186162
$this->assertTrue($this->input->isValid());
187163
}
188164

165+
public function testMerge($sourceRawValue = 'bazRawValue')
166+
{
167+
parent::testMerge([$sourceRawValue]);
168+
}
169+
170+
public function testInputMerge()
171+
{
172+
$source = new Input();
173+
$source->setValue([]);
174+
$source->setContinueIfEmpty(true);
175+
176+
$target = $this->input;
177+
$target->setContinueIfEmpty(false);
178+
179+
$return = $target->merge($source);
180+
$this->assertSame($target, $return, 'merge() must return it self');
181+
182+
$this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match');
183+
}
184+
189185
public function fallbackValueVsIsValidProvider()
190186
{
191187
$dataSets = parent::fallbackValueVsIsValidProvider();

test/FileInputTest.php

+20-29
Original file line numberDiff line numberDiff line change
@@ -328,35 +328,6 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet(
328328
$this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput');
329329
}
330330

331-
public function testMerge()
332-
{
333-
$value = ['tmp_name' => 'bar'];
334-
335-
$input = new FileInput('foo');
336-
$input->setAutoPrependUploadValidator(false);
337-
$input->setValue($value);
338-
$filter = new Filter\StringTrim();
339-
$input->getFilterChain()->attach($filter);
340-
$validator = new Validator\Digits();
341-
$input->getValidatorChain()->attach($validator);
342-
343-
$input2 = new FileInput('bar');
344-
$input2->merge($input);
345-
$validatorChain = $input->getValidatorChain();
346-
$filterChain = $input->getFilterChain();
347-
348-
$this->assertFalse($input2->getAutoPrependUploadValidator());
349-
$this->assertEquals($value, $input2->getRawValue());
350-
$this->assertEquals(1, $validatorChain->count());
351-
$this->assertEquals(1, $filterChain->count());
352-
353-
$validators = $validatorChain->getValidators();
354-
$this->assertInstanceOf(Validator\Digits::class, $validators[0]['instance']);
355-
356-
$filters = $filterChain->getFilters()->toArray();
357-
$this->assertInstanceOf(Filter\StringTrim::class, $filters[0]);
358-
}
359-
360331
public function testIsEmptyFileNotArray()
361332
{
362333
$rawValue = 'file';
@@ -415,6 +386,26 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
415386
$this->markTestSkipped('does not apply to FileInput');
416387
}
417388

389+
/**
390+
* Specific FileInput::merge extras
391+
*/
392+
public function testFileInputMerge()
393+
{
394+
$source = new FileInput();
395+
$source->setAutoPrependUploadValidator(true);
396+
397+
$target = $this->input;
398+
$target->setAutoPrependUploadValidator(false);
399+
400+
$return = $target->merge($source);
401+
$this->assertSame($target, $return, 'merge() must return it self');
402+
403+
$this->assertEquals(
404+
true,
405+
$target->getAutoPrependUploadValidator(),
406+
'getAutoPrependUploadValidator() value not match'
407+
);
408+
}
418409

419410
public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider()
420411
{

test/InputTest.php

+82-50
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use PHPUnit_Framework_TestCase as TestCase;
1414
use stdClass;
1515
use Zend\Filter;
16+
use Zend\Filter\FilterChain;
1617
use Zend\InputFilter\Input;
18+
use Zend\InputFilter\InputInterface;
1719
use Zend\Validator;
1820
use 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

Comments
 (0)