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

Commit e4ad477

Browse files
committed
Input fallback feature should work when required and optional
1 parent 05fb482 commit e4ad477

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

src/ArrayInput.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ public function isValid($context = null)
6464
$required = $this->isRequired();
6565
$hasFallback = $this->hasFallback();
6666

67-
if (! $hasValue && $required && !$hasFallback) {
68-
$this->setErrorMessage('Value is required');
69-
return false;
70-
}
71-
72-
if (! $hasValue && $required && $hasFallback) {
67+
if (! $hasValue && $hasFallback) {
7368
$this->setValue($this->getFallbackValue());
7469
return true;
7570
}
7671

72+
if (! $hasValue && $required) {
73+
$this->setErrorMessage('Value is required');
74+
return false;
75+
}
76+
7777
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
7878
$this->injectNotEmptyValidator();
7979
}
@@ -90,7 +90,7 @@ public function isValid($context = null)
9090
if (!$result) {
9191
if ($hasFallback) {
9292
$this->setValue($this->getFallbackValue());
93-
$result = true;
93+
return true;
9494
}
9595
break;
9696
}

src/Input.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,16 @@ public function isValid($context = null)
377377
$allowEmpty = $this->allowEmpty();
378378
$continueIfEmpty = $this->continueIfEmpty();
379379

380-
if (! $hasValue && $required && ! $this->hasFallback()) {
381-
$this->setErrorMessage('Value is required');
382-
return false;
383-
}
384-
385-
if (! $hasValue && $required && $this->hasFallback()) {
380+
if (! $hasValue && $this->hasFallback()) {
386381
$this->setValue($this->getFallbackValue());
387382
return true;
388383
}
389384

385+
if (! $hasValue && $required) {
386+
$this->setErrorMessage('Value is required');
387+
return false;
388+
}
389+
390390
if ($empty && ! $required && ! $continueIfEmpty) {
391391
return true;
392392
}

test/ArrayInputTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ public function fallbackValueVsIsValidProvider()
218218
{
219219
$dataSets = parent::fallbackValueVsIsValidProvider();
220220
array_walk($dataSets, function (&$set) {
221-
$set[0] = [$set[0]]; // Wrap fallback value into an array.
222-
$set[1] = [$set[1]]; // Wrap value into an array.
223-
$set[3] = [$set[3]]; // Wrap expected value into an array.
221+
$set[1] = [$set[1]]; // Wrap fallback value into an array.
222+
$set[2] = [$set[2]]; // Wrap value into an array.
223+
$set[4] = [$set[4]]; // Wrap expected value into an array.
224224
});
225225

226226
return $dataSets;

test/FileInputTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
312312
}
313313

314314
public function testFallbackValueVsIsValidRules(
315+
$required = null,
315316
$fallbackValue = null,
316317
$originalValue = null,
317318
$isValid = null,
@@ -322,6 +323,7 @@ public function testFallbackValueVsIsValidRules(
322323

323324

324325
public function testFallbackValueVsIsValidRulesWhenValueNotSet(
326+
$required = null,
325327
$fallbackValue = null,
326328
$originalValue = null,
327329
$isValid = null,

test/InputTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ public function testSetFallbackValue($fallbackValue)
115115
/**
116116
* @dataProvider fallbackValueVsIsValidProvider
117117
*/
118-
public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue, $isValid, $expectedValue)
118+
public function testFallbackValueVsIsValidRules($required, $fallbackValue, $originalValue, $isValid, $expectedValue)
119119
{
120120
$input = $this->input;
121121
$input->setContinueIfEmpty(true);
122122

123+
$input->setRequired($required);
123124
$input->setValidatorChain($this->createValidatorChainMock($isValid));
124125
$input->setFallbackValue($fallbackValue);
125126
$input->setValue($originalValue);
@@ -137,13 +138,14 @@ public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue,
137138
/**
138139
* @dataProvider fallbackValueVsIsValidProvider
139140
*/
140-
public function testFallbackValueVsIsValidRulesWhenValueNotSet($fallbackValue, $originalValue, $isValid)
141+
public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallbackValue, $originalValue, $isValid)
141142
{
142143
$expectedValue = $fallbackValue; // Should always return the fallback value
143144

144145
$input = $this->input;
145146
$input->setContinueIfEmpty(true);
146147

148+
$input->setRequired($required);
147149
$input->setValidatorChain($this->createValidatorChainMock($isValid));
148150
$input->setFallbackValue($fallbackValue);
149151

@@ -931,6 +933,7 @@ public function testResetValueReturnsInputValueToDefaultValue($value)
931933

932934
public function fallbackValueVsIsValidProvider()
933935
{
936+
$required = true;
934937
$isValid=true;
935938

936939
$originalValue = 'fooValue';
@@ -939,8 +942,10 @@ public function fallbackValueVsIsValidProvider()
939942
// @codingStandardsIgnoreStart
940943
return [
941944
// Description => [$inputIsRequired, $fallbackValue, $originalValue, $isValid, $expectedValue]
942-
'Input: Invalid. getValue: fallback' => [$fallbackValue, $originalValue, !$isValid, $fallbackValue],
943-
'Input: Valid. getValue: original' => [$fallbackValue, $originalValue, $isValid, $originalValue],
945+
'Required: T, Input: Invalid. getValue: fallback' => [ $required, $fallbackValue, $originalValue, !$isValid, $fallbackValue],
946+
'Required: T, Input: Valid. getValue: original' => [ $required, $fallbackValue, $originalValue, $isValid, $originalValue],
947+
'Required: F, Input: Invalid. getValue: fallback' => [!$required, $fallbackValue, $originalValue, !$isValid, $fallbackValue],
948+
'Required: F, Input: Valid. getValue: original' => [!$required, $fallbackValue, $originalValue, $isValid, $originalValue],
944949
];
945950
// @codingStandardsIgnoreEnd
946951
}

0 commit comments

Comments
 (0)