From 8c35a2060ef8d12c420e5e0815b880bc070c2071 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Fri, 21 Aug 2015 07:31:17 +0200 Subject: [PATCH 1/4] [Input] Add hasValue()/resetValue() methods This methods provide a flag for distinguish when $value contains a real or the default property value. --- src/ArrayInput.php | 10 ++++++++++ src/Input.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++ test/InputTest.php | 29 +++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 3d1b0144..05e8a306 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -31,6 +31,16 @@ public function setValue($value) return parent::setValue($value); } + /** + * {@inheritdoc} + */ + public function resetValue() + { + $this->value = []; + $this->hasValue = false; + return $this; + } + /** * @return array */ diff --git a/src/Input.php b/src/Input.php index 6fa86a21..70dce948 100644 --- a/src/Input.php +++ b/src/Input.php @@ -67,6 +67,15 @@ class Input implements */ protected $value; + /** + * Flag for distinguish when $value contains a real `null` or the PHP default property value. + * + * PHP gives to all properties a default value of `null` unless other default value is set. + * + * @var bool + */ + protected $hasValue = false; + /** * @var mixed */ @@ -163,12 +172,36 @@ public function setValidatorChain(ValidatorChain $validatorChain) } /** + * Set the input value. + * + * If you want to remove/unset the current value use {@link Input::resetValue()}. + * + * @see Input::getValue() For retrieve the input value. + * @see Input::hasValue() For to know if input value was set. + * @see Input::resetValue() For reset the input value to the default state. + * * @param mixed $value * @return Input */ public function setValue($value) { $this->value = $value; + $this->hasValue = true; + return $this; + } + + /** + * Reset input value to the default state. + * + * @see Input::hasValue() For to know if input value was set. + * @see Input::setValue() For set a new value. + * + * @return Input + */ + public function resetValue() + { + $this->value = null; + $this->hasValue = false; return $this; } @@ -270,6 +303,23 @@ public function getValue() return $filter->filter($this->value); } + /** + * Flag for inform if input value was set. + * + * This flag used for distinguish when {@link Input::getValue()} will return a real `null` value or the PHP default + * value. + * + * @see Input::getValue() For retrieve the input value. + * @see Input::setValue() For set a new value. + * @see Input::resetValue() For reset the input value to the default state. + * + * @return bool + */ + public function hasValue() + { + return $this->hasValue; + } + /** * @return mixed */ diff --git a/test/InputTest.php b/test/InputTest.php index 100d5317..ba639b1a 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -854,4 +854,33 @@ public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsA $input->setValue($value); $this->{$assertion}($input->isValid()); } + + /** + * @dataProvider emptyValuesProvider + */ + public function testSetValuePutInputInTheDesiredState($value) + { + $input = $this->input; + $this->assertFalse($input->hasValue(), 'Input should not have value by default'); + + $input->setValue($value); + $this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set"); + } + + /** + * @dataProvider emptyValuesProvider + */ + public function testResetValueReturnsInputValueToDefaultValue($value) + { + $input = $this->input; + $originalInput = clone $input; + $this->assertFalse($input->hasValue(), 'Input should not have value by default'); + + $input->setValue($value); + $this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set"); + + $return = $input->resetValue(); + $this->assertSame($input, $return, 'resetValue() must return itself'); + $this->assertEquals($originalInput, $input, 'Input was not reset to the default value state'); + } } From de0e3c9fa3ad3ad945160f40f27a2b497d1c053c Mon Sep 17 00:00:00 2001 From: Maks3w Date: Thu, 27 Aug 2015 09:43:02 +0200 Subject: [PATCH 2/4] Delegate to InputInterface::isValid when data not exists Delegate to input isValid grant full control about the fallback value feature. This commit also consolidate Input::setFallbackValue tests --- src/ArrayInput.php | 16 ++++- src/BaseInputFilter.php | 37 ++-------- src/FileInput.php | 6 ++ src/Input.php | 11 +++ test/ArrayInputTest.php | 12 ++++ test/FileInputTest.php | 19 +++++ test/InputTest.php | 155 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 224 insertions(+), 32 deletions(-) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 05e8a306..517dee6b 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -60,6 +60,20 @@ public function getValue() */ public function isValid($context = null) { + $hasValue = $this->hasValue(); + $required = $this->isRequired(); + $hasFallback = $this->hasFallback(); + + if (! $hasValue && $required && !$hasFallback) { + $this->setErrorMessage('Value is required'); + return false; + } + + if (! $hasValue && $required && $hasFallback) { + $this->setValue($this->getFallbackValue()); + return true; + } + if (!$this->continueIfEmpty() && !$this->allowEmpty()) { $this->injectNotEmptyValidator(); } @@ -74,7 +88,7 @@ public function isValid($context = null) } $result = $validator->isValid($value, $context); if (!$result) { - if ($this->hasFallback()) { + if ($hasFallback) { $this->setValue($this->getFallbackValue()); $result = true; } diff --git a/src/BaseInputFilter.php b/src/BaseInputFilter.php index 028d5f40..a5cdd488 100644 --- a/src/BaseInputFilter.php +++ b/src/BaseInputFilter.php @@ -254,8 +254,6 @@ protected function validateInputs(array $inputs, $data = [], $context = null) continue; } - $hasFallback = ($input instanceof Input && $input->hasFallback()); - // If input is optional (not required), and value is not set, then ignore. if (!array_key_exists($name, $data) && !$input->isRequired() @@ -263,34 +261,6 @@ protected function validateInputs(array $inputs, $data = [], $context = null) continue; } - // If the value is required, not present in the data set, and - // has no fallback, validation fails. - if (!array_key_exists($name, $data) - && $input->isRequired() - && !$hasFallback - ) { - $input->setErrorMessage('Value is required'); - $this->invalidInputs[$name] = $input; - - if ($input->breakOnFailure()) { - return false; - } - - $valid = false; - continue; - } - - // If the value is required, not present in the data set, and - // has a fallback, validation passes, and we set the input - // value to the fallback. - if (!array_key_exists($name, $data) - && $input->isRequired() - && $hasFallback - ) { - $input->setValue($input->getFallbackValue()); - continue; - } - // Make sure we have a value (empty) for validation of context if (!array_key_exists($name, $data)) { $data[$name] = null; @@ -545,7 +515,7 @@ protected function populate() $input->clearRawValues(); } - if (!isset($this->data[$name])) { + if (!array_key_exists($name, $this->data)) { // No value; clear value in this input if ($input instanceof InputFilterInterface) { $input->setData([]); @@ -557,6 +527,11 @@ protected function populate() continue; } + if ($input instanceof Input) { + $input->resetValue(); + continue; + } + $input->setValue(null); continue; } diff --git a/src/FileInput.php b/src/FileInput.php index 80748f67..3b87a9d4 100644 --- a/src/FileInput.php +++ b/src/FileInput.php @@ -113,11 +113,17 @@ public function isEmptyFile($rawValue) public function isValid($context = null) { $rawValue = $this->getRawValue(); + $hasValue = $this->hasValue(); $empty = $this->isEmptyFile($rawValue); $required = $this->isRequired(); $allowEmpty = $this->allowEmpty(); $continueIfEmpty = $this->continueIfEmpty(); + if (! $hasValue && $required && ! $this->hasFallback()) { + $this->setErrorMessage('Value is required'); + return false; + } + if ($empty && ! $required && ! $continueIfEmpty) { return true; } diff --git a/src/Input.php b/src/Input.php index 70dce948..03b2ee6d 100644 --- a/src/Input.php +++ b/src/Input.php @@ -371,11 +371,22 @@ public function merge(InputInterface $input) public function isValid($context = null) { $value = $this->getValue(); + $hasValue = $this->hasValue(); $empty = ($value === null || $value === '' || $value === []); $required = $this->isRequired(); $allowEmpty = $this->allowEmpty(); $continueIfEmpty = $this->continueIfEmpty(); + if (! $hasValue && $required && ! $this->hasFallback()) { + $this->setErrorMessage('Value is required'); + return false; + } + + if (! $hasValue && $required && $this->hasFallback()) { + $this->setValue($this->getFallbackValue()); + return true; + } + if ($empty && ! $required && ! $continueIfEmpty) { return true; } diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 9f049917..130caad2 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -246,4 +246,16 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid() })); $this->assertTrue($this->input->isValid()); } + + public function fallbackValueVsIsValidProvider() + { + $dataSets = parent::fallbackValueVsIsValidProvider(); + array_walk($dataSets, function (&$set) { + $set[0] = [$set[0]]; // Wrap fallback value into an array. + $set[1] = [$set[1]]; // Wrap value into an array. + $set[3] = [$set[3]]; // Wrap expected value into an array. + }); + + return $dataSets; + } } diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 51fbffa9..67a5547d 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -311,6 +311,25 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists() $this->markTestSkipped('Test is not enabled in FileInputTest'); } + public function testFallbackValueVsIsValidRules( + $fallbackValue = null, + $originalValue = null, + $isValid = null, + $expectedValue = null + ) { + $this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput'); + } + + + public function testFallbackValueVsIsValidRulesWhenValueNotSet( + $fallbackValue = null, + $originalValue = null, + $isValid = null, + $expectedValue = null + ) { + $this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput'); + } + public function testMerge() { $value = ['tmp_name' => 'bar']; diff --git a/test/InputTest.php b/test/InputTest.php index ba639b1a..55be893d 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -9,11 +9,14 @@ namespace ZendTest\InputFilter; +use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit_Framework_TestCase as TestCase; use RuntimeException; +use stdClass; use Zend\Filter; use Zend\InputFilter\Input; use Zend\Validator; +use Zend\Validator\ValidatorChain; class InputTest extends TestCase { @@ -95,6 +98,78 @@ public function testContinueIfEmptyFlagIsMutable() $this->assertTrue($input->continueIfEmpty()); } + /** + * @dataProvider setValueProvider + */ + public function testSetFallbackValue($fallbackValue) + { + $input = $this->input; + + $return = $input->setFallbackValue($fallbackValue); + $this->assertSame($input, $return, 'setFallbackValue() must return it self'); + + $this->assertEquals($fallbackValue, $input->getFallbackValue(), 'getFallbackValue() value not match'); + $this->assertEquals(true, $input->hasFallback(), 'hasFallback() value not match'); + } + + /** + * @dataProvider fallbackValueVsIsValidProvider + */ + public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue, $isValid, $expectedValue) + { + $input = $this->input; + $input->setContinueIfEmpty(true); + + $input->setValidatorChain($this->createValidatorChainMock($isValid)); + $input->setFallbackValue($fallbackValue); + $input->setValue($originalValue); + + $this->assertTrue( + $input->isValid(), + 'isValid() should be return always true when fallback value is set. Detail: ' . + json_encode($input->getMessages()) + ); + $this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid'); + $this->assertSame($expectedValue, $input->getRawValue(), 'getRawValue() value not match'); + $this->assertSame($expectedValue, $input->getValue(), 'getValue() value not match'); + } + + /** + * @dataProvider fallbackValueVsIsValidProvider + */ + public function testFallbackValueVsIsValidRulesWhenValueNotSet($fallbackValue, $originalValue, $isValid) + { + $expectedValue = $fallbackValue; // Should always return the fallback value + + $input = $this->input; + $input->setContinueIfEmpty(true); + + $input->setValidatorChain($this->createValidatorChainMock($isValid)); + $input->setFallbackValue($fallbackValue); + + $this->assertTrue( + $input->isValid(), + 'isValid() should be return always true when fallback value is set. Detail: ' . + json_encode($input->getMessages()) + ); + $this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid'); + $this->assertSame($expectedValue, $input->getRawValue(), 'getRawValue() value not match'); + $this->assertSame($expectedValue, $input->getValue(), 'getValue() value not match'); + } + + public function testRequiredWithoutFallbackAndValueNotSetThenFail() + { + $input = $this->input; + $input->setRequired(true); + $input->setContinueIfEmpty(true); + + $this->assertFalse( + $input->isValid(), + 'isValid() should be return always false when no fallback value, is required, and not data is set.' + ); + $this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() should be empty because the input is valid'); + } + public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue() { $input = new Input('foo'); @@ -883,4 +958,84 @@ public function testResetValueReturnsInputValueToDefaultValue($value) $this->assertSame($input, $return, 'resetValue() must return itself'); $this->assertEquals($originalInput, $input, 'Input was not reset to the default value state'); } + + public function fallbackValueVsIsValidProvider() + { + $isValid = true; + + $originalValue = 'fooValue'; + $fallbackValue = 'fooFallbackValue'; + + // @codingStandardsIgnoreStart + return [ + // Description => [$inputIsRequired, $fallbackValue, $originalValue, $isValid, $expectedValue] + 'Input: Invalid. getValue: fallback' => [$fallbackValue, $originalValue, !$isValid, $fallbackValue], + 'Input: Valid. getValue: original' => [$fallbackValue, $originalValue, $isValid, $originalValue], + ]; + // @codingStandardsIgnoreEnd + } + + public function setValueProvider() + { + $emptyValues = $this->emptyValueProvider(); + $mixedValues = $this->mixedValueProvider(); + + $values = array_merge($emptyValues, $mixedValues); + + return $values; + } + + public function emptyValueProvider() + { + return [ + // Description => [$value] + 'null' => [null], + '""' => [''], +// '"0"' => ['0'], +// '0' => [0], +// '0.0' => [0.0], +// 'false' => [false], + '[]' => [[]], + ]; + } + + public function mixedValueProvider() + { + return [ + // Description => [$value] + '"0"' => ['0'], + '0' => [0], + '0.0' => [0.0], + 'false' => [false], + 'php' => ['php'], + 'whitespace' => [' '], + '1' => [1], + '1.0' => [1.0], + 'true' => [true], + '["php"]' => [['php']], + 'object' => [new stdClass()], + // @codingStandardsIgnoreStart + 'callable' => [function () {}], + // @codingStandardsIgnoreEnd + ]; + } + + /** + * @param null|bool $isValid If set stub isValid method for return the argument value. + * + * @return MockObject|ValidatorChain + */ + protected function createValidatorChainMock($isValid = null) + { + /** @var ValidatorChain|MockObject $validatorChain */ + $validatorChain = $this->getMock(ValidatorChain::class); + + if ($isValid !== null) { + $validatorChain->method('isValid') + ->willReturn($isValid) + ; + } + + return $validatorChain; + } } From 152ee5baf04ebb7cf07722668c11a512f18d87d9 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 1 Sep 2015 09:00:32 +0200 Subject: [PATCH 3/4] Remove legacy setFallbackValue tests --- test/ArrayInputTest.php | 33 --------------- test/BaseInputFilterTest.php | 80 ------------------------------------ test/FileInputTest.php | 5 --- test/InputTest.php | 30 -------------- 4 files changed, 148 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 130caad2..e4242345 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -187,39 +187,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain() $this->assertEquals($notEmptyMock, $validators[1]['instance']); } - public function dataFallbackValue() - { - return [ - [ - 'fallbackValue' => [] - ], - [ - 'fallbackValue' => [''], - ], - [ - 'fallbackValue' => [null], - ], - [ - 'fallbackValue' => ['some value'], - ], - ]; - } - - /** - * @dataProvider dataFallbackValue - */ - public function testFallbackValue($fallbackValue) - { - $this->input->setFallbackValue($fallbackValue); - $validator = new Validator\Date(); - $this->input->getValidatorChain()->attach($validator); - $this->input->setValue(['123']); // not a date - - $this->assertTrue($this->input->isValid()); - $this->assertEmpty($this->input->getMessages()); - $this->assertSame($fallbackValue, $this->input->getValue()); - } - public function emptyValuesProvider() { return [ diff --git a/test/BaseInputFilterTest.php b/test/BaseInputFilterTest.php index 3fa714af..ada72dc9 100644 --- a/test/BaseInputFilterTest.php +++ b/test/BaseInputFilterTest.php @@ -1035,86 +1035,6 @@ public function testEmptyValuePassedForRequiredButAllowedEmptyInputShouldMarkInp $this->assertTrue($filter->isValid(), 'Empty value should mark input filter as valid'); } - /** - * @group 10 - */ - public function testMissingRequiredWithFallbackShouldMarkInputValid() - { - $foo = new Input('foo'); - $foo->setRequired(true); - $foo->setAllowEmpty(false); - - $bar = new Input('bar'); - $bar->setRequired(true); - $bar->setFallbackValue('baz'); - - $filter = new InputFilter(); - $filter->add($foo); - $filter->add($bar); - - $filter->setData(['foo' => 'xyz']); - $this->assertTrue($filter->isValid(), 'Missing input with fallback value should mark input filter as valid'); - $data = $filter->getValues(); - $this->assertArrayHasKey('bar', $data); - $this->assertEquals($bar->getFallbackValue(), $data['bar']); - } - - /** - * @group 10 - */ - public function testMissingRequiredThatAllowsEmptyWithFallbackShouldMarkInputValid() - { - $foo = new Input('foo'); - $foo->setRequired(true); - $foo->setAllowEmpty(false); - - $bar = new Input('bar'); - $bar->setRequired(true); - $bar->setAllowEmpty(true); - $bar->setFallbackValue('baz'); - - $filter = new InputFilter(); - $filter->add($foo); - $filter->add($bar); - - $filter->setData(['foo' => 'xyz']); - $this->assertTrue($filter->isValid(), 'Missing input with fallback value should mark input filter as valid'); - $data = $filter->getValues(); - $this->assertArrayHasKey('bar', $data); - $this->assertEquals($bar->getFallbackValue(), $data['bar']); - $this->assertArrayNotHasKey('bar', $filter->getValidInput()); - $this->assertArrayNotHasKey('bar', $filter->getInvalidInput()); - } - - /** - * @group 10 - */ - public function testEmptyRequiredValueWithFallbackShouldMarkInputValid() - { - $foo = new Input('foo'); - $foo->setRequired(true); - $foo->setAllowEmpty(false); - - $bar = new Input('bar'); - $bar->setRequired(true); - $bar->setFallbackValue('baz'); - - $filter = new InputFilter(); - $filter->add($foo); - $filter->add($bar); - - $filter->setData([ - 'foo' => 'xyz', - 'bar' => null, - ]); - $this->assertTrue($filter->isValid(), 'Empty input with fallback value should mark input filter as valid'); - $data = $filter->getValues(); - $this->assertArrayHasKey('bar', $data); - $this->assertEquals($bar->getFallbackValue(), $data['bar']); - $this->assertArrayHasKey('bar', $filter->getValidInput()); - $this->assertArrayNotHasKey('bar', $filter->getInvalidInput()); - } - /** * @group 15 */ diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 67a5547d..288f4fcf 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -359,11 +359,6 @@ public function testMerge() $this->assertInstanceOf('Zend\Filter\StringTrim', $filters[0]); } - public function testFallbackValue($fallbackValue = null) - { - $this->markTestSkipped('Not use fallback value'); - } - public function testIsEmptyFileNotArray() { $rawValue = 'file'; diff --git a/test/InputTest.php b/test/InputTest.php index 55be893d..41a23e55 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -431,36 +431,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain() $this->assertEquals($notEmptyMock, $validators[1]['instance']); } - public function dataFallbackValue() - { - return [ - [ - 'fallbackValue' => null - ], - [ - 'fallbackValue' => '' - ], - [ - 'fallbackValue' => 'some value' - ], - ]; - } - - /** - * @dataProvider dataFallbackValue - */ - public function testFallbackValue($fallbackValue) - { - $this->input->setFallbackValue($fallbackValue); - $validator = new Validator\Date(); - $this->input->getValidatorChain()->attach($validator); - $this->input->setValue('123'); // not a date - - $this->assertTrue($this->input->isValid()); - $this->assertEmpty($this->input->getMessages()); - $this->assertSame($fallbackValue, $this->input->getValue()); - } - public function testMergeRetainsContinueIfEmptyFlag() { $input = new Input('foo'); From 44cd4188120fe803818e072acd7648041a1cc06b Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 1 Sep 2015 09:01:55 +0200 Subject: [PATCH 4/4] Input fallback feature should work when required and optional --- src/ArrayInput.php | 14 +++++++------- src/Input.php | 12 ++++++------ test/ArrayInputTest.php | 6 +++--- test/FileInputTest.php | 2 ++ test/InputTest.php | 13 +++++++++---- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 517dee6b..ef513fe0 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -64,16 +64,16 @@ public function isValid($context = null) $required = $this->isRequired(); $hasFallback = $this->hasFallback(); - if (! $hasValue && $required && !$hasFallback) { - $this->setErrorMessage('Value is required'); - return false; - } - - if (! $hasValue && $required && $hasFallback) { + if (! $hasValue && $hasFallback) { $this->setValue($this->getFallbackValue()); return true; } + if (! $hasValue && $required) { + $this->setErrorMessage('Value is required'); + return false; + } + if (!$this->continueIfEmpty() && !$this->allowEmpty()) { $this->injectNotEmptyValidator(); } @@ -90,7 +90,7 @@ public function isValid($context = null) if (!$result) { if ($hasFallback) { $this->setValue($this->getFallbackValue()); - $result = true; + return true; } break; } diff --git a/src/Input.php b/src/Input.php index 03b2ee6d..575a390b 100644 --- a/src/Input.php +++ b/src/Input.php @@ -377,16 +377,16 @@ public function isValid($context = null) $allowEmpty = $this->allowEmpty(); $continueIfEmpty = $this->continueIfEmpty(); - if (! $hasValue && $required && ! $this->hasFallback()) { - $this->setErrorMessage('Value is required'); - return false; - } - - if (! $hasValue && $required && $this->hasFallback()) { + if (! $hasValue && $this->hasFallback()) { $this->setValue($this->getFallbackValue()); return true; } + if (! $hasValue && $required) { + $this->setErrorMessage('Value is required'); + return false; + } + if ($empty && ! $required && ! $continueIfEmpty) { return true; } diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index e4242345..ed0b1a0b 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -218,9 +218,9 @@ public function fallbackValueVsIsValidProvider() { $dataSets = parent::fallbackValueVsIsValidProvider(); array_walk($dataSets, function (&$set) { - $set[0] = [$set[0]]; // Wrap fallback value into an array. - $set[1] = [$set[1]]; // Wrap value into an array. - $set[3] = [$set[3]]; // Wrap expected value into an array. + $set[1] = [$set[1]]; // Wrap fallback value into an array. + $set[2] = [$set[2]]; // Wrap value into an array. + $set[4] = [$set[4]]; // Wrap expected value into an array. }); return $dataSets; diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 288f4fcf..b22eed54 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -312,6 +312,7 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists() } public function testFallbackValueVsIsValidRules( + $required = null, $fallbackValue = null, $originalValue = null, $isValid = null, @@ -322,6 +323,7 @@ public function testFallbackValueVsIsValidRules( public function testFallbackValueVsIsValidRulesWhenValueNotSet( + $required = null, $fallbackValue = null, $originalValue = null, $isValid = null, diff --git a/test/InputTest.php b/test/InputTest.php index 41a23e55..dfa74763 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -115,11 +115,12 @@ public function testSetFallbackValue($fallbackValue) /** * @dataProvider fallbackValueVsIsValidProvider */ - public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue, $isValid, $expectedValue) + public function testFallbackValueVsIsValidRules($required, $fallbackValue, $originalValue, $isValid, $expectedValue) { $input = $this->input; $input->setContinueIfEmpty(true); + $input->setRequired($required); $input->setValidatorChain($this->createValidatorChainMock($isValid)); $input->setFallbackValue($fallbackValue); $input->setValue($originalValue); @@ -137,13 +138,14 @@ public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue, /** * @dataProvider fallbackValueVsIsValidProvider */ - public function testFallbackValueVsIsValidRulesWhenValueNotSet($fallbackValue, $originalValue, $isValid) + public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallbackValue, $originalValue, $isValid) { $expectedValue = $fallbackValue; // Should always return the fallback value $input = $this->input; $input->setContinueIfEmpty(true); + $input->setRequired($required); $input->setValidatorChain($this->createValidatorChainMock($isValid)); $input->setFallbackValue($fallbackValue); @@ -931,6 +933,7 @@ public function testResetValueReturnsInputValueToDefaultValue($value) public function fallbackValueVsIsValidProvider() { + $required = true; $isValid = true; $originalValue = 'fooValue'; @@ -939,8 +942,10 @@ public function fallbackValueVsIsValidProvider() // @codingStandardsIgnoreStart return [ // Description => [$inputIsRequired, $fallbackValue, $originalValue, $isValid, $expectedValue] - 'Input: Invalid. getValue: fallback' => [$fallbackValue, $originalValue, !$isValid, $fallbackValue], - 'Input: Valid. getValue: original' => [$fallbackValue, $originalValue, $isValid, $originalValue], + 'Required: T, Input: Invalid. getValue: fallback' => [ $required, $fallbackValue, $originalValue, !$isValid, $fallbackValue], + 'Required: T, Input: Valid. getValue: original' => [ $required, $fallbackValue, $originalValue, $isValid, $originalValue], + 'Required: F, Input: Invalid. getValue: fallback' => [!$required, $fallbackValue, $originalValue, !$isValid, $fallbackValue], + 'Required: F, Input: Valid. getValue: original' => [!$required, $fallbackValue, $originalValue, $isValid, $originalValue], ]; // @codingStandardsIgnoreEnd }