Skip to content
Permalink
Browse files

Implemented 'with' relations and testing belongsToMany

  • Loading branch information...
lorenzo committed Oct 7, 2013
1 parent 2f0c4b5 commit 50ff9574e121cee9a1e4dff0cc758f9ef0fc6ac1
Showing with 77 additions and 2 deletions.
  1. +6 −2 Cake/ORM/Table.php
  2. +71 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -794,7 +794,7 @@ protected function _setupAssociations() {
}
if (!empty($val['className'])) {
$val['entityClass'] = App::classname($val['className'], 'Model\Entity');
$val['entityClass'] = App::classname($val['className'], 'Model\Entity');
unset($val['className']);
}
@@ -804,7 +804,11 @@ protected function _setupAssociations() {
$val['property'] = $name;
}
$this->{$assoc}($key, $val);
$result = $this->{$assoc}($key, $val);
if ($assoc === 'belongsToMany' && !empty($val['with'])) {
$withClass = App::classname($val['with'], 'Model\Entity');
$result->pivot($withClass::repository());
}
}
}
}
@@ -923,4 +923,75 @@ class_alias($bazEntity, 'Foo2\Model\Entity\Baz');
$this->assertSame($bazTarget, $association->target());
}
/**
* Tests automatic setup of associations based on entity information
*
* @return void
*/
public function testAutoSetupBelongstoMany() {
$table = new Table(['table' => 'users', 'connection' => $this->connection]);
$barTarget = new Table(['table' => 'bars', 'connection' => $this->connection]);
$entity = $this->getMockClass('\Cake\ORM\Entity', ['belongsToMany']);
$barEntity = $this->getMockClass('\Cake\ORM\Entity', ['repository']);
class_alias($barEntity, 'Foo3\Model\Entity\Bar');
$entity::staticExpects($this->once())->method('belongsToMany')
->will($this->returnValue(['Foo3.Bar']));
$barEntity::staticExpects($this->at(0))->method('repository')
->will($this->returnValue($barTarget));
$table->entityClass($entity);
$association = $table->association('Bar');
$this->assertInstanceOf('\Cake\ORM\Association\BelongsToMany', $association);
$this->assertEquals('Bar', $association->name());
$this->assertEquals('bars', $association->property());
$this->assertSame($barTarget, $association->target());
}
/**
* Tests that it is possible to setup a belongsTo association and specify the pivot
* entity
*
* @return void
*/
public function testAutoSetupBelongstoManyWith() {
$table = new Table(['table' => 'users', 'connection' => $this->connection]);
$barTarget = new Table(['table' => 'bars', 'connection' => $this->connection]);
$entity = $this->getMockClass('\Cake\ORM\Entity', ['belongsToMany']);
$barEntity = $this->getMockClass('\Cake\ORM\Entity', ['repository']);
class_alias($barEntity, 'Foo4\Model\Entity\Bar');
$entity::staticExpects($this->once())->method('belongsToMany')
->will($this->returnValue([
'Bar' => [
'className' => 'Foo4.Bar',
'with' => 'Foo4.Baz'
]
]));
$barEntity::staticExpects($this->at(1))->method('repository')
->will($this->returnValue($barTarget));
$bazTarget = new Table([
'table' => 'baz',
'connection' => $this->connection
]);
$bazEntity = $this->getMockClass('\Cake\ORM\Entity', ['repository']);
class_alias($bazEntity, 'Foo4\Model\Entity\Baz');
$bazEntity::staticExpects($this->at(0))->method('repository')
->will($this->returnValue($bazTarget));
$table->entityClass($entity);
$association = $table->association('Bar');
$this->assertInstanceOf('\Cake\ORM\Association\BelongsToMany', $association);
$this->assertEquals('Bar', $association->name());
$this->assertEquals('bars', $association->property());
$this->assertSame($barTarget, $association->target());
$this->assertSame($bazTarget, $association->pivot());
}
}

0 comments on commit 50ff957

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