Skip to content

Commit

Permalink
Implemented 'with' relations and testing belongsToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 7, 2013
1 parent 2f0c4b5 commit 50ff957
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Cake/ORM/Table.php
Expand Up @@ -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']);
}

Expand All @@ -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());
}
}
}
}
Expand Down
71 changes: 71 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -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.