From b59b249f12bd1500c0859e21a51ac8dd57489248 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 19 Aug 2013 13:36:27 -0500 Subject: [PATCH] Revert "Revert "Merge branch 'superdweebie-rand-bugfix'"" This reverts commit b0ae4689135d62555cf9bbe55cdd7dddcd2d8f05 in order to allow forward-porting fixes to develop. Conflicts: library/Zend/ModuleManager/ModuleEvent.php --- composer.json | 11 ++++++----- src/ArrayInput.php | 4 ++-- src/Input.php | 26 +++++++++++++++++++++++--- test/ArrayInputTest.php | 33 +++++++++++++++++++++++++++++++++ test/FileInputTest.php | 5 +++++ test/InputTest.php | 30 ++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index f662e78a..0d3e04dd 100644 --- a/composer.json +++ b/composer.json @@ -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" }, @@ -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" } } \ No newline at end of file diff --git a/src/ArrayInput.php b/src/ArrayInput.php index 5dec0151..3c5c4783 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -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; diff --git a/src/Input.php b/src/Input.php index da84df9e..7d302e8f 100644 --- a/src/Input.php +++ b/src/Input.php @@ -70,6 +70,11 @@ class Input implements InputInterface, EmptyContextInterface */ protected $fallbackValue; + /** + * @var bool + */ + protected $hasFallback = false; + public function __construct($name = null) { $this->name = $name; @@ -173,6 +178,7 @@ public function setValue($value) public function setFallbackValue($value) { $this->fallbackValue = $value; + $this->hasFallback = true; return $this; } @@ -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 @@ -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; } @@ -325,7 +345,7 @@ public function getMessages() return (array) $this->errorMessage; } - if ($this->getFallbackValue()) { + if ($this->hasFallback()) { return array(); } diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index fb7403ef..a0e17275 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -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()); + } } diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 22d18c40..a72a7987 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -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'); + } } diff --git a/test/InputTest.php b/test/InputTest.php index f3638ffc..24338dd6 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -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');