diff --git a/src/ArrayInput.php b/src/ArrayInput.php index d23b9d81..1f4f1f6d 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -76,22 +76,10 @@ public function isValid($context = null) return false; } - if (!$this->continueIfEmpty() && !$this->allowEmpty()) { - $this->injectNotEmptyValidator(); - } $validator = $this->getValidatorChain(); $values = $this->getValue(); $result = true; foreach ($values as $value) { - $empty = ($value === null || $value === '' || $value === []); - if ($empty && !$this->isRequired() && !$this->continueIfEmpty()) { - $result = true; - continue; - } - if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) { - $result = true; - continue; - } $result = $validator->isValid($value, $context); if (!$result) { if ($hasFallback) { diff --git a/src/EmptyContextInterface.php b/src/EmptyContextInterface.php deleted file mode 100644 index fd049db1..00000000 --- a/src/EmptyContextInterface.php +++ /dev/null @@ -1,31 +0,0 @@ -setRequired($value); break; - case 'allow_empty': - $input->setAllowEmpty($value); - if (!isset($inputSpecification['required'])) { - $input->setRequired(!$value); - } - break; - case 'continue_if_empty': - if (!$input instanceof Input) { - throw new Exception\RuntimeException(sprintf( - '%s "continue_if_empty" can only set to inputs of type "%s"', - __METHOD__, - Input::class - )); - } - $input->setContinueIfEmpty($inputSpecification['continue_if_empty']); - break; case 'error_message': $input->setErrorMessage($value); break; diff --git a/src/FileInput.php b/src/FileInput.php index b195306a..d0bad147 100644 --- a/src/FileInput.php +++ b/src/FileInput.php @@ -9,8 +9,6 @@ namespace Zend\InputFilter; -use Zend\Validator\File\UploadFile as UploadValidator; - /** * FileInput is a special Input type for handling uploaded files. * @@ -32,29 +30,6 @@ class FileInput extends Input */ protected $isValid = false; - /** - * @var bool - */ - protected $autoPrependUploadValidator = true; - - /** - * @param bool $value Enable/Disable automatically prepending an Upload validator - * @return FileInput - */ - public function setAutoPrependUploadValidator($value) - { - $this->autoPrependUploadValidator = $value; - return $this; - } - - /** - * @return bool - */ - public function getAutoPrependUploadValidator() - { - return $this->autoPrependUploadValidator; - } - /** * @return mixed */ @@ -83,29 +58,6 @@ public function getValue() return $value; } - /** - * Checks if the raw input value is an empty file input eg: no file was uploaded - * - * @param $rawValue - * @return bool - */ - public function isEmptyFile($rawValue) - { - if (!is_array($rawValue)) { - return true; - } - - if (isset($rawValue['error']) && $rawValue['error'] === UPLOAD_ERR_NO_FILE) { - return true; - } - - if (count($rawValue) === 1 && isset($rawValue[0])) { - return $this->isEmptyFile($rawValue[0]); - } - - return false; - } - /** * @param mixed $context Extra "context" to provide the validator * @return bool @@ -114,10 +66,7 @@ public function isValid($context = null) { $rawValue = $this->getRawValue(); $hasValue = $this->hasValue(); - $empty = $this->isEmptyFile($rawValue); $required = $this->isRequired(); - $allowEmpty = $this->allowEmpty(); - $continueIfEmpty = $this->continueIfEmpty(); if (! $hasValue && ! $required) { return true; @@ -130,15 +79,6 @@ public function isValid($context = null) return false; } - if ($empty && ! $required && ! $continueIfEmpty) { - return true; - } - - if ($empty && $allowEmpty && ! $continueIfEmpty) { - return true; - } - - $this->injectUploadValidator(); $validator = $this->getValidatorChain(); //$value = $this->getValue(); // Do not run the filters yet for File uploads (see getValue()) @@ -168,53 +108,4 @@ public function isValid($context = null) return $this->isValid; } - - /** - * @return void - */ - protected function injectUploadValidator() - { - if (!$this->autoPrependUploadValidator) { - return; - } - $chain = $this->getValidatorChain(); - - // Check if Upload validator is already first in chain - $validators = $chain->getValidators(); - if (isset($validators[0]['instance']) - && $validators[0]['instance'] instanceof UploadValidator - ) { - $this->autoPrependUploadValidator = false; - return; - } - - $chain->prependByName('fileuploadfile', [], true); - $this->autoPrependUploadValidator = false; - } - - /** - * @deprecated 2.4.8 See note on parent class. Removal does not affect this class. - * - * No-op, NotEmpty validator does not apply for FileInputs. - * See also: BaseInputFilter::isValid() - * - * @return void - */ - protected function injectNotEmptyValidator() - { - $this->notEmptyValidator = true; - } - - /** - * @param InputInterface $input - * @return FileInput - */ - public function merge(InputInterface $input) - { - parent::merge($input); - if ($input instanceof FileInput) { - $this->setAutoPrependUploadValidator($input->getAutoPrependUploadValidator()); - } - return $this; - } } diff --git a/src/Input.php b/src/Input.php index 6342040b..4b909370 100644 --- a/src/Input.php +++ b/src/Input.php @@ -10,28 +10,12 @@ namespace Zend\InputFilter; use Zend\Filter\FilterChain; -use Zend\ServiceManager\AbstractPluginManager; use Zend\Validator\NotEmpty; use Zend\Validator\ValidatorChain; class Input implements - InputInterface, - EmptyContextInterface + InputInterface { - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @var bool - */ - protected $allowEmpty = false; - - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @var bool - */ - protected $continueIfEmpty = false; - /** * @var bool */ @@ -52,13 +36,6 @@ class Input implements */ protected $name; - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @var bool - */ - protected $notEmptyValidator = false; - /** * @var bool */ @@ -96,18 +73,6 @@ public function __construct($name = null) $this->name = $name; } - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain and set this to `true`. - * - * @param bool $allowEmpty - * @return Input - */ - public function setAllowEmpty($allowEmpty) - { - $this->allowEmpty = (bool) $allowEmpty; - return $this; - } - /** * @param bool $breakOnFailure * @return Input @@ -118,18 +83,6 @@ public function setBreakOnFailure($breakOnFailure) return $this; } - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain and set this to `true`. - * - * @param bool $continueIfEmpty - * @return Input - */ - public function setContinueIfEmpty($continueIfEmpty) - { - $this->continueIfEmpty = (bool) $continueIfEmpty; - return $this; - } - /** * @param string|null $errorMessage * @return Input @@ -225,16 +178,6 @@ public function setFallbackValue($value) return $this; } - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @return bool - */ - public function allowEmpty() - { - return $this->allowEmpty; - } - /** * @return bool */ @@ -243,16 +186,6 @@ public function breakOnFailure() return $this->breakOnFailure; } - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. Should always return `true`. - * - * @return bool - */ - public function continueIfEmpty() - { - return $this->continueIfEmpty; - } - /** * @return string|null */ @@ -361,13 +294,9 @@ public function clearFallbackValue() public function merge(InputInterface $input) { $this->setBreakOnFailure($input->breakOnFailure()); - if ($input instanceof Input) { - $this->setContinueIfEmpty($input->continueIfEmpty()); - } $this->setErrorMessage($input->getErrorMessage()); $this->setName($input->getName()); $this->setRequired($input->isRequired()); - $this->setAllowEmpty($input->allowEmpty()); if (!($input instanceof Input) || $input->hasValue()) { $this->setValue($input->getRawValue()); } @@ -388,10 +317,7 @@ public function isValid($context = null) { $value = $this->getValue(); $hasValue = $this->hasValue(); - $empty = ($value === null || $value === '' || $value === []); $required = $this->isRequired(); - $allowEmpty = $this->allowEmpty(); - $continueIfEmpty = $this->continueIfEmpty(); if (! $hasValue && $this->hasFallback()) { $this->setValue($this->getFallbackValue()); @@ -409,22 +335,6 @@ public function isValid($context = null) return false; } - if ($empty && ! $required && ! $continueIfEmpty) { - return true; - } - - if ($empty && $allowEmpty && ! $continueIfEmpty) { - return true; - } - - // At this point, we need to run validators. - // If we do not allow empty and the "continue if empty" flag are - // BOTH false, we inject the "not empty" validator into the chain, - // which adds that logic into the validation routine. - if (! $allowEmpty && ! $continueIfEmpty) { - $this->injectNotEmptyValidator(); - } - $validator = $this->getValidatorChain(); $result = $validator->isValid($value, $context); if (! $result && $this->hasFallback()) { @@ -452,38 +362,6 @@ public function getMessages() return $validator->getMessages(); } - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @return void - */ - protected function injectNotEmptyValidator() - { - if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) { - return; - } - $chain = $this->getValidatorChain(); - - // Check if NotEmpty validator is already in chain - $validators = $chain->getValidators(); - foreach ($validators as $validator) { - if ($validator['instance'] instanceof NotEmpty) { - $this->notEmptyValidator = true; - return; - } - } - - $this->notEmptyValidator = true; - - if (class_exists(AbstractPluginManager::class)) { - $chain->prependByName('NotEmpty', [], true); - - return; - } - - $chain->prependValidator(new NotEmpty(), true); - } - /** * Create and return the validation failure message for required input. * diff --git a/src/InputInterface.php b/src/InputInterface.php index cde375fa..48da0fe8 100644 --- a/src/InputInterface.php +++ b/src/InputInterface.php @@ -14,14 +14,6 @@ interface InputInterface { - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain and set this to `true`. - * - * @param bool $allowEmpty - * @return self - */ - public function setAllowEmpty($allowEmpty); - /** * @param bool $breakOnFailure * @return self @@ -70,13 +62,6 @@ public function setValue($value); */ public function merge(InputInterface $input); - /** - * @deprecated 2.4.8 Add Zend\Validator\NotEmpty validator to the ValidatorChain. - * - * @return bool - */ - public function allowEmpty(); - /** * @return bool */ diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index ae542b1d..e9391f58 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -48,26 +48,6 @@ 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['raw'] = [$set['raw']]; // Wrap value into an array. - }); - - return $dataSets; - } - protected function createFilterChainMock(array $valueMap = []) { // ArrayInput filters per each array value @@ -103,16 +83,6 @@ function ($values) { return parent::createValidatorChainMock($valueMap, $messages); } - protected function createNonEmptyValidatorMock($isValid, $value, $context = null) - { - // ArrayInput validates per each array value - if (is_array($value)) { - $value = current($value); - } - - return parent::createNonEmptyValidatorMock($isValid, $value, $context); - } - protected function getDummyValue($raw = true) { return [parent::getDummyValue($raw)]; diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 41cd1bda..20810f9b 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -196,7 +196,6 @@ public function inputTypeSpecificationProvider() { return [ // Description => [$specificationKey] - 'continue_if_empty' => ['continue_if_empty'], 'fallback_value' => ['fallback_value'], ]; } @@ -426,31 +425,6 @@ public function testFactoryWillCreateInputWithSuggestedValidators() } } - public function testFactoryWillCreateInputWithSuggestedRequiredFlagAndAlternativeAllowEmptyFlag() - { - $factory = $this->createDefaultFactory(); - $input = $factory->createInput([ - 'name' => 'foo', - 'required' => false, - 'allow_empty' => false, - ]); - $this->assertInstanceOf(InputInterface::class, $input); - $this->assertFalse($input->isRequired()); - $this->assertFalse($input->allowEmpty()); - } - - public function testFactoryWillCreateInputWithSuggestedAllowEmptyFlagAndImpliesRequiredFlag() - { - $factory = $this->createDefaultFactory(); - $input = $factory->createInput([ - 'name' => 'foo', - 'allow_empty' => true, - ]); - $this->assertInstanceOf(InputInterface::class, $input); - $this->assertTrue($input->allowEmpty()); - $this->assertFalse($input->isRequired()); - } - public function testFactoryWillCreateInputWithSuggestedName() { $factory = $this->createDefaultFactory(); @@ -461,17 +435,6 @@ public function testFactoryWillCreateInputWithSuggestedName() $this->assertEquals('foo', $input->getName()); } - public function testFactoryWillCreateInputWithContinueIfEmptyFlag() - { - $factory = $this->createDefaultFactory(); - $input = $factory->createInput([ - 'name' => 'foo', - 'continue_if_empty' => true, - ]); - $this->assertInstanceOf(InputInterface::class, $input); - $this->assertTrue($input->continueIfEmpty()); - } - public function testFactoryAcceptsInputInterface() { $factory = $this->createDefaultFactory(); @@ -521,7 +484,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi ], ], 'bar' => [ - 'allow_empty' => true, 'filters' => [ [ 'name' => 'string_trim', @@ -553,7 +515,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi ], ], 'bar' => [ - 'allow_empty' => true, 'filters' => [ [ 'name' => 'string_trim', @@ -573,7 +534,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi ], 'zomg' => [ 'name' => 'zomg', - 'continue_if_empty' => true, ], ]); $this->assertInstanceOf(InputFilter::class, $inputFilter); @@ -590,7 +550,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi break; case 'bar': $this->assertInstanceOf(Input::class, $input); - $this->assertTrue($input->allowEmpty()); $this->assertEquals(2, count($input->getFilterChain())); break; case 'baz': @@ -602,7 +561,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi $this->assertEquals(2, count($foo->getValidatorChain())); $bar = $input->get('bar'); $this->assertInstanceOf(Input::class, $bar); - $this->assertTrue($bar->allowEmpty()); $this->assertEquals(2, count($bar->getFilterChain())); break; case 'bat': @@ -611,7 +569,6 @@ public function testFactoryWillCreateInputFilterAndAllInputObjectsFromGivenConfi break; case 'zomg': $this->assertInstanceOf(Input::class, $input); - $this->assertTrue($input->continueIfEmpty()); } } } diff --git a/test/FileInputTest.php b/test/FileInputTest.php index d6106fd1..9d465c22 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -9,23 +9,16 @@ namespace ZendTest\InputFilter; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Zend\InputFilter\FileInput; -use Zend\Validator; /** * @covers Zend\InputFilter\FileInput */ class FileInputTest extends InputTest { - /** @var FileInput */ - protected $input; - public function setUp() { $this->input = new FileInput('foo'); - // Upload validator does not work in CLI test environment, disable - $this->input->setAutoPrependUploadValidator(false); } public function testRetrievingValueFiltersTheValue() @@ -111,76 +104,37 @@ public function testValidationOperatesBeforeFiltering() $this->assertEquals($badValue, $this->input->getValue()); } - public function testAutoPrependUploadValidatorIsOnByDefault() - { - $input = new FileInput('foo'); - $this->assertTrue($input->getAutoPrependUploadValidator()); - } - - public function testUploadValidatorIsAddedWhenIsValidIsCalled() - { - $this->input->setAutoPrependUploadValidator(true); - $this->assertTrue($this->input->getAutoPrependUploadValidator()); - $this->assertTrue($this->input->isRequired()); - $this->input->setValue([ - 'tmp_name' => __FILE__, - 'name' => 'foo', - 'size' => 1, - 'error' => 0, - ]); - $validatorChain = $this->input->getValidatorChain(); - $this->assertEquals(0, count($validatorChain->getValidators())); - - $this->assertFalse($this->input->isValid()); - $validators = $validatorChain->getValidators(); - $this->assertEquals(1, count($validators)); - $this->assertInstanceOf(Validator\File\UploadFile::class, $validators[0]['instance']); - } - - public function testUploadValidatorIsNotAddedWhenIsValidIsCalled() - { - $this->assertFalse($this->input->getAutoPrependUploadValidator()); - $this->assertTrue($this->input->isRequired()); - $this->input->setValue(['tmp_name' => 'bar']); - $validatorChain = $this->input->getValidatorChain(); - $this->assertEquals(0, count($validatorChain->getValidators())); - - $this->assertTrue( - $this->input->isValid(), - 'isValid() value not match. Detail . ' . json_encode($this->input->getMessages()) - ); - $this->assertEquals(0, count($validatorChain->getValidators())); - } - - public function testRequiredUploadValidatorValidatorNotAddedWhenOneExists() + public function testCanValidateArrayOfMultiFileData() { - $this->input->setAutoPrependUploadValidator(true); - $this->assertTrue($this->input->getAutoPrependUploadValidator()); - $this->assertTrue($this->input->isRequired()); - $this->input->setValue(['tmp_name' => 'bar']); - - /** @var Validator\File\UploadFile|MockObject $uploadMock */ - $uploadMock = $this->getMock(Validator\File\UploadFile::class, ['isValid']); - $uploadMock->expects($this->exactly(1)) - ->method('isValid') - ->will($this->returnValue(true)); + $values = [ + [ + 'tmp_name' => __FILE__, + 'name' => 'foo', + ], + [ + 'tmp_name' => __FILE__, + 'name' => 'bar', + ], + [ + 'tmp_name' => __FILE__, + 'name' => 'baz', + ], + ]; + $this->input->setValue($values); + $this->input->setValidatorChain($this->createValidatorChainMock([ + [$values[0], null, true], + [$values[1], null, false], + [$values[2], null, true], + ])); - $validatorChain = $this->input->getValidatorChain(); - $validatorChain->prependValidator($uploadMock); - $this->assertTrue( + $this->assertFalse( $this->input->isValid(), 'isValid() value not match. Detail . ' . json_encode($this->input->getMessages()) ); - - $validators = $validatorChain->getValidators(); - $this->assertEquals(1, count($validators)); - $this->assertEquals($uploadMock, $validators[0]['instance']); } public function testValidationsRunWithoutFileArrayDueToAjaxPost() { - $this->input->setAutoPrependUploadValidator(true); - $this->assertTrue($this->input->getAutoPrependUploadValidator()); $this->assertTrue($this->input->isRequired()); $this->input->setValue(''); @@ -195,16 +149,6 @@ public function testValidationsRunWithoutFileArrayDueToAjaxPost() $this->assertFalse($this->input->isValid()); } - public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value = null) - { - $this->markTestSkipped('Test is not enabled in FileInputTest'); - } - - public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value = null) - { - $this->markTestSkipped('Test is not enabled in FileInputTest'); - } - public function testFallbackValueVsIsValidRules( $required = null, $fallbackValue = null, @@ -223,146 +167,6 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet( $this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput'); } - public function testIsEmptyFileNotArray() - { - $rawValue = 'file'; - $this->assertTrue($this->input->isEmptyFile($rawValue)); - } - - public function testIsEmptyFileUploadNoFile() - { - $rawValue = [ - 'tmp_name' => '', - 'error' => \UPLOAD_ERR_NO_FILE, - ]; - $this->assertTrue($this->input->isEmptyFile($rawValue)); - } - - public function testIsEmptyFileOk() - { - $rawValue = [ - 'tmp_name' => 'name', - 'error' => \UPLOAD_ERR_OK, - ]; - $this->assertFalse($this->input->isEmptyFile($rawValue)); - } - - public function testIsEmptyMultiFileUploadNoFile() - { - $rawValue = [[ - 'tmp_name' => 'foo', - 'error' => \UPLOAD_ERR_NO_FILE - ]]; - $this->assertTrue($this->input->isEmptyFile($rawValue)); - } - - public function testIsEmptyFileMultiFileOk() - { - $rawValue = [ - [ - 'tmp_name' => 'foo', - 'error' => \UPLOAD_ERR_OK - ], - [ - 'tmp_name' => 'bar', - 'error' => \UPLOAD_ERR_OK - ], - ]; - $this->assertFalse($this->input->isEmptyFile($rawValue)); - } - - /** - * Specific FileInput::merge extras - */ - public function testFileInputMerge() - { - $source = new FileInput(); - $source->setAutoPrependUploadValidator(true); - - $target = $this->input; - $target->setAutoPrependUploadValidator(false); - - $return = $target->merge($source); - $this->assertSame($target, $return, 'merge() must return it self'); - - $this->assertEquals( - true, - $target->getAutoPrependUploadValidator(), - 'getAutoPrependUploadValidator() value not match' - ); - } - - public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider() - { - $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, Value: Empty / tmp_name']); - unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty / single']); - unset($dataSets['Required: T; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty / multi']); - - return $dataSets; - } - - public function emptyValueProvider() - { - 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, - ], - ], - ]; - } - - public function mixedValueProvider() - { - $fooUploadErrOk = [ - 'tmp_name' => 'foo', - 'error' => UPLOAD_ERR_OK, - ]; - - return [ - 'single' => [ - 'raw' => $fooUploadErrOk, - 'filtered' => $fooUploadErrOk, - ], - 'multi' => [ - 'raw' => [ - $fooUploadErrOk, - ], - 'filtered' => $fooUploadErrOk, - ], - ]; - } - protected function getDummyValue($raw = true) { return ['tmp_name' => 'bar']; diff --git a/test/InputTest.php b/test/InputTest.php index 29e5e424..e5619e27 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -11,13 +11,11 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit_Framework_TestCase as TestCase; -use stdClass; use Zend\Filter\FilterChain; use Zend\InputFilter\Input; use Zend\InputFilter\InputInterface; use Zend\Validator\NotEmpty as NotEmptyValidator; use Zend\Validator\ValidatorChain; -use Zend\Validator\ValidatorInterface; /** * @covers Zend\InputFilter\Input @@ -93,35 +91,9 @@ public function testRequiredFlagIsMutable() $this->assertFalse($this->input->isRequired()); } - public function testInputDoesNotAllowEmptyValuesByDefault() - { - $this->assertFalse($this->input->allowEmpty()); - } - - public function testAllowEmptyFlagIsMutable() - { - $this->input->setAllowEmpty(true); - $this->assertTrue($this->input->allowEmpty()); - } - - public function testContinueIfEmptyFlagIsFalseByDefault() - { - $input = $this->input; - $this->assertFalse($input->continueIfEmpty()); - } - - public function testContinueIfEmptyFlagIsMutable() - { - $input = $this->input; - $input->setContinueIfEmpty(true); - $this->assertTrue($input->continueIfEmpty()); - } - - /** - * @dataProvider setValueProvider - */ - public function testSetFallbackValue($fallbackValue) + public function testSetFallbackValue() { + $fallbackValue = $this->getDummyValue(); $input = $this->input; $return = $input->setFallbackValue($fallbackValue); @@ -137,7 +109,6 @@ public function testSetFallbackValue($fallbackValue) public function testFallbackValueVsIsValidRules($required, $fallbackValue, $originalValue, $isValid, $expectedValue) { $input = $this->input; - $input->setContinueIfEmpty(true); $input->setRequired($required); $input->setValidatorChain($this->createValidatorChainMock([[$originalValue, null, $isValid]])); @@ -162,7 +133,6 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallb $expectedValue = $fallbackValue; // Should always return the fallback value $input = $this->input; - $input->setContinueIfEmpty(true); $input->setRequired($required); $input->setValidatorChain($this->createValidatorChainMock()); @@ -242,13 +212,9 @@ public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid() { $input = $this->input; $input->setRequired(false); - $input->setAllowEmpty(false); - $input->setContinueIfEmpty(true); // Validator should not to be called - $input->getValidatorChain() - ->attach($this->createValidatorMock(null, null)) - ; + $input->setValidatorChain($this->createValidatorChainMock()); $this->assertTrue( $input->isValid(), 'isValid() should be return always true when is not required, and no data is set. Detail: ' . @@ -257,20 +223,6 @@ public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid() $this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid'); } - /** - * @dataProvider emptyValueProvider - */ - public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue($value) - { - $input = $this->input; - $input->setContinueIfEmpty(true); - $input->setValue($value); - $input->isValid(); - $validators = $input->getValidatorChain() - ->getValidators(); - $this->assertEmpty($validators); - } - public function testDefaultGetValue() { $this->assertNull($this->input->getValue()); @@ -318,7 +270,6 @@ public function testValidationOperatesOnFilteredValue() $validatorChain = $this->createValidatorChainMock([[$valueFiltered, null, true]]); - $this->input->setAllowEmpty(true); $this->input->setFilterChain($filterChain); $this->input->setValidatorChain($validatorChain); $this->input->setValue($valueRaw); @@ -340,88 +291,19 @@ public function testBreakOnFailureFlagIsMutable() $this->assertTrue($this->input->breakOnFailure()); } - /** - * @dataProvider emptyValueProvider - */ - public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value) - { - $this->assertTrue($this->input->isRequired()); - $this->input->setValue($value); - $validatorChain = $this->input->getValidatorChain(); - $this->assertEquals(0, count($validatorChain->getValidators())); - - $this->assertFalse($this->input->isValid()); - $messages = $this->input->getMessages(); - $this->assertArrayHasKey('isEmpty', $messages); - $this->assertEquals(1, count($validatorChain->getValidators())); - - // Assert that NotEmpty validator wasn't added again - $this->assertFalse($this->input->isValid()); - $this->assertEquals(1, count($validatorChain->getValidators())); - } - - /** - * @dataProvider emptyValueProvider - */ - public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value) - { - $this->input->setRequired(true); - $this->input->setValue($value); - - $notEmptyMock = $this->createNonEmptyValidatorMock(false, $value); - - $validatorChain = $this->input->getValidatorChain(); - $validatorChain->prependValidator($notEmptyMock); - $this->assertFalse($this->input->isValid()); - - $validators = $validatorChain->getValidators(); - $this->assertEquals(1, count($validators)); - $this->assertEquals($notEmptyMock, $validators[0]['instance']); - } - - /** - * @dataProvider emptyValueProvider - */ - public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain($valueRaw, $valueFiltered) - { - $filterChain = $this->createFilterChainMock([[$valueRaw, $valueFiltered]]); - $validatorChain = $this->input->getValidatorChain(); - - $this->input->setRequired(true); - $this->input->setFilterChain($filterChain); - $this->input->setValue($valueRaw); - - $notEmptyMock = $this->createNonEmptyValidatorMock(false, $valueFiltered); - - $validatorChain->attach($this->createValidatorMock(true)); - $validatorChain->attach($notEmptyMock); - - $this->assertFalse($this->input->isValid()); - - $validators = $validatorChain->getValidators(); - $this->assertEquals(2, count($validators)); - $this->assertEquals($notEmptyMock, $validators[1]['instance']); - } - /** * @group 7448 - * @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider + * @dataProvider isRequiredVsIsValidProvider */ - public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid( + public function testIsRequiredVsIsValid( $required, - $allowEmpty, - $continueIfEmpty, $validator, - $value, $expectedIsValid, $expectedMessages ) { + $value = $this->getDummyValue(); $this->input->setRequired($required); - $this->input->setAllowEmpty($allowEmpty); - $this->input->setContinueIfEmpty($continueIfEmpty); - $this->input->getValidatorChain() - ->attach($validator) - ; + $this->input->setValidatorChain($validator); $this->input->setValue($value); $this->assertEquals( @@ -434,28 +316,22 @@ public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid( $this->assertEquals($value, $this->input->getValue(), 'getValue() must return the filtered value always'); } - /** - * @dataProvider setValueProvider - */ - public function testSetValuePutInputInTheDesiredState($value) + public function testSetValuePutInputInTheDesiredState() { $input = $this->input; $this->assertFalse($input->hasValue(), 'Input should not have value by default'); - $input->setValue($value); + $input->setValue($this->getDummyValue()); $this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set"); } - /** - * @dataProvider setValueProvider - */ - public function testResetValueReturnsInputValueToDefaultValue($value) + public function testResetValueReturnsInputValueToDefaultValue() { $input = $this->input; $originalInput = clone $input; $this->assertFalse($input->hasValue(), 'Input should not have value by default'); - $input->setValue($value); + $input->setValue($this->getDummyValue()); $this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set"); $return = $input->resetValue(); @@ -513,17 +389,14 @@ public function testMerge() public function testInputMergeWithoutValues() { $source = new Input(); - $source->setContinueIfEmpty(true); $this->assertFalse($source->hasValue(), 'Source should not have a value'); $target = $this->input; - $target->setContinueIfEmpty(false); $this->assertFalse($target->hasValue(), 'Target should not have a value'); $return = $target->merge($source); $this->assertSame($target, $return, 'merge() must return it self'); - $this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match'); $this->assertFalse($target->hasValue(), 'hasValue() value not match'); } @@ -533,17 +406,14 @@ public function testInputMergeWithoutValues() public function testInputMergeWithSourceValue() { $source = new Input(); - $source->setContinueIfEmpty(true); $source->setValue(['foo']); $target = $this->input; - $target->setContinueIfEmpty(false); $this->assertFalse($target->hasValue(), 'Target should not have a value'); $return = $target->merge($source); $this->assertSame($target, $return, 'merge() must return it self'); - $this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match'); $this->assertEquals(['foo'], $target->getRawValue(), 'getRawValue() value not match'); $this->assertTrue($target->hasValue(), 'hasValue() value not match'); } @@ -554,17 +424,14 @@ public function testInputMergeWithSourceValue() public function testInputMergeWithTargetValue() { $source = new Input(); - $source->setContinueIfEmpty(true); $this->assertFalse($source->hasValue(), 'Source should not have a value'); $target = $this->input; - $target->setContinueIfEmpty(false); $target->setValue(['foo']); $return = $target->merge($source); $this->assertSame($target, $return, 'merge() must return it self'); - $this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match'); $this->assertEquals(['foo'], $target->getRawValue(), 'getRawValue() value not match'); $this->assertTrue($target->hasValue(), 'hasValue() value not match'); } @@ -588,172 +455,38 @@ public function fallbackValueVsIsValidProvider() // @codingStandardsIgnoreEnd } - public function setValueProvider() - { - $emptyValues = $this->emptyValueProvider(); - $mixedValues = $this->mixedValueProvider(); - - $values = array_merge($emptyValues, $mixedValues); - - return $values; - } - - public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider() + public function isRequiredVsIsValidProvider() { - $allValues = $this->setValueProvider(); - $emptyValues = $this->emptyValueProvider(); - $nonEmptyValues = array_diff_key($allValues, $emptyValues); - $isRequired = true; - $aEmpty = true; - $cIEmpty = true; $isValid = true; $validatorMsg = ['FooValidator' => 'Invalid Value']; - $notEmptyMsg = ['isEmpty' => "Value is required and can't be empty"]; - $validatorNotCall = function ($value, $context = null) { - return $this->createValidatorMock(null, $value, $context); - }; $validatorInvalid = function ($value, $context = null) use ($validatorMsg) { - return $this->createValidatorMock(false, $value, $context, $validatorMsg); + return $this->createValidatorChainMock([[$value, $context, false]], $validatorMsg); }; $validatorValid = function ($value, $context = null) { - return $this->createValidatorMock(true, $value, $context); + return $this->createValidatorChainMock([[$value, $context, true]]); }; // @codingStandardsIgnoreStart - $dataTemplates=[ - // Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validator, [$values], $expectedIsValid, $expectedMessages] - 'Required: T; AEmpty: T; CIEmpty: T; Validator: T' => [ $isRequired, $aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []], - 'Required: T; AEmpty: T; CIEmpty: T; Validator: F' => [ $isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg], - - 'Required: T; AEmpty: T; CIEmpty: F; Validator: X, Value: Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []], - 'Required: T; AEmpty: T; CIEmpty: F; Validator: T, Value: Not Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []], - 'Required: T; AEmpty: T; CIEmpty: F; Validator: F, Value: Not Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg], - - 'Required: T; AEmpty: F; CIEmpty: T; Validator: T' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []], - 'Required: T; AEmpty: F; CIEmpty: T; Validator: F' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg], + $dataSets=[ + // Description => [$isRequired, $validator, $expectedIsValid, $expectedMessages] + 'Required: T; Validator: T' => [ $isRequired, $validatorValid , $isValid, []], + 'Required: T; Validator: F' => [ $isRequired, $validatorInvalid, !$isValid, $validatorMsg], - 'Required: T; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , !$isValid, $notEmptyMsg], - 'Required: T; AEmpty: F; CIEmpty: F; Validator: T, Value: Not Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []], - 'Required: T; AEmpty: F; CIEmpty: F; Validator: F, Value: Not Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg], - - 'Required: F; AEmpty: T; CIEmpty: T; Validator: T' => [!$isRequired, $aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []], - 'Required: F; AEmpty: T; CIEmpty: T; Validator: F' => [!$isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg], - - 'Required: F; AEmpty: T; CIEmpty: F; Validator: X, Value: Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []], - 'Required: F; AEmpty: T; CIEmpty: F; Validator: T, Value: Not Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []], - 'Required: F; AEmpty: T; CIEmpty: F; Validator: F, Value: Not Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg], - - 'Required: F; AEmpty: F; CIEmpty: T; Validator: T' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []], - 'Required: F; AEmpty: F; CIEmpty: T; Validator: F' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg], - - 'Required: F; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []], - 'Required: F; AEmpty: F; CIEmpty: F; Validator: T, Value: Not Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []], - 'Required: F; AEmpty: F; CIEmpty: F; Validator: F, Value: Not Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg], + 'Required: F; Validator: T' => [!$isRequired, $validatorValid , $isValid, []], + 'Required: F; Validator: F' => [!$isRequired, $validatorInvalid, !$isValid, $validatorMsg], ]; // @codingStandardsIgnoreEnd - // Expand data template matrix for each possible input value. - // Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validator, $value, $expectedIsValid] - $dataSets = []; - foreach ($dataTemplates as $dataTemplateDescription => $dataTemplate) { - foreach ($dataTemplate[4] as $valueDescription => $value) { - $tmpTemplate = $dataTemplate; - $tmpTemplate[3] = $dataTemplate[3]($value['filtered']); // Get validator mock for each data set - $tmpTemplate[4] = $value['raw']; // expand value - - $dataSets[$dataTemplateDescription . ' / ' . $valueDescription] = $tmpTemplate; - } + foreach ($dataSets as &$dataSet) { + $dataSet[1] = $dataSet[1]($this->getDummyValue()); // Get validator mock for each data set } return $dataSets; } - public function emptyValueProvider() - { - return [ - // Description => [$value] - 'null' => [ - 'raw' => null, - 'filtered' => null, - ], - '""' => [ - 'raw' => '', - 'filtered' => '', - ], -// '"0"' => ['0'], -// '0' => [0], -// '0.0' => [0.0], -// 'false' => [false], - '[]' => [ - 'raw' => [], - 'filtered' => [], - ], - ]; - } - - public function mixedValueProvider() - { - return [ - // Description => [$value] - '"0"' => [ - 'raw' => '0', - 'filtered' => '0', - ], - '0' => [ - 'raw' => 0, - 'filtered' => 0, - ], - '0.0' => [ - 'raw' => 0.0, - 'filtered' => 0.0, - ], -// TODO enable me -// 'false' => [ -// 'raw' => false, -// 'filtered' => false, -// ], - 'php' => [ - 'raw' => 'php', - 'filtered' => 'php', - ], -// TODO enable me -// 'whitespace' => [ -// 'raw' => ' ', -// 'filtered' => ' ', -// ], - '1' => [ - 'raw' => 1, - 'filtered' => 1, - ], - '1.0' => [ - 'raw' => 1.0, - 'filtered' => 1.0, - ], - 'true' => [ - 'raw' => true, - 'filtered' => true, - ], - '["php"]' => [ - 'raw' => ['php'], - 'filtered' => ['php'], - ], - 'object' => [ - 'raw' => new stdClass(), - 'filtered' => new stdClass(), - ], - // @codingStandardsIgnoreStart -// TODO Skip HHVM failure enable me -// 'callable' => [ -// 'raw' => function () {}, -// 'filtered' => function () {}, -// ], - // @codingStandardsIgnoreEnd - ]; - } - /** * @return InputInterface|MockObject */ @@ -811,60 +544,6 @@ protected function createValidatorChainMock(array $valueMap = [], $messages = [] return $validatorChain; } - /** - * @param null|bool $isValid - * @param mixed $value - * @param mixed $context - * @param string[] $messages - * - * @return ValidatorInterface|MockObject - */ - protected function createValidatorMock($isValid, $value = 'not-set', $context = null, $messages = []) - { - /** @var ValidatorInterface|MockObject $validator */ - $validator = $this->getMock(ValidatorInterface::class); - - if (($isValid === false) || ($isValid === true)) { - $isValidMethod = $validator->expects($this->once()) - ->method('isValid') - ->willReturn($isValid) - ; - } else { - $isValidMethod = $validator->expects($this->never()) - ->method('isValid') - ; - } - if ($value !== 'not-set') { - $isValidMethod->with($value, $context); - } - - $validator->method('getMessages') - ->willReturn($messages) - ; - - return $validator; - } - - /** - * @param bool $isValid - * @param mixed $value - * @param mixed $context - * - * @return NotEmptyValidator|MockObject - */ - protected function createNonEmptyValidatorMock($isValid, $value, $context = null) - { - /** @var NotEmptyValidator|MockObject $notEmptyMock */ - $notEmptyMock = $this->getMock(NotEmptyValidator::class, ['isValid']); - $notEmptyMock->expects($this->once()) - ->method('isValid') - ->with($value, $context) - ->willReturn($isValid) - ; - - return $notEmptyMock; - } - protected function getDummyValue($raw = true) { if ($raw) {