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

Commit

Permalink
Input fallback feature should work when required and optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Sep 1, 2015
1 parent 152ee5b commit 44cd418
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -90,7 +90,7 @@ public function isValid($context = null)
if (!$result) {
if ($hasFallback) {
$this->setValue($this->getFallbackValue());
$result = true;
return true;
}
break;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test/FileInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
}

public function testFallbackValueVsIsValidRules(
$required = null,
$fallbackValue = null,
$originalValue = null,
$isValid = null,
Expand All @@ -322,6 +323,7 @@ public function testFallbackValueVsIsValidRules(


public function testFallbackValueVsIsValidRulesWhenValueNotSet(
$required = null,
$fallbackValue = null,
$originalValue = null,
$isValid = null,
Expand Down
13 changes: 9 additions & 4 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -931,6 +933,7 @@ public function testResetValueReturnsInputValueToDefaultValue($value)

public function fallbackValueVsIsValidProvider()
{
$required = true;
$isValid = true;

$originalValue = 'fooValue';
Expand All @@ -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
}
Expand Down

0 comments on commit 44cd418

Please sign in to comment.