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

Commit 651e553

Browse files
committed
Input fallback feature should work when required and optional
1 parent 49942f9 commit 651e553

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

src/ArrayInput.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public function isValid($context = null)
5454
$required = $this->isRequired();
5555
$hasFallback = $this->hasFallback();
5656

57-
if (! $hasValue && $required && !$hasFallback) {
58-
$this->setErrorMessage('Value is required');
59-
return false;
60-
}
61-
62-
if (! $hasValue && $required && $hasFallback) {
57+
if (! $hasValue && $hasFallback) {
6358
$this->setValue($this->getFallbackValue());
6459
return true;
6560
}
6661

62+
if (! $hasValue && $required) {
63+
$this->setErrorMessage('Value is required');
64+
return false;
65+
}
66+
6767
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
6868
$this->injectNotEmptyValidator();
6969
}
@@ -80,7 +80,7 @@ public function isValid($context = null)
8080
if (!$result) {
8181
if ($hasFallback) {
8282
$this->setValue($this->getFallbackValue());
83-
$result = true;
83+
return true;
8484
}
8585
break;
8686
}

src/Input.php

+6-6
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

+14-7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function testArrayInputSetFallbackValue($fallbackValue)
7474
}
7575

7676
public function testFallbackValueVsIsValidRules(
77+
$required = null,
7778
$fallbackValue = null,
7879
$originalValue = null,
7980
$isValid = null,
@@ -87,12 +88,18 @@ public function testFallbackValueVsIsValidRules(
8788
*
8889
* @dataProvider fallbackValueVsIsValidProvider
8990
*/
90-
public function testArrayInputFallbackValueVsIsValidRules($fallbackValue, $originalValue, $isValid, $expectedValue)
91-
{
92-
parent::testFallbackValueVsIsValidRules([$fallbackValue], [$originalValue], $isValid, [$expectedValue]);
91+
public function testArrayInputFallbackValueVsIsValidRules(
92+
$required,
93+
$fallbackValue,
94+
$originalValue,
95+
$isValid,
96+
$expectedValue
97+
) {
98+
parent::testFallbackValueVsIsValidRules($required, [$fallbackValue], [$originalValue], $isValid, [$expectedValue]);
9399
}
94100

95101
public function testFallbackValueVsIsValidRulesWhenValueNotSet(
102+
$required = null,
96103
$fallbackValue = null,
97104
$originalValue = null,
98105
$isValid = null,
@@ -107,16 +114,16 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet(
107114
* @dataProvider fallbackValueVsIsValidProvider
108115
*/
109116
public function testArrayInputFallbackValueVsIsValidRulesWhenValueNotSet(
117+
$required,
110118
$fallbackValue,
111119
$originalValue,
112-
$isValid,
113-
$expectedValue
120+
$isValid
114121
) {
115122
parent::testFallbackValueVsIsValidRulesWhenValueNotSet(
123+
$required,
116124
[$fallbackValue],
117125
[$originalValue],
118-
$isValid,
119-
[$expectedValue]
126+
$isValid
120127
);
121128
}
122129

test/FileInputTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ public function testSetFallbackValue($fallbackValue = null)
317317
}
318318

319319
public function testFallbackValueVsIsValidRules(
320+
$required = null,
320321
$fallbackValue = null,
321322
$originalValue = null,
322323
$isValid = null,
@@ -327,6 +328,7 @@ public function testFallbackValueVsIsValidRules(
327328

328329

329330
public function testFallbackValueVsIsValidRulesWhenValueNotSet(
331+
$required = null,
330332
$fallbackValue = null,
331333
$originalValue = null,
332334
$isValid = null,

test/InputTest.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,31 @@ public function testSetFallbackValue($fallbackValue)
114114

115115
public function fallbackValueVsIsValidProvider()
116116
{
117+
$required = true;
118+
117119
$originalValue = 'fooValue';
118120
$fallbackValue = 'fooFallbackValue';
119121

122+
// @codingStandardsIgnoreStart
120123
return [
121-
// Description => [$fallbackValue, $originalValue, $isValid, $expectedValue]
122-
'Input is invalid getValue return fallback value' => [$fallbackValue, $originalValue, false, $fallbackValue],
123-
'Input is valid getValue return original value' => [$fallbackValue, $originalValue, true, $originalValue],
124+
// Description => [$inputIsRequired, $fallbackValue, $originalValue, $isValid, $expectedValue]
125+
'Required: T, Input: Invalid. getValue: fallback' => [$required, $fallbackValue, $originalValue, false, $fallbackValue],
126+
'Required: T, Input: Valid. getValue: original' => [$required, $fallbackValue, $originalValue, true, $originalValue],
127+
'Required: F, Input: Invalid. getValue: fallback' => [!$required, $fallbackValue, $originalValue, false, $fallbackValue],
128+
'Required: F, Input: Valid. getValue: original' => [!$required, $fallbackValue, $originalValue, true, $originalValue],
124129
];
130+
// @codingStandardsIgnoreEnd
125131
}
126132

127133
/**
128134
* @dataProvider fallbackValueVsIsValidProvider
129135
*/
130-
public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue, $isValid, $expectedValue)
136+
public function testFallbackValueVsIsValidRules($required, $fallbackValue, $originalValue, $isValid, $expectedValue)
131137
{
132138
$input = $this->input;
133139
$input->setContinueIfEmpty(true);
134140

141+
$input->setRequired($required);
135142
$input->setValidatorChain($this->createValidatorChainMock($isValid));
136143
$input->setFallbackValue($fallbackValue);
137144
$input->setValue($originalValue);
@@ -149,13 +156,14 @@ public function testFallbackValueVsIsValidRules($fallbackValue, $originalValue,
149156
/**
150157
* @dataProvider fallbackValueVsIsValidProvider
151158
*/
152-
public function testFallbackValueVsIsValidRulesWhenValueNotSet($fallbackValue, $originalValue, $isValid)
159+
public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallbackValue, $originalValue, $isValid)
153160
{
154161
$expectedValue = $fallbackValue; // Should always return the fallback value
155162

156163
$input = $this->input;
157164
$input->setContinueIfEmpty(true);
158165

166+
$input->setRequired($required);
159167
$input->setValidatorChain($this->createValidatorChainMock($isValid));
160168
$input->setFallbackValue($fallbackValue);
161169

0 commit comments

Comments
 (0)