Skip to content

Commit

Permalink
fix: nested objects with custom classes (#12)
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
g105b authored Sep 14, 2022
1 parent f7c45b3 commit f384434
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions test/phpunit/DataObjectBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

use Gt\DataObject\AssociativeArrayWithinObjectException;
use Gt\DataObject\DataObjectBuilder;
use Gt\DataObject\DataObject;
use Gt\DataObject\Json\JsonArrayData;
use Gt\DataObject\Json\JsonPrimitiveData;
use Gt\DataObject\ObjectWithinAssociativeArrayException;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -42,6 +39,28 @@ public function testFromObjectNested() {
self::assertEquals("value4", $nestedOutput->getString("key4"));
}

public function fromObjectNestedWithCustomClass() {
$obj = new StdClass();
$obj->key1 = "value1";
$obj->key2 = "value2";
$obj->nested = new StdClass();
$obj->nested->deepNested = new StdClass();
$obj->nested->key3 = "value3";
$obj->nested->key4 = "value4";

$myClass = new class extends StdClass {
};

$sut = new DataObjectBuilder();
$output = $sut->fromObject($obj, $myClass);

$nestedOutput = $output->get("nested");
$deepNestedOutput = $nestedOutput->getObject("deepNested");
self::assertIsObject($nestedOutput);
self::assertInstanceOf($myClass, $nestedOutput);
self::assertInstanceOf($myClass, $deepNestedOutput);
}

public function testFromObjectNestedArray() {
$obj = new StdClass();
$obj->key1 = "value1";
Expand Down Expand Up @@ -163,4 +182,4 @@ public function testMixingObjectInAssociativeArrayThrowsError() {
self::expectException(ObjectWithinAssociativeArrayException::class);
$sut->fromAssociativeArray($array);
}
}
}

0 comments on commit f384434

Please sign in to comment.