Skip to content

Commit

Permalink
Test the same value if it already exist in property
Browse files Browse the repository at this point in the history
  • Loading branch information
roukmoute committed Oct 14, 2019
1 parent 3bf5395 commit 2451a20
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/DtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ private function assetMappings(SortedMap $getterSetterMapping, $instance): void

$this->callGetter($fieldName, $pair->getter(), $instance, $newObject);
} elseif ($pair->isImmutable()) {
$newObject = $this->createObject($fieldName, (string) $pair->getter()->getReturnType());
ReflectionClass::createFromInstance($instance)
->getProperty($fieldName)
->setValue($instance, $newObject)
;
$reflectionProperty = ReflectionClass::createFromInstance($instance)->getProperty($fieldName);
if (($newObject = $reflectionProperty->getValue($instance)) === null) {
$newObject = $this->createObject($fieldName, (string) $pair->getter()->getReturnType());
$reflectionProperty->setValue($instance, $newObject);
}

$this->callGetter($fieldName, $pair->getter(), $instance, $newObject);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPUnit/Constructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace PHPUnit;

class Constructor
{
/** @var string */
private $string;

/** @var int */
private $int;

/** @var bool */
private $bool;

public function __construct(string $string, int $int, bool $bool)
{
$this->string = $string;
$this->int = $int;
$this->bool = $bool;
}

public function string(): string
{
return $this->string;
}

public function int(): int
{
return $this->int;
}

public function isBool(): bool
{
return $this->bool;
}
}
15 changes: 15 additions & 0 deletions tests/PHPUnit/ContructorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PHPUnit;

use DtoTester\DtoTest;

class ContructorTest extends DtoTest
{
protected function getInstance()
{
return new Constructor('string', 42, false);
}
}

0 comments on commit 2451a20

Please sign in to comment.