From cbedb187af8fd3db39db10f4391ee8c7f84f6e58 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 1 Sep 2015 20:51:09 +0200 Subject: [PATCH 1/5] Use setValueProvider instead emptyValuesProvider where possible --- test/ArrayInputTest.php | 20 ++++++++++++++++++++ test/InputTest.php | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 3314b15a..86d060e9 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -232,4 +232,24 @@ public function fallbackValueVsIsValidProvider() return $dataSets; } + + public function emptyValueProvider() + { + $dataSets = parent::emptyValueProvider(); + array_walk($dataSets, function (&$set) { + $set[0] = [$set[0]]; // 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; + } } diff --git a/test/InputTest.php b/test/InputTest.php index 0e282ff0..aa1961f3 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -764,7 +764,7 @@ public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsA } /** - * @dataProvider emptyValuesProvider + * @dataProvider setValueProvider */ public function testSetValuePutInputInTheDesiredState($value) { @@ -776,7 +776,7 @@ public function testSetValuePutInputInTheDesiredState($value) } /** - * @dataProvider emptyValuesProvider + * @dataProvider setValueProvider */ public function testResetValueReturnsInputValueToDefaultValue($value) { From 095e011a9e910cb33b7481984f16aa704866db1f Mon Sep 17 00:00:00 2001 From: Maks3w Date: Sun, 30 Aug 2015 14:15:28 +0200 Subject: [PATCH 2/5] Remove tests supersede by group 7448 All these test cases are duplicated with 7448 --- test/ArrayInputTest.php | 25 ------------- test/FileInputTest.php | 61 ------------------------------- test/InputTest.php | 81 ----------------------------------------- 3 files changed, 167 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 86d060e9..bfbd522d 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -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']); @@ -194,15 +178,6 @@ public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain() $this->assertEquals($notEmptyMock, $validators[1]['instance']); } - public function emptyValuesProvider() - { - return [ - [[null]], - [['']], - [[[]]], - ]; - } - public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid() { $this->input->setValue(['nonempty']) diff --git a/test/FileInputTest.php b/test/FileInputTest.php index c0604d71..9af26b65 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -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'); @@ -421,51 +405,6 @@ public function testIsEmptyFileMultiFileOk() $this->assertFalse($this->input->isEmptyFile($rawValue)); } - public function emptyValuesProvider() - { - // Provide empty values specific for file input - return [ - ['file'], - [[ - 'tmp_name' => '', - 'error' => \UPLOAD_ERR_NO_FILE, - ]], - [[[ - 'tmp_name' => 'foo', - 'error' => \UPLOAD_ERR_NO_FILE - ]]], - ]; - } - - /** - * @dataProvider emptyValuesProvider - */ - public function testAllowEmptyOptionSet($emptyValue) - { - // 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); - } - - /** - * @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() { $this->markTestSkipped('does not apply to FileInput'); diff --git a/test/InputTest.php b/test/InputTest.php index aa1961f3..4cf4deae 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -213,22 +213,6 @@ public function testCanRetrieveRawValue() $this->assertEquals('bar', $this->input->getRawValue()); } - public function testIsValidReturnsFalseIfValidationChainFails() - { - $this->input->setValue('bar'); - $validator = new Validator\Digits(); - $this->input->getValidatorChain()->attach($validator); - $this->assertFalse($this->input->isValid()); - } - - public function testIsValidReturnsTrueIfValidationChainSucceeds() - { - $this->input->setValue('123'); - $validator = new Validator\Digits(); - $this->input->getValidatorChain()->attach($validator); - $this->assertTrue($this->input->isValid()); - } - public function testValidationOperatesOnFilteredValue() { $this->input->setValue(' 123 '); @@ -309,71 +293,6 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists() $this->assertEquals($notEmptyMock, $validators[0]['instance']); } - public function emptyValuesProvider() - { - return [ - [null], - [''], - [[]], - ]; - } - - /** - * @dataProvider emptyValuesProvider - */ - public function testValidatorSkippedIfValueIsEmptyAndAllowedAndNotContinue($emptyValue) - { - $validator = function () { - return false; - }; - $this->input->setAllowEmpty(true) - ->setContinueIfEmpty(false) - ->setValue($emptyValue) - ->getValidatorChain()->attach(new Validator\Callback($validator)); - - $this->assertTrue($this->input->isValid()); - } - - /** - * @dataProvider emptyValuesProvider - */ - public function testAllowEmptyOptionSet($emptyValue) - { - $this->input->setAllowEmpty(true); - $this->input->setValue($emptyValue); - $this->assertTrue($this->input->isValid()); - } - - /** - * @dataProvider emptyValuesProvider - */ - public function testAllowEmptyOptionNotSet($emptyValue) - { - $this->input->setAllowEmpty(false); - $this->input->setValue($emptyValue); - $this->assertFalse($this->input->isValid()); - } - - /** - * @dataProvider emptyValuesProvider - */ - public function testValidatorInvokedIfValueIsEmptyAndAllowedAndContinue($emptyValue) - { - $message = 'failure by explicit validator'; - $validator = new Validator\Callback(function ($value) { - return false; - }); - $validator->setMessage($message); - $this->input->setAllowEmpty(true) - ->setContinueIfEmpty(true) - ->setValue($emptyValue) - ->getValidatorChain()->attach($validator); - $this->assertFalse($this->input->isValid()); - // Test reason for validation failure; ensures that failure was not - // caused by accidentally injected NotEmpty validator - $this->assertEquals(['callbackValue' => $message], $this->input->getMessages()); - } - public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid() { $this->input->setValue('nonempty') From ebb0663e0f786001ae73db428e1b95601f96c872 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Sun, 30 Aug 2015 14:15:49 +0200 Subject: [PATCH 3/5] Refactor tests for group 7448 as a data set matrix --- test/ArrayInputTest.php | 2 +- test/FileInputTest.php | 51 +++++++++++++++ test/InputTest.php | 133 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 182 insertions(+), 4 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index bfbd522d..444205b2 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -212,7 +212,7 @@ public function emptyValueProvider() { $dataSets = parent::emptyValueProvider(); array_walk($dataSets, function (&$set) { - $set[0] = [$set[0]]; // Wrap value into an array. + $set['raw'] = [$set['raw']]; // Wrap value into an array. }); return $dataSets; diff --git a/test/FileInputTest.php b/test/FileInputTest.php index 9af26b65..f901153c 100644 --- a/test/FileInputTest.php +++ b/test/FileInputTest.php @@ -414,4 +414,55 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid() { $this->markTestSkipped('does not apply to FileInput'); } + + + 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 / 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 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, + ], + ], + ]; + } } diff --git a/test/InputTest.php b/test/InputTest.php index 4cf4deae..06b84837 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -382,6 +382,35 @@ public function testMergeRetainsAllowEmptyFlag() $this->assertTrue($input2->allowEmpty()); } + /** + * @group 7448 + * @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider + */ + public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid( + $required, + $allowEmpty, + $continueIfEmpty, + $validator, + $value, + $expectedIsValid, + $expectedMessages + ) { + $this->input->setRequired($required); + $this->input->setAllowEmpty($allowEmpty); + $this->input->setContinueIfEmpty($continueIfEmpty); + $this->input->getValidatorChain() + ->attach($validator) + ; + $this->input->setValue($value); + + $this->assertEquals( + $expectedIsValid, + $this->input->isValid(), + 'isValid() value not match. Detail: ' . json_encode($this->input->getMessages()) + ); + $this->assertEquals($expectedMessages, $this->input->getMessages(), 'getMessages() value not match'); + } + public function whenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun() { $validator = new Validator\Callback(function ($value) { @@ -740,17 +769,82 @@ public function setValueProvider() return $values; } + public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider() + { + $emptyValues = $this->emptyValueProvider(); + + $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); + }; + $validatorValid = function ($value, $context = null) { + return $this->createValidatorMock(true, $value, $context); + }; + + // @codingStandardsIgnoreStart + $dataTemplates=[ + // Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validator, [$values], $expectedIsValid, $expectedMessages] + 'Required: T; AEmpty: T; CIEmpty: T; Validator: T' => [ $isRequired, $aEmpty, $cIEmpty, $validatorValid , $emptyValues, $isValid, []], + 'Required: T; AEmpty: T; CIEmpty: T; Validator: F' => [ $isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $emptyValues, !$isValid, $validatorMsg], + 'Required: T; AEmpty: T; CIEmpty: F; Validator: X' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues, $isValid, []], + 'Required: T; AEmpty: F; CIEmpty: T; Validator: T' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorValid , $emptyValues, $isValid, []], + 'Required: T; AEmpty: F; CIEmpty: T; Validator: F' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $emptyValues, !$isValid, $validatorMsg], + 'Required: T; AEmpty: F; CIEmpty: F; Validator: X' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues, !$isValid, $notEmptyMsg], + 'Required: F; AEmpty: T; CIEmpty: T; Validator: T' => [!$isRequired, $aEmpty, $cIEmpty, $validatorValid , $emptyValues, $isValid, []], + 'Required: F; AEmpty: T; CIEmpty: T; Validator: F' => [!$isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $emptyValues, !$isValid, $validatorMsg], + 'Required: F; AEmpty: T; CIEmpty: F; Validator: X' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues, $isValid, []], + 'Required: F; AEmpty: F; CIEmpty: T; Validator: T' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorValid , $emptyValues, $isValid, []], + 'Required: F; AEmpty: F; CIEmpty: T; Validator: F' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $emptyValues, !$isValid, $validatorMsg], + 'Required: F; AEmpty: F; CIEmpty: F; Validator: X' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues, $isValid, []], + ]; + // @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; + } + } + + return $dataSets; + } + public function emptyValueProvider() { return [ // Description => [$value] - 'null' => [null], - '""' => [''], + 'null' => [ + 'raw' => null, + 'filtered' => null, + ], + '""' => [ + 'raw' => '', + 'filtered' => '', + ], // '"0"' => ['0'], // '0' => [0], // '0.0' => [0.0], // 'false' => [false], - '[]' => [[]], + '[]' => [ + 'raw' => [], + 'filtered' => [], + ], ]; } @@ -793,4 +887,37 @@ protected function createValidatorChainMock($isValid = null) return $validatorChain; } + + /** + * @param null|bool $isValid + * @param mixed $value + * @param mixed $context + * @param string[] $messages + * + * @return Validator\ValidatorInterface|MockObject + */ + protected function createValidatorMock($isValid, $value, $context = null, $messages = []) + { + /** @var Validator\ValidatorInterface|MockObject $validator */ + $validator = $this->getMock(Validator\ValidatorInterface::class); + + if (($isValid === false) || ($isValid === true)) { + $validator->expects($this->once()) + ->method('isValid') + ->with($value, $context) + ->willReturn($isValid) + ; + } else { + $validator->expects($this->never()) + ->method('isValid') + ->with($value, $context) + ; + } + + $validator->method('getMessages') + ->willReturn($messages) + ; + + return $validator; + } } From 4e7068b319a1dfda2fdb2133ea7ac0c3b1d13aec Mon Sep 17 00:00:00 2001 From: Maks3w Date: Sun, 30 Aug 2015 15:19:54 +0200 Subject: [PATCH 4/5] Fix ArrayInput when Required: F; AEmpty: F; CIEmpty: F; VChain: NotCall --- src/ArrayInput.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ArrayInput.php b/src/ArrayInput.php index ef513fe0..5f41546e 100644 --- a/src/ArrayInput.php +++ b/src/ArrayInput.php @@ -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; } From 3c811f527a7b53b4e777244a7a3b1cb696747ac7 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Sun, 30 Aug 2015 14:39:00 +0200 Subject: [PATCH 5/5] Remove old tests --- test/ArrayInputTest.php | 10 -- test/InputTest.php | 311 ---------------------------------------- 2 files changed, 321 deletions(-) diff --git a/test/ArrayInputTest.php b/test/ArrayInputTest.php index 444205b2..f0ec6e34 100644 --- a/test/ArrayInputTest.php +++ b/test/ArrayInputTest.php @@ -73,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']); diff --git a/test/InputTest.php b/test/InputTest.php index 06b84837..3f98fe63 100644 --- a/test/InputTest.php +++ b/test/InputTest.php @@ -11,7 +11,6 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit_Framework_TestCase as TestCase; -use RuntimeException; use stdClass; use Zend\Filter; use Zend\InputFilter\Input; @@ -223,16 +222,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'); @@ -411,306 +400,6 @@ public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid( $this->assertEquals($expectedMessages, $this->input->getMessages(), 'getMessages() value not match'); } - public function whenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun() - { - $validator = new Validator\Callback(function ($value) { - throw new RuntimeException('Validator executed when it should not be'); - }); - - $requiredFirst = new Input('foo'); - $requiredFirst->setRequired(true) - ->setAllowEmpty(true) - ->setContinueIfEmpty(false) - ->getValidatorChain()->attach($validator); - - return [ - 'required-first-null' => [$requiredFirst, null], - 'required-first-empty' => [$requiredFirst, ''], - 'required-first-array' => [$requiredFirst, []], - ]; - } - - /** - * @group 7448 - * @dataProvider whenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun - */ - public function testWhenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun(Input $input, $value) - { - $input->setValue($value); - $this->assertTrue($input->isValid()); - } - - public function whenRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun() - { - $alwaysInvalid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return false; - }); - - $emptyIsValid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return true; - }); - - $requiredFirstInvalid = new Input('foo'); - $requiredFirstInvalid->getValidatorChain()->attach($alwaysInvalid); - $requiredFirstValid = new Input('foo'); - $requiredFirstValid->getValidatorChain()->attach($emptyIsValid); - foreach ([$requiredFirstValid, $requiredFirstInvalid] as $input) { - $input->setRequired(true) - ->setAllowEmpty(true) - ->setContinueIfEmpty(true); - } - - return [ - 'required-first-null-valid' => [$requiredFirstValid, null, 'assertTrue'], - 'required-first-null-invalid' => [$requiredFirstInvalid, null, 'assertFalse'], - 'required-first-empty-valid' => [$requiredFirstValid, '', 'assertTrue'], - 'required-first-empty-invalid' => [$requiredFirstInvalid, '', 'assertFalse'], - 'required-first-array-valid' => [$requiredFirstValid, [], 'assertTrue'], - 'required-first-array-invalid' => [$requiredFirstInvalid, [], 'assertFalse'], - ]; - } - - /** - * @group 7448 - * @dataProvider whenRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun - */ - public function testWhenRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun(Input $input, $value, $assertion) - { - $input->setValue($value); - $this->{$assertion}($input->isValid()); - } - - public function whenRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun() - { - $validator = new Validator\Callback(function ($value) { - throw new RuntimeException('Validator executed when it should not be'); - }); - - $requiredFirst = new Input('foo'); - $requiredFirst->setRequired(true) - ->setAllowEmpty(false) - ->setContinueIfEmpty(false) - ->getValidatorChain()->attach($validator); - - return [ - 'required-first-null' => [$requiredFirst, null], - 'required-first-empty' => [$requiredFirst, ''], - 'required-first-array' => [$requiredFirst, []], - ]; - } - - /** - * @group 7448 - * @dataProvider whenRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun - */ - public function testWhenRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun(Input $input, $value) - { - $input->setValue($value); - $this->assertFalse($input->isValid()); - } - - public function whenRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun() - { - $alwaysInvalid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return false; - }); - - $emptyIsValid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return true; - }); - - $requiredFirstInvalid = new Input('foo'); - $requiredFirstInvalid->getValidatorChain()->attach($alwaysInvalid); - $requiredFirstValid = new Input('foo'); - $requiredFirstValid->getValidatorChain()->attach($emptyIsValid); - foreach ([$requiredFirstValid, $requiredFirstInvalid] as $input) { - $input->setRequired(true) - ->setAllowEmpty(false) - ->setContinueIfEmpty(true); - } - - return [ - 'required-first-null-valid' => [$requiredFirstValid, null, 'assertTrue'], - 'required-first-null-invalid' => [$requiredFirstInvalid, null, 'assertFalse'], - 'required-first-empty-valid' => [$requiredFirstValid, '', 'assertTrue'], - 'required-first-empty-invalid' => [$requiredFirstInvalid, '', 'assertFalse'], - 'required-first-array-valid' => [$requiredFirstValid, [], 'assertTrue'], - 'required-first-array-invalid' => [$requiredFirstInvalid, [], 'assertFalse'], - ]; - } - - /** - * @group 7448 - * @dataProvider whenRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun - */ - public function testWhenRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun(Input $input, $value, $assertion) - { - $input->setValue($value); - $this->{$assertion}($input->isValid()); - } - - public function whenNotRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun() - { - $validator = new Validator\Callback(function ($value) { - throw new RuntimeException('Validator executed when it should not be'); - }); - - $requiredFirst = new Input('foo'); - $requiredFirst->setRequired(false) - ->setAllowEmpty(true) - ->setContinueIfEmpty(false) - ->getValidatorChain()->attach($validator); - - return [ - 'required-first-null' => [$requiredFirst, null], - 'required-first-empty' => [$requiredFirst, ''], - 'required-first-array' => [$requiredFirst, []], - ]; - } - - /** - * @group 7448 - * @dataProvider whenNotRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun - */ - public function testWhenNotRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun(Input $input, $value) - { - $input->setValue($value); - $this->assertTrue($input->isValid()); - } - - public function whenNotRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun() - { - $validator = new Validator\Callback(function ($value) { - throw new RuntimeException('Validator executed when it should not be'); - }); - - $requiredFirst = new Input('foo'); - $requiredFirst->setRequired(false) - ->setAllowEmpty(false) - ->setContinueIfEmpty(false) - ->getValidatorChain()->attach($validator); - - return [ - 'required-first-null' => [$requiredFirst, null], - 'required-first-empty' => [$requiredFirst, ''], - 'required-first-array' => [$requiredFirst, []], - ]; - } - - /** - * @group 7448 - * @dataProvider whenNotRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun - */ - public function testWhenNotRequiredAndNotAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun(Input $input, $value) - { - $input->setValue($value); - $this->assertTrue($input->isValid()); - } - - public function whenNotRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun() - { - $alwaysInvalid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return false; - }); - - $emptyIsValid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return true; - }); - - $requiredFirstInvalid = new Input('foo'); - $requiredFirstInvalid->getValidatorChain()->attach($alwaysInvalid); - $requiredFirstValid = new Input('foo'); - $requiredFirstValid->getValidatorChain()->attach($emptyIsValid); - foreach ([$requiredFirstValid, $requiredFirstInvalid] as $input) { - $input->setRequired(false) - ->setAllowEmpty(true) - ->setContinueIfEmpty(true); - } - - return [ - 'required-first-null-valid' => [$requiredFirstValid, null, 'assertTrue'], - 'required-first-null-invalid' => [$requiredFirstInvalid, null, 'assertFalse'], - 'required-first-empty-valid' => [$requiredFirstValid, '', 'assertTrue'], - 'required-first-empty-invalid' => [$requiredFirstInvalid, '', 'assertFalse'], - 'required-first-array-valid' => [$requiredFirstValid, [], 'assertTrue'], - 'required-first-array-invalid' => [$requiredFirstInvalid, [], 'assertFalse'], - ]; - } - - /** - * @group 7448 - * @dataProvider whenNotRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun - */ - public function testWhenNotRequiredAndAllowEmptyAndContinueIfEmptyValidatorsAreRun(Input $input, $value, $assertion) - { - $input->setValue($value); - $this->{$assertion}($input->isValid()); - } - - public function whenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun() - { - $alwaysInvalid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return false; - }); - - $emptyIsValid = new Validator\Callback(function ($value) { - if (! empty($value)) { - throw new RuntimeException('Unexpected non-empty value provided to validate'); - } - return true; - }); - - $requiredFirstInvalid = new Input('foo'); - $requiredFirstInvalid->getValidatorChain()->attach($alwaysInvalid); - $requiredFirstValid = new Input('foo'); - $requiredFirstValid->getValidatorChain()->attach($emptyIsValid); - foreach ([$requiredFirstValid, $requiredFirstInvalid] as $input) { - $input->setRequired(false) - ->setAllowEmpty(false) - ->setContinueIfEmpty(true); - } - - return [ - 'required-first-null-valid' => [$requiredFirstValid, null, 'assertTrue'], - 'required-first-null-invalid' => [$requiredFirstInvalid, null, 'assertFalse'], - 'required-first-empty-valid' => [$requiredFirstValid, '', 'assertTrue'], - 'required-first-empty-invalid' => [$requiredFirstInvalid, '', 'assertFalse'], - 'required-first-array-valid' => [$requiredFirstValid, [], 'assertTrue'], - 'required-first-array-invalid' => [$requiredFirstInvalid, [], 'assertFalse'], - ]; - } - - /** - * @group 7448 - * @dataProvider whenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun - */ - public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsAreRun(Input $input, $value, $assertion) - { - $input->setValue($value); - $this->{$assertion}($input->isValid()); - } - /** * @dataProvider setValueProvider */