From afa83e32236d718acf57158938308dbc6a479d9a Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 4 Jan 2014 14:08:21 +0100 Subject: [PATCH] Added missing test for attachTo --- .../TestCase/ORM/Association/HasManyTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Cake/Test/TestCase/ORM/Association/HasManyTest.php b/Cake/Test/TestCase/ORM/Association/HasManyTest.php index 1f903c0d853..2ff12671f9c 100644 --- a/Cake/Test/TestCase/ORM/Association/HasManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasManyTest.php @@ -457,6 +457,43 @@ public function testAttachToNoFields() { $association->attachTo($query, ['includeFields' => false]); } +/** + * Tests that by supplying a query builder function, it is possible to add fields + * and conditions to an association + * + * @return void + */ + public function testAttachToWithQueryBuilder() { + $query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]); + $config = [ + 'sourceTable' => $this->author, + 'targetTable' => $this->article, + 'conditions' => ['Articles.is_active' => true] + ]; + $field = new IdentifierExpression('Articles.author_id'); + $association = new HasMany('Articles', $config); + $query->expects($this->once())->method('join')->with([ + 'Articles' => [ + 'conditions' => new QueryExpression([ + 'Articles.is_active' => true, + ['Authors.id' => $field], + new QueryExpression(['a' => 1]) + ]), + 'type' => 'INNER', + 'table' => 'articles' + ] + ]); + $query->expects($this->once())->method('select') + ->with([ + 'Articles__a' => 'Articles.a', + 'Articles__b' => 'Articles.b' + ]); + $builder = function($q) { + return $q->select(['a', 'b'])->where(['a' => 1]); + }; + $association->attachTo($query, ['queryBuilder' => $builder]); + } + /** * Test cascading deletes. *