Skip to content

Commit

Permalink
Janky start to marshalling simple + associated data.
Browse files Browse the repository at this point in the history
I need to solve the conventions around this a bit better as right now
it is awkward to use. However, hasOne/belongsTo is working.
  • Loading branch information
markstory committed Dec 13, 2013
1 parent 433815e commit 3ef37bb
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Cake/ORM/Marshaller.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\ORM;

use Cake\ORM\Table;
use Cake\Utility\Hash;

/**
* Contains logic to convert array data into entities.
Expand Down Expand Up @@ -58,6 +59,31 @@ public function __construct(Table $table, $safe = false) {
* @return Cake\ORM\Entity
*/
public function one(array $data, $associations = []) {
$associations = Hash::normalize($associations);
$class = $this->_table->entityClass();
$entity = new $class();
foreach ($data as $key => $value) {
if (array_key_exists($key, $associations)) {
$assoc = $this->_table->association($key);
$value = $this->_marshalAssociation($assoc, $value, $associations[$key]);
$entity->set($assoc->property(), $value);
} else {
$entity->set($key, $value);
}
}
return $entity;
}

/**
* Create a new sub-marshaller and marshal the associated data.
*
* @return mixed
*/
protected function _marshalAssociation($assoc, $value, $associations) {
$targetTable = $assoc->target();
$marshaller = $targetTable->marshaller();
// TODO switch to one based on association type.
return $marshaller->one($value, (array)$associations);
}

/**
Expand Down
141 changes: 141 additions & 0 deletions Cake/Test/TestCase/ORM/MarshallerTest.php
@@ -0,0 +1,141 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\ORM;

use Cake\ORM\Marshaller;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
* Marshaller test case
*/
class MarshallerTest extends TestCase {

public $fixtures = ['core.article', 'core.user', 'core.comment'];

/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$articles->hasMany('Comments');

$comments = TableRegistry::get('Comments');
$comments->belongsTo('Articles');
$comments->belongsTo('Users');

$this->articles = $articles;
$this->comments = $comments;
}

/**
* Teardown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
TableRegistry::clear();
unset($this->articles, $this->comments);
}

/**
* Test one() in a simple use.
*
* @return void
*/
public function testOneSimple() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'not_in_schema' => true
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, []);

$this->assertInstanceOf('Cake\ORM\Entity', $result);
$this->assertEquals($data, $result->toArray());
$this->assertTrue($result->dirty(), 'Should be a dirty entity.');
$this->assertNull($result->isNew(), 'Should be detached');
}

/**
* test one() with association data.
*
* @return void
*/
public function testOneAssociationsSingle() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'Comments' => [
['comment' => 'First post', 'user_id' => 2],
['comment' => 'Second post', 'user_id' => 2],
],
'Users' => [
'username' => 'mark',
'password' => 'secret'
]
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, ['Users']);

$this->assertEquals($data['title'], $result->title);
$this->assertEquals($data['body'], $result->body);
$this->assertEquals($data['author_id'], $result->author_id);

$this->assertInternalType('array', $result->Comments);
$this->assertCount(2, $result->Comments);
$this->assertInternalType('array', $result->Comments[0]);
$this->assertInternalType('array', $result->Comments[1]);

$this->assertInstanceOf('Cake\ORM\Entity', $result->user);
$this->assertEquals($data['Users']['username'], $result->user->username);
$this->assertEquals($data['Users']['password'], $result->user->password);
}

/**
* test one() with association data.
*
* @return void
*/
public function testOneAssociationsMany() {
$this->markTestIncomplete('not done');
}

public function testOneDeepAssociations() {
$this->markTestIncomplete('not done');
}

public function testManySimple() {
$this->markTestIncomplete('not done');
}

public function testManyAssociations() {
$this->markTestIncomplete('not done');
}

public function testManyDeepAssociations() {
$this->markTestIncomplete('not done');
}

}

0 comments on commit 3ef37bb

Please sign in to comment.