From 50ff9574e121cee9a1e4dff0cc758f9ef0fc6ac1 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Tue, 8 Oct 2013 00:41:19 +0200 Subject: [PATCH] Implemented 'with' relations and testing belongsToMany --- Cake/ORM/Table.php | 8 +++- Cake/Test/TestCase/ORM/TableTest.php | 71 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index 84675589512..8d2d009e35f 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.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()); + } } } } diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index 77e1975205a..e87d587dcd8 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -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()); + } + }