Skip to content

Commit

Permalink
entity: hasValue() should not set null value for uninitialized property
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and hrach committed Apr 15, 2021
1 parent d62a96f commit c44ea71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Entity/ImmutableDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function &internalGetValue(PropertyMetadata $metadata, string $name)
private function internalHasValue(PropertyMetadata $metadata, string $name): bool
{
if (!isset($this->validated[$name])) {
$this->initProperty($metadata, $name);
$this->initProperty($metadata, $name, false);
}

if ($this->data[$name] instanceof IPropertyContainer) {
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/integration/Entity/entity.hasValue().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
namespace NextrasTests\Orm\Integration\Entity;


use Nextras\Orm\Entity\AbstractEntity;
use Nextras\Orm\Entity\Reflection\EntityMetadata;
use Nextras\Orm\Model\MetadataStorage;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Ean;
use Tester\Assert;


Expand All @@ -28,6 +32,22 @@ class EntityHasValueTest extends DataTestCase
Assert::false($author->hasValue('name'));
Assert::true($author->hasValue('web'));
Assert::true($author->hasValue('age'));

// avoid default ean constructor not to set the default type
$ean = new class extends Ean {
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct()
{
AbstractEntity::__construct();
}


protected function createMetadata(): EntityMetadata
{
return MetadataStorage::get(Ean::class);
}
};
Assert::false($ean->hasValue('type'));
}

}
Expand Down
2 changes: 1 addition & 1 deletion tests/inc/model/ean/Ean.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @property Book $book {1:1 Book::$ean}
* @property EanType $type {wrapper TestEnumPropertyWrapper}
*/
final class Ean extends Entity
class Ean extends Entity
{
public function __construct(EanType $type = null)
{
Expand Down

0 comments on commit c44ea71

Please sign in to comment.