Skip to content
Permalink
Browse files

Making jsonSerialize be called recursively in entities

  • Loading branch information...
lorenzo committed Apr 30, 2016
1 parent c8edd81 commit 590d135b0e1806354eb132443c9630c11599dae9
Showing with 16 additions and 1 deletion.
  1. +1 −1 src/Datasource/EntityTrait.php
  2. +15 −0 tests/TestCase/ORM/EntityTest.php
@@ -467,7 +467,7 @@ public function toArray()
*/
public function jsonSerialize()
{
return $this->toArray();
return $this->extract($this->visibleProperties());
}
/**
@@ -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
*

0 comments on commit 590d135

Please sign in to comment.
You can’t perform that action at this time.