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

Commit 67c68c8

Browse files
committed
Merge branch 'hotfix/41'
Close #41
2 parents 8936631 + 04f08a8 commit 67c68c8

File tree

4 files changed

+200
-486
lines changed

4 files changed

+200
-486
lines changed

src/ArrayInput.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public function isValid($context = null)
8282
$result = true;
8383
foreach ($values as $value) {
8484
$empty = ($value === null || $value === '' || $value === []);
85-
if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
85+
if ($empty && !$this->isRequired() && !$this->continueIfEmpty()) {
86+
$result = true;
87+
continue;
88+
}
89+
if ($empty && $this->isRequired() && $this->allowEmpty() && !$this->continueIfEmpty()) {
8690
$result = true;
8791
continue;
8892
}

test/ArrayInputTest.php

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ public function testCanRetrieveRawValue()
6363
$this->assertEquals(['bar'], $this->input->getRawValue());
6464
}
6565

66-
public function testIsValidReturnsFalseIfValidationChainFails()
67-
{
68-
$this->input->setValue(['123', 'bar']);
69-
$validator = new Validator\Digits();
70-
$this->input->getValidatorChain()->attach($validator);
71-
$this->assertFalse($this->input->isValid());
72-
}
73-
74-
public function testIsValidReturnsTrueIfValidationChainSucceeds()
75-
{
76-
$this->input->setValue(['123', '123']);
77-
$validator = new Validator\Digits();
78-
$this->input->getValidatorChain()->attach($validator);
79-
$this->assertTrue($this->input->isValid());
80-
}
81-
8266
public function testValidationOperatesOnFilteredValue()
8367
{
8468
$this->input->setValue([' 123 ', ' 123']);
@@ -89,16 +73,6 @@ public function testValidationOperatesOnFilteredValue()
8973
$this->assertTrue($this->input->isValid());
9074
}
9175

92-
public function testGetMessagesReturnsValidationMessages()
93-
{
94-
$this->input->setValue(['bar']);
95-
$validator = new Validator\Digits();
96-
$this->input->getValidatorChain()->attach($validator);
97-
$this->assertFalse($this->input->isValid());
98-
$messages = $this->input->getMessages();
99-
$this->assertArrayHasKey(Validator\Digits::NOT_DIGITS, $messages);
100-
}
101-
10276
public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
10377
{
10478
$this->input->setValue(['bar']);
@@ -194,15 +168,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
194168
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
195169
}
196170

197-
public function emptyValuesProvider()
198-
{
199-
return [
200-
[[null]],
201-
[['']],
202-
[[[]]],
203-
];
204-
}
205-
206171
public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
207172
{
208173
$this->input->setValue(['nonempty'])
@@ -232,4 +197,24 @@ public function fallbackValueVsIsValidProvider()
232197

233198
return $dataSets;
234199
}
200+
201+
public function emptyValueProvider()
202+
{
203+
$dataSets = parent::emptyValueProvider();
204+
array_walk($dataSets, function (&$set) {
205+
$set['raw'] = [$set['raw']]; // Wrap value into an array.
206+
});
207+
208+
return $dataSets;
209+
}
210+
211+
public function mixedValueProvider()
212+
{
213+
$dataSets = parent::mixedValueProvider();
214+
array_walk($dataSets, function (&$set) {
215+
$set[0] = [$set[0]]; // Wrap value into an array.
216+
});
217+
218+
return $dataSets;
219+
}
235220
}

test/FileInputTest.php

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,6 @@ public function testCanRetrieveRawValue()
113113
$this->assertEquals($value, $this->input->getRawValue());
114114
}
115115

116-
public function testIsValidReturnsFalseIfValidationChainFails()
117-
{
118-
$this->input->setValue(['tmp_name' => 'bar']);
119-
$validator = new Validator\Digits();
120-
$this->input->getValidatorChain()->attach($validator);
121-
$this->assertFalse($this->input->isValid());
122-
}
123-
124-
public function testIsValidReturnsTrueIfValidationChainSucceeds()
125-
{
126-
$this->input->setValue(['tmp_name' => 'bar']);
127-
$validator = new Validator\NotEmpty();
128-
$this->input->getValidatorChain()->attach($validator);
129-
$this->assertTrue($this->input->isValid());
130-
}
131-
132116
public function testValidationOperatesOnFilteredValue()
133117
{
134118
$this->markTestSkipped('Test is not enabled in FileInputTest');
@@ -421,58 +405,64 @@ public function testIsEmptyFileMultiFileOk()
421405
$this->assertFalse($this->input->isEmptyFile($rawValue));
422406
}
423407

424-
public function emptyValuesProvider()
408+
public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
425409
{
426-
// Provide empty values specific for file input
427-
return [
428-
['file'],
429-
[[
430-
'tmp_name' => '',
431-
'error' => \UPLOAD_ERR_NO_FILE,
432-
]],
433-
[[[
434-
'tmp_name' => 'foo',
435-
'error' => \UPLOAD_ERR_NO_FILE
436-
]]],
437-
];
410+
$this->markTestSkipped('does not apply to FileInput');
438411
}
439412

440-
/**
441-
* @dataProvider emptyValuesProvider
442-
*/
443-
public function testAllowEmptyOptionSet($emptyValue)
413+
public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
444414
{
445-
// UploadFile validator is disabled, pretend one
446-
$validator = new Validator\Callback(function () {
447-
return false; // This should never be called
448-
});
449-
$this->input->getValidatorChain()->attach($validator);
450-
parent::testAllowEmptyOptionSet($emptyValue);
415+
$this->markTestSkipped('does not apply to FileInput');
451416
}
452417

453-
/**
454-
* @dataProvider emptyValuesProvider
455-
*/
456-
public function testAllowEmptyOptionNotSet($emptyValue)
457-
{
458-
// UploadFile validator is disabled, pretend one
459-
$message = 'pretend failing UploadFile validator';
460-
$validator = new Validator\Callback(function () {
461-
return false;
462-
});
463-
$validator->setMessage($message);
464-
$this->input->getValidatorChain()->attach($validator);
465-
parent::testAllowEmptyOptionNotSet($emptyValue);
466-
$this->assertEquals(['callbackValue' => $message], $this->input->getMessages());
467-
}
468418

469-
public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
419+
public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider()
470420
{
471-
$this->markTestSkipped('does not apply to FileInput');
421+
$dataSets = parent::isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider();
422+
423+
// FileInput do not use NotEmpty validator so the only validator present in the chain is the custom one.
424+
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / tmp_name']);
425+
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / single']);
426+
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / multi']);
427+
428+
return $dataSets;
472429
}
473430

474-
public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
431+
public function emptyValueProvider()
475432
{
476-
$this->markTestSkipped('does not apply to FileInput');
433+
return [
434+
'tmp_name' => [
435+
'raw' => 'file',
436+
'filtered' => [
437+
'tmp_name' => 'file',
438+
'name' => 'file',
439+
'size' => 0,
440+
'type' => '',
441+
'error' => UPLOAD_ERR_NO_FILE,
442+
],
443+
],
444+
'single' => [
445+
'raw' => [
446+
'tmp_name' => '',
447+
'error' => UPLOAD_ERR_NO_FILE,
448+
],
449+
'filtered' => [
450+
'tmp_name' => '',
451+
'error' => UPLOAD_ERR_NO_FILE,
452+
],
453+
],
454+
'multi' => [
455+
'raw' => [
456+
[
457+
'tmp_name' => 'foo',
458+
'error' => UPLOAD_ERR_NO_FILE,
459+
],
460+
],
461+
'filtered' => [
462+
'tmp_name' => 'foo',
463+
'error' => UPLOAD_ERR_NO_FILE,
464+
],
465+
],
466+
];
477467
}
478468
}

0 commit comments

Comments
 (0)