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

Commit

Permalink
Merge branch 'superdweebie-rand-bugfix'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
"zendframework/zend-validator": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"require-dev": {
"zendframework/zend-servicemanager": "self.version",
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
},
"suggest": {
"zendframework/zend-servicemanager": "To support plugin manager support"
},
Expand All @@ -31,10 +37,5 @@
"psr-4": {
"ZendTest\\InputFilter\\": "test/"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
}
}
4 changes: 2 additions & 2 deletions src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function isValid($context = null)
foreach ($values as $value) {
$result = $validator->isValid($value, $context);
if (!$result) {
if ($fallbackValue = $this->getFallbackValue()) {
$this->setValue($fallbackValue);
if ($this->hasFallback()) {
$this->setValue($this->getFallbackValue());
$result = true;
}
break;
Expand Down
26 changes: 23 additions & 3 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class Input implements InputInterface, EmptyContextInterface
*/
protected $fallbackValue;

/**
* @var bool
*/
protected $hasFallback = false;

public function __construct($name = null)
{
$this->name = $name;
Expand Down Expand Up @@ -173,6 +178,7 @@ public function setValue($value)
public function setFallbackValue($value)
{
$this->fallbackValue = $value;
$this->hasFallback = true;
return $this;
}

Expand Down Expand Up @@ -271,6 +277,20 @@ public function getFallbackValue()
return $this->fallbackValue;
}

/**
* @return bool
*/
public function hasFallback()
{
return $this->hasFallback;
}

public function clearFallbackValue()
{
$this->hasFallback = false;
$this->fallbackValue = null;
}

/**
* @param InputInterface $input
* @return Input
Expand Down Expand Up @@ -308,8 +328,8 @@ public function isValid($context = null)
$validator = $this->getValidatorChain();
$value = $this->getValue();
$result = $validator->isValid($value, $context);
if (!$result && $fallbackValue = $this->getFallbackValue()) {
$this->setValue($fallbackValue);
if (!$result && $this->hasFallback()) {
$this->setValue($this->getFallbackValue());
$result = true;
}

Expand All @@ -325,7 +345,7 @@ public function getMessages()
return (array) $this->errorMessage;
}

if ($this->getFallbackValue()) {
if ($this->hasFallback()) {
return array();
}

Expand Down
33 changes: 33 additions & 0 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,37 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
$this->assertEquals(2, count($validators));
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
}

public function dataFallbackValue()
{
return array(
array(
'fallbackValue' => array()
),
array(
'fallbackValue' => array(''),
),
array(
'fallbackValue' => array(null),
),
array(
'fallbackValue' => array('some value'),
),
);
}

/**
* @dataProvider dataFallbackValue
*/
public function testFallbackValue($fallbackValue)
{
$this->input->setFallbackValue($fallbackValue);
$validator = new Validator\Date();
$this->input->getValidatorChain()->attach($validator);
$this->input->setValue(array('123')); // not a date

$this->assertTrue($this->input->isValid());
$this->assertEmpty($this->input->getMessages());
$this->assertSame($fallbackValue, $this->input->getValue());
}
}
5 changes: 5 additions & 0 deletions test/FileInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,9 @@ public function testMerge()
$filters = $filterChain->getFilters()->toArray();
$this->assertInstanceOf('Zend\Filter\StringTrim', $filters[0]);
}

public function testFallbackValue($fallbackValue = null)
{
$this->markTestSkipped('Not use fallback value');
}
}
30 changes: 30 additions & 0 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,36 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
}

public function dataFallbackValue()
{
return array(
array(
'fallbackValue' => null
),
array(
'fallbackValue' => ''
),
array(
'fallbackValue' => 'some value'
),
);
}

/**
* @dataProvider dataFallbackValue
*/
public function testFallbackValue($fallbackValue)
{
$this->input->setFallbackValue($fallbackValue);
$validator = new Validator\Date();
$this->input->getValidatorChain()->attach($validator);
$this->input->setValue('123'); // not a date

$this->assertTrue($this->input->isValid());
$this->assertEmpty($this->input->getMessages());
$this->assertSame($fallbackValue, $this->input->getValue());
}

public function testMergeRetainsContinueIfEmptyFlag()
{
$input = new Input('foo');
Expand Down

0 comments on commit 3623840

Please sign in to comment.