Skip to content

Commit

Permalink
fix bug with arrays of documents in jsonSerialize output
Browse files Browse the repository at this point in the history
fix and test
  • Loading branch information
Tim committed Aug 24, 2020
1 parent 09f4af0 commit f3f68ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ protected function getFieldArray( string ...$fields ) : array
$out[$name][] = $subDocument->jsonSerialize();
}
}
$out[$name] = $this->{$name};
else
{
$out[$name] = $this->{$name};
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion tests/arrayDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class arrayDocumentTest extends TestCase
{
public function testJSONOutput()
protected function getDoc()
{
$doc = new arrayDocument();

Expand All @@ -33,6 +33,13 @@ public function testJSONOutput()
->full_name( 'Jenny Jumper' )
);

return $doc;
}
//------------------------------------------------------------------------
public function testJSONOutput()
{
$doc = $this->getDoc();

$this->assertEquals(
'{"name":[{"full_name":"Robby the Robot","first_name":"Robby","last_name":"Robot"},{"full_name":"Shelia Supreme","first_name":"Shelia","last_name":"Supreme"},{"full_name":"Duran Duran","first_name":"Duran","last_name":"Duran"},{"full_name":"Jenny Jumper","first_name":"Jenny","last_name":"Jumper"}]}',
json_encode( $doc )
Expand All @@ -46,4 +53,12 @@ public function testJSONOutput()
unset( $doc->name );
$doc->doValidate();
}
//------------------------------------------------------------------------
public function testGetFieldArray()
{
$doc = $this->getDoc();
$arr = $doc->jsonSerialize();
$this->assertTrue( is_array( $arr['name'][0] ), 'sub document is serialized' );

}
}

0 comments on commit f3f68ef

Please sign in to comment.