From 7c34b4a6284067051da8f37fe7de8bd02dcf6c3a Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Wed, 17 Jan 2018 13:44:30 +0100 Subject: [PATCH] Entity: fixed enumeration checking for nullable properties --- src/LeanMapper/Entity.php | 2 +- tests/LeanMapper/Entity.enum.3.phpt | 44 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/LeanMapper/Entity.enum.3.phpt diff --git a/src/LeanMapper/Entity.php b/src/LeanMapper/Entity.php index 1e411f9..bd07f50 100644 --- a/src/LeanMapper/Entity.php +++ b/src/LeanMapper/Entity.php @@ -677,7 +677,7 @@ protected function set($property, $value) if ($value !== null) { settype($value, $property->getType()); } - if ($property->containsEnumeration() and !$property->isValueFromEnum($value)) { + if ($value !== null and $property->containsEnumeration() and !$property->isValueFromEnum($value)) { throw new InvalidValueException( "Given value is not from possible values enumeration in property '{$property->getName()}' in entity " . get_called_class() . '.' ); diff --git a/tests/LeanMapper/Entity.enum.3.phpt b/tests/LeanMapper/Entity.enum.3.phpt new file mode 100644 index 0000000..ba62b33 --- /dev/null +++ b/tests/LeanMapper/Entity.enum.3.phpt @@ -0,0 +1,44 @@ +state = Author::STATE_ACTIVE; +Assert::equal(Author::STATE_ACTIVE, $author->state); + +$author->state = NULL; +Assert::equal(NULL, $author->state); + +$author->finalState = Author::STATE_DELETED; +Assert::equal(Author::STATE_DELETED, $author->finalState); + +Assert::exception(function () use ($author) { + + $author->finalState = NULL; + +}, 'LeanMapper\Exception\InvalidValueException', "Property 'finalState' in entity Author cannot be null.");