@@ -32,18 +32,21 @@ class BelongsToManyTest extends \Cake\TestSuite\TestCase {
32
32
* @return void
33
33
*/
34
34
public function setUp () {
35
- $ this ->tag = Table::build ('Tag ' , [
36
- 'schema ' => [
37
- 'id ' => ['type ' => 'integer ' ],
38
- 'name ' => ['type ' => 'string ' ],
39
- ]
35
+ $ this ->tag = $ this ->getMock (
36
+ 'Cake\ORM\Table ' , ['find ' ], [['alias ' => 'Tag ' , 'table ' => 'tags ' ]]
37
+ );
38
+ $ this ->tag ->schema ([
39
+ 'id ' => ['type ' => 'integer ' ],
40
+ 'name ' => ['type ' => 'string ' ],
40
41
]);
41
- $ this ->article = Table::build ('Article ' , [
42
- 'schema ' => [
43
- 'id ' => ['type ' => 'integer ' ],
44
- 'name ' => ['type ' => 'string ' ],
45
- ]
42
+ $ this ->article = $ this ->getMock (
43
+ 'Cake\ORM\Table ' , ['find ' ], [['alias ' => 'Article ' , 'table ' => 'articles ' ]]
44
+ );
45
+ $ this ->article ->schema ([
46
+ 'id ' => ['type ' => 'integer ' ],
47
+ 'name ' => ['type ' => 'string ' ],
46
48
]);
49
+ Table::instance ('Article ' , $ this ->article );
47
50
}
48
51
49
52
/**
@@ -201,7 +204,7 @@ public function testAttachTo() {
201
204
* @return void
202
205
*/
203
206
public function testAttachToNoFields () {
204
- $ query = $ this ->getMock ('\Cake\ORM\Query ' , ['join ' , 'select ' ], [null ]);
207
+ $ query = $ this ->getMock ('\Cake\ORM\Query ' , ['join ' , 'select ' ], [null ]);
205
208
$ config = [
206
209
'sourceTable ' => $ this ->article ,
207
210
'targetTable ' => $ this ->tag ,
@@ -237,4 +240,108 @@ public function testAttachToNoFields() {
237
240
$ association ->attachTo ($ query , ['includeFields ' => false ]);
238
241
}
239
242
243
+ /**
244
+ * Test the eager loader method with no extra options
245
+ *
246
+ * @return void
247
+ */
248
+ public function testEagerLoader () {
249
+ $ config = [
250
+ 'sourceTable ' => $ this ->article ,
251
+ 'targetTable ' => $ this ->tag ,
252
+ ];
253
+ Table::build ('ArticleTag ' , [
254
+ 'table ' => 'articles_tags ' ,
255
+ 'schema ' => [
256
+ 'article_id ' => ['type ' => 'integer ' ],
257
+ 'tag_id ' => ['type ' => 'integer ' ]
258
+ ]
259
+ ]);
260
+ $ association = new BelongsToMany ('Tag ' , $ config );
261
+ $ keys = [1 , 2 , 3 , 4 ];
262
+ $ query = $ this ->getMock ('Cake\ORM\Query ' , ['execute ' , 'contain ' ], [null ]);
263
+ $ this ->tag ->expects ($ this ->once ())->method ('find ' )->with ('all ' )
264
+ ->will ($ this ->returnValue ($ query ));
265
+ $ results = [
266
+ ['id ' => 1 , 'name ' => 'foo ' , 'ArticleTag ' => ['article_id ' => 1 ]],
267
+ ['id ' => 2 , 'name ' => 'bar ' , 'ArticleTag ' => ['article_id ' => 2 ]]
268
+ ];
269
+ $ query ->expects ($ this ->once ())->method ('execute ' )
270
+ ->will ($ this ->returnValue ($ results ));
271
+
272
+ $ query ->expects ($ this ->once ())->method ('contain ' )->with ([
273
+ 'ArticleTag ' => [
274
+ 'conditions ' => ['ArticleTag.article_id in ' => $ keys ],
275
+ 'filtering ' => true
276
+ ]
277
+ ]);
278
+
279
+ $ callable = $ association ->eagerLoader (compact ('keys ' ));
280
+ $ row = ['Article__id ' => 1 , 'title ' => 'article 1 ' ];
281
+ $ result = $ callable ($ row );
282
+ $ row ['Article__Tag ' ] = [
283
+ ['id ' => 1 , 'name ' => 'foo ' , 'ArticleTag ' => ['article_id ' => 1 ]]
284
+ ];
285
+ $ this ->assertEquals ($ row , $ result );
286
+
287
+ $ row = ['Article__id ' => 2 , 'title ' => 'article 2 ' ];
288
+ $ result = $ callable ($ row );
289
+ $ row ['Article__Tag ' ] = [
290
+ ['id ' => 2 , 'name ' => 'bar ' , 'ArticleTag ' => ['article_id ' => 2 ]]
291
+ ];
292
+ $ this ->assertEquals ($ row , $ result );
293
+ }
294
+
295
+
296
+ /**
297
+ * Test the eager loader method with default query clauses
298
+ *
299
+ * @return void
300
+ */
301
+ public function testEagerLoaderWithDefaults () {
302
+ $ config = [
303
+ 'sourceTable ' => $ this ->article ,
304
+ 'targetTable ' => $ this ->tag ,
305
+ 'conditions ' => ['Tag.name ' => 'foo ' ],
306
+ 'sort ' => ['id ' => 'ASC ' ],
307
+ ];
308
+ Table::build ('ArticleTag ' , [
309
+ 'table ' => 'articles_tags ' ,
310
+ 'schema ' => [
311
+ 'article_id ' => ['type ' => 'integer ' ],
312
+ 'tag_id ' => ['type ' => 'integer ' ]
313
+ ]
314
+ ]);
315
+ $ association = new BelongsToMany ('Tag ' , $ config );
316
+ $ keys = [1 , 2 , 3 , 4 ];
317
+ $ methods = ['execute ' , 'contain ' , 'where ' , 'order ' ];
318
+ $ query = $ this ->getMock ('Cake\ORM\Query ' , $ methods , [null ]);
319
+ $ this ->tag ->expects ($ this ->once ())->method ('find ' )->with ('all ' )
320
+ ->will ($ this ->returnValue ($ query ));
321
+ $ results = [
322
+ ['id ' => 1 , 'name ' => 'foo ' , 'ArticleTag ' => ['article_id ' => 1 ]],
323
+ ['id ' => 2 , 'name ' => 'bar ' , 'ArticleTag ' => ['article_id ' => 2 ]]
324
+ ];
325
+ $ query ->expects ($ this ->once ())->method ('execute ' )
326
+ ->will ($ this ->returnValue ($ results ));
327
+
328
+ $ query ->expects ($ this ->once ())->method ('contain ' )->with ([
329
+ 'ArticleTag ' => [
330
+ 'conditions ' => ['ArticleTag.article_id in ' => $ keys ],
331
+ 'filtering ' => true
332
+ ]
333
+ ]);
334
+
335
+ $ query ->expects ($ this ->once ())->method ('where ' )
336
+ ->with (['Tag.name ' => 'foo ' ])
337
+ ->will ($ this ->returnValue ($ query ));
338
+
339
+ $ query ->expects ($ this ->once ())->method ('order ' )
340
+ ->with (['id ' => 'ASC ' ])
341
+ ->will ($ this ->returnValue ($ query ));
342
+
343
+ $ callable = $ association ->eagerLoader (compact ('keys ' ));
344
+ }
345
+
346
+
240
347
}
0 commit comments