Skip to content

Commit

Permalink
Making jsonSerialize be called recursively in entities
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 30, 2016
1 parent c8edd81 commit 590d135
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Datasource/EntityTrait.php
Expand Up @@ -467,7 +467,7 @@ public function toArray()
*/
public function jsonSerialize()
{
return $this->toArray();
return $this->extract($this->visibleProperties());
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -645,6 +645,21 @@ public function testJsonSerialize()
$this->assertEquals(json_encode($data), json_encode($entity));
}

/**
* Tests that jsonSerialize is called recursivily for contained entities
*
* @return void
*/
public function testJsonSerializeRecursive()
{
$phone = $this->getMock(Entity::class, ['jsonSerialize']);
$phone->expects($this->once())->method('jsonSerialize')->will($this->returnValue('12345'));
$data = ['name' => 'James', 'age' => 20, 'phone' => $phone];
$entity = new Entity($data);
$expected = ['name' => 'James', 'age' => 20, 'phone' => '12345'];
$this->assertEquals(json_encode($expected), json_encode($entity));
}

/**
* Tests the extract method
*
Expand Down

0 comments on commit 590d135

Please sign in to comment.