Skip to content

Commit

Permalink
Merge pull request #7 from andreypostal/remove-dead-code
Browse files Browse the repository at this point in the history
Remove dead code and improve testing
  • Loading branch information
andreypostal authored Aug 20, 2024
2 parents 6aabdc6 + f5a5e54 commit c9acdc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/JsonSerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ trait JsonSerializerTrait
public function serialize(object $obj): array
{
$class = new ReflectionClass($obj);
if ($class->isEnum()) {
return $obj->value;
}

$skipAttributeCheck = ($class->getAttributes(JsonObjectAttribute::class)[0] ?? null) !== null;
$output = [];
Expand Down
28 changes: 28 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,34 @@ public function testArrayEnum(): void
JsonHandler::Encode($arr);
}

/**
* @throws JsonException
* @throws ReflectionException
*/
public function testArrayOfChild(): void
{
$obj = new WithArrayOfChildObject();

$handler = new JsonHandler();
$arr = $handler->serialize($obj);

$this->assertArrayHasKey('id', $arr);
$this->assertArrayHasKey('children', $arr);
$this->assertIsArray($arr['children']);
$this->assertCount(2, $arr['children']);
$this->assertIsArray($arr['children'][0]);

$this->assertArrayHasKey('string', $arr['children'][1]);
$this->assertArrayHasKey('int', $arr['children'][1]);
$this->assertIsInt($arr['children'][1]['int']);
$this->assertEquals(11, $arr['children'][1]['int']);

$this->assertIsFloat($arr['children'][1]['float']);
$this->assertEquals(11.50, $arr['children'][1]['float']);

JsonHandler::Encode($arr);
}

/**
* @throws JsonException
* @throws ReflectionException
Expand Down

0 comments on commit c9acdc5

Please sign in to comment.