Skip to content

Commit

Permalink
Value Object hydration fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiCollin committed Mar 20, 2015
1 parent 163eb8a commit 14d6a23
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/AnalogueTest/App/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

class Resource extends Entity {

public function __construct($name)
public function __construct($name, V $value = null)
{
$this->name = $name;
// Instantiate value map
if(! $value)
{
$this->value = new V('a','b');
}
else $this->value = $value;
}
}
2 changes: 2 additions & 0 deletions tests/AnalogueTest/App/ResourceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ResourceMap extends EntityMap {

protected $deletedAtColumn = 'custom_deleted_at';

protected $embeddables = ['value' => 'AnalogueTest\App\V'];

public function roles(Resource $resource)
{
return $this->morphedByMany($resource, 'AnalogueTest\App\Role', 'resource_owner');
Expand Down
13 changes: 13 additions & 0 deletions tests/AnalogueTest/App/V.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace AnalogueTest\App;

use Analogue\ORM\ValueObject;

class V extends ValueObject {

public function __construct($a,$b)
{
$this->value_object_1 = $a;
$this->value_object_2 = $b;
}

}
9 changes: 9 additions & 0 deletions tests/AnalogueTest/App/VMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace AnalogueTest\App;

use Analogue\ORM\ValueMap;

class VMap extends ValueMap {

protected $attributes = ['value_object_1', 'value_object_2'];

}
File renamed without changes.
42 changes: 42 additions & 0 deletions tests/AnalogueTest/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPUnit_Framework_TestCase;
use Analogue\ORM\Entity;
use AnalogueTest\App\CustomCommand;
use AnalogueTest\App\Resource;

class MapperTest extends PHPUnit_Framework_TestCase {

Expand Down Expand Up @@ -48,4 +49,45 @@ public function testRedirectCallsOnNewQuery()
$this->assertEquals('entities', $mapper->getTable());
}

public function testStoreAndHydrateAllColumnTypes()
{
$r = new Resource('columns');
$string = ' test ';
$boolean = true;
$integer = 10;
$date = \Carbon\Carbon::now();
$dateTime = \Carbon\Carbon::now();
$time = \Carbon\Carbon::now();
$decimal = 12345.12;
$double = 1234512.12345;
$enum = 'a';
$float = 0.12345;
$json = json_encode(['a' => '1', 'b' => '2']);
$r->foo_string = $string;
$r->foo_boolean= $boolean;
$r->foo_integer =$integer;
$r->foo_date = $date;
$r->foo_dateTime = $dateTime;
$r->foo_time = $time;
$r->foo_decimal = $decimal;
$r->foo_double = $double;
$r->foo_enum = $enum;
$r->foo_float = $float;
$r->foo_json = $json;
$m=get_mapper($r);
$m->store($r);
$q=$m->whereName('columns')->first();

$this->assertEquals($string, $q->foo_string);
$this->assertEquals($boolean, $q->foo_boolean);
$this->assertEquals($integer, $q->foo_integer);
$this->assertEquals($date, $q->foo_date);
$this->assertEquals($dateTime, $q->foo_dateTime);
$this->assertEquals($time, $q->foo_time);
$this->assertEquals($decimal, $q->foo_decimal);
$this->assertEquals($double, $q->foo_double);
$this->assertEquals($enum, $q->foo_enum);
$this->assertEquals($float, $q->foo_float);
$this->assertEquals($json, $q->foo_json);
}
}
17 changes: 16 additions & 1 deletion tests/AnalogueTest/ValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
use AnalogueTest\App\Meta;
use AnalogueTest\App\User;
use AnalogueTest\App\Role;
use AnalogueTest\App\Resource;
use AnalogueTest\App\V;

class ValueObjectTest extends PHPUnit_Framework_TestCase {

public function testValueObjectStore()
public function testValueObjectStoreAndRead()
{
$user = new User('boris', new Role('meta'));
$uMapper = get_mapper($user);
Expand All @@ -16,6 +18,19 @@ public function testValueObjectStore()
$q = $uMapper->whereEmail('boris')->first();
//tdd($q);
$this->assertEquals('value', $q->metas->get('key'));
$this->assertEquals(1, count($q->metas->all()));
}

public function testValueObjectWithMultipleFields()
{
$resource = new Resource('res');
$rMapper = get_mapper($resource);
$v= new V('v1','v2');
$resource->value = $v;
$rMapper->store($resource);
$q = $rMapper->whereName('res')->first();
$this->assertEquals('v1', $q->value->value_object_1);
$this->assertEquals('v2', $q->value->value_object_2);
}

}
Binary file modified tests/test.sqlite
Binary file not shown.

0 comments on commit 14d6a23

Please sign in to comment.