Skip to content

Commit

Permalink
Add JSON testing to EntityTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-parry committed Oct 16, 2018
1 parent 953fdf0 commit 02263db
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,35 @@ public function testCastArrayByArraySerialize()

//--------------------------------------------------------------------

public function testCastAsJSON()
{
$entity = $this->getCastEntity();

$entity->tenth = ['foo' => 'bar'];

// Should be a JSON-encoded string now...
$check = $this->getPrivateProperty($entity, 'tenth');
$this->assertEquals('{"foo":"bar"}', $check);

$this->assertEquals((object) ['foo' => 'bar'], $entity->tenth);
}

public function testCastAsJSONArray()
{
$entity = $this->getCastEntity();

$data = ['Sun', 'Mon', 'Tue'];
$entity->eleventh = $data;

// Should be a JSON-encoded string now...
$check = $this->getPrivateProperty($entity, 'eleventh');
$this->assertEquals('["Sun","Mon","Tue"]', $check);

$this->assertEquals($data, $entity->eleventh);
}

//--------------------------------------------------------------------

public function testAsArray()
{
$entity = $this->getEntity();
Expand Down Expand Up @@ -417,14 +446,14 @@ public function testFilledConstruction()
public function testChangedArray()
{
$data = [
'bar' => 'baz'
'bar' => 'baz'
];

$something = new SomeEntity($data);
$whatsnew = $something->toArray(true);
$expected = $data;
$this->assertEquals($expected, $whatsnew);

$something->magic = 'rockin';
$expected['magic'] = 'rockin';
$expected['foo'] = null;
Expand Down Expand Up @@ -508,6 +537,8 @@ protected function getCastEntity()
protected $seventh;
protected $eighth;
protected $ninth;
protected $tenth;
protected $eleventh;
// 'bar' is db column, 'foo' is internal representation
protected $_options = [
'casts' => [
Expand All @@ -519,7 +550,9 @@ protected function getCastEntity()
'sixth' => 'object',
'seventh' => 'array',
'eighth' => 'datetime',
'ninth' => 'timestamp'
'ninth' => 'timestamp',
'tenth' => 'json',
'eleventh' => 'json-array'
],
'dates' => [],
'datamap' => []
Expand Down

0 comments on commit 02263db

Please sign in to comment.