From 3a6e1f573c3cf11584a617d72c845a4791a1d052 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 5 Aug 2020 14:28:48 -0300 Subject: [PATCH 1/2] refactor: changing the validation for field filled We should handle differently string and array. --- src/Node/BaseNode.php | 7 ++++++- tests/Node/BaseNodeTest.php | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Node/BaseNode.php b/src/Node/BaseNode.php index 580c50e..04aaf84 100644 --- a/src/Node/BaseNode.php +++ b/src/Node/BaseNode.php @@ -275,12 +275,17 @@ public function walk($input) protected function checkConstraints(string $field, $value): void { foreach ($this->constraints as $constraint) { - if (!$constraint->validate($value) && ($this->isRequired() || !empty($value))) { + if (!$constraint->validate($value) && ($this->isRequired() || $this->checkIfFieldValueIsSpecified($value))) { throw new InvalidConstraintException($constraint->getErrorMessage($field)); } } } + private function checkIfFieldValueIsSpecified($value): bool + { + return ($this->type === 'string' || $this->type === 'array' ? !empty($value) : !is_null($value)); + } + private function setHandler(InputHandler $handler, string $type): self { $handler->setRootType($type); diff --git a/tests/Node/BaseNodeTest.php b/tests/Node/BaseNodeTest.php index 0e7887c..7e50db7 100644 --- a/tests/Node/BaseNodeTest.php +++ b/tests/Node/BaseNodeTest.php @@ -5,6 +5,7 @@ namespace Linio\Component\Input\Node; use Linio\Component\Input\Constraint\NotNull; +use Linio\Component\Input\Constraint\Range; use Linio\Component\Input\Constraint\StringSize; use Linio\Component\Input\Exception\InvalidConstraintException; use Linio\Component\Input\Transformer\DateTimeTransformer; @@ -142,6 +143,27 @@ public function testNotRequiredWithConstraints(): void ->addConstraint(new StringSize(1, 255)); $this->assertNull($child->getValue('foobar', null)); - $this->assertEmpty($child->getValue('foobar', '')); + } + + public function testNotRequiredWithContraintsAndIntegerField(): void + { + $typeHandler = $this->prophesize(TypeHandler::class); + $typeHandler->getType('integer')->willReturn(new BaseNode()); + + $base = new BaseNode(); + $base->setTypeHandler($typeHandler->reveal()); + $base->setTypeAlias('integer'); + $child = $base->add('foobar', 'integer') + ->setTypeAlias('integer') + ->setRequired(false) + ->addConstraint(new Range(1, 255)) + ->setType('integer'); + + + $this->assertEmpty($child->getValue('foobar', null)); + + $this->expectException(InvalidConstraintException::class); + $child->getValue('foobar', 0); + } } From 9b4f98d53811932b475c8698f6be2263b45ce5de Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 5 Aug 2020 14:31:04 -0300 Subject: [PATCH 2/2] fix lint --- src/Node/BaseNode.php | 2 +- tests/Node/BaseNodeTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Node/BaseNode.php b/src/Node/BaseNode.php index 04aaf84..2e05f1a 100644 --- a/src/Node/BaseNode.php +++ b/src/Node/BaseNode.php @@ -283,7 +283,7 @@ protected function checkConstraints(string $field, $value): void private function checkIfFieldValueIsSpecified($value): bool { - return ($this->type === 'string' || $this->type === 'array' ? !empty($value) : !is_null($value)); + return $this->type === 'string' || $this->type === 'array' ? !empty($value) : $value !== null; } private function setHandler(InputHandler $handler, string $type): self diff --git a/tests/Node/BaseNodeTest.php b/tests/Node/BaseNodeTest.php index 7e50db7..b238e4e 100644 --- a/tests/Node/BaseNodeTest.php +++ b/tests/Node/BaseNodeTest.php @@ -159,11 +159,9 @@ public function testNotRequiredWithContraintsAndIntegerField(): void ->addConstraint(new Range(1, 255)) ->setType('integer'); - $this->assertEmpty($child->getValue('foobar', null)); $this->expectException(InvalidConstraintException::class); $child->getValue('foobar', 0); - } }