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

Refactor tests for group 7448 as a data set matrix #41

Merged
merged 5 commits into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ public function isValid($context = null)
$result = true;
foreach ($values as $value) {
$empty = ($value === null || $value === '' || $value === []);
if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
if ($empty && !$this->isRequired() && !$this->continueIfEmpty()) {
$result = true;
continue;
}
if ($empty && $this->isRequired() && $this->allowEmpty() && !$this->continueIfEmpty()) {
$result = true;
continue;
}
Expand Down
55 changes: 20 additions & 35 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@ public function testCanRetrieveRawValue()
$this->assertEquals(['bar'], $this->input->getRawValue());
}

public function testIsValidReturnsFalseIfValidationChainFails()
{
$this->input->setValue(['123', 'bar']);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertFalse($this->input->isValid());
}

public function testIsValidReturnsTrueIfValidationChainSucceeds()
{
$this->input->setValue(['123', '123']);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertTrue($this->input->isValid());
}

public function testValidationOperatesOnFilteredValue()
{
$this->input->setValue([' 123 ', ' 123']);
Expand All @@ -89,16 +73,6 @@ public function testValidationOperatesOnFilteredValue()
$this->assertTrue($this->input->isValid());
}

public function testGetMessagesReturnsValidationMessages()
{
$this->input->setValue(['bar']);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertFalse($this->input->isValid());
$messages = $this->input->getMessages();
$this->assertArrayHasKey(Validator\Digits::NOT_DIGITS, $messages);
}

public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
{
$this->input->setValue(['bar']);
Expand Down Expand Up @@ -194,15 +168,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
}

public function emptyValuesProvider()
{
return [
[[null]],
[['']],
[[[]]],
];
}

public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
{
$this->input->setValue(['nonempty'])
Expand Down Expand Up @@ -232,4 +197,24 @@ public function fallbackValueVsIsValidProvider()

return $dataSets;
}

public function emptyValueProvider()
{
$dataSets = parent::emptyValueProvider();
array_walk($dataSets, function (&$set) {
$set['raw'] = [$set['raw']]; // Wrap value into an array.
});

return $dataSets;
}

public function mixedValueProvider()
{
$dataSets = parent::mixedValueProvider();
array_walk($dataSets, function (&$set) {
$set[0] = [$set[0]]; // Wrap value into an array.
});

return $dataSets;
}
}
106 changes: 48 additions & 58 deletions test/FileInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,6 @@ public function testCanRetrieveRawValue()
$this->assertEquals($value, $this->input->getRawValue());
}

public function testIsValidReturnsFalseIfValidationChainFails()
{
$this->input->setValue(['tmp_name' => 'bar']);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertFalse($this->input->isValid());
}

public function testIsValidReturnsTrueIfValidationChainSucceeds()
{
$this->input->setValue(['tmp_name' => 'bar']);
$validator = new Validator\NotEmpty();
$this->input->getValidatorChain()->attach($validator);
$this->assertTrue($this->input->isValid());
}

public function testValidationOperatesOnFilteredValue()
{
$this->markTestSkipped('Test is not enabled in FileInputTest');
Expand Down Expand Up @@ -421,58 +405,64 @@ public function testIsEmptyFileMultiFileOk()
$this->assertFalse($this->input->isEmptyFile($rawValue));
}

public function emptyValuesProvider()
public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
{
// Provide empty values specific for file input
return [
['file'],
[[
'tmp_name' => '',
'error' => \UPLOAD_ERR_NO_FILE,
]],
[[[
'tmp_name' => 'foo',
'error' => \UPLOAD_ERR_NO_FILE
]]],
];
$this->markTestSkipped('does not apply to FileInput');
}

/**
* @dataProvider emptyValuesProvider
*/
public function testAllowEmptyOptionSet($emptyValue)
public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
{
// UploadFile validator is disabled, pretend one
$validator = new Validator\Callback(function () {
return false; // This should never be called
});
$this->input->getValidatorChain()->attach($validator);
parent::testAllowEmptyOptionSet($emptyValue);
$this->markTestSkipped('does not apply to FileInput');
}

/**
* @dataProvider emptyValuesProvider
*/
public function testAllowEmptyOptionNotSet($emptyValue)
{
// UploadFile validator is disabled, pretend one
$message = 'pretend failing UploadFile validator';
$validator = new Validator\Callback(function () {
return false;
});
$validator->setMessage($message);
$this->input->getValidatorChain()->attach($validator);
parent::testAllowEmptyOptionNotSet($emptyValue);
$this->assertEquals(['callbackValue' => $message], $this->input->getMessages());
}

public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider()
{
$this->markTestSkipped('does not apply to FileInput');
$dataSets = parent::isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider();

// FileInput do not use NotEmpty validator so the only validator present in the chain is the custom one.
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / tmp_name']);
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / single']);
unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X / multi']);

return $dataSets;
}

public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
public function emptyValueProvider()
{
$this->markTestSkipped('does not apply to FileInput');
return [
'tmp_name' => [
'raw' => 'file',
'filtered' => [
'tmp_name' => 'file',
'name' => 'file',
'size' => 0,
'type' => '',
'error' => UPLOAD_ERR_NO_FILE,
],
],
'single' => [
'raw' => [
'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE,
],
'filtered' => [
'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE,
],
],
'multi' => [
'raw' => [
[
'tmp_name' => 'foo',
'error' => UPLOAD_ERR_NO_FILE,
],
],
'filtered' => [
'tmp_name' => 'foo',
'error' => UPLOAD_ERR_NO_FILE,
],
],
];
}
}
Loading