/
AssociationTest.php
755 lines (686 loc) · 22.3 KB
/
AssociationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\ORM;
use Cake\Core\Plugin;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* A Test double used to assert that default tables are created
*/
class TestTable extends Table
{
public function initialize(array $config = [])
{
$this->setSchema(['id' => ['type' => 'integer']]);
}
public function findPublished($query)
{
return $query->applyOptions(['this' => 'worked']);
}
}
/**
* Tests Association class
*/
class AssociationTest extends TestCase
{
/**
* @var \Cake\ORM\Association|\PHPUnit_Framework_MockObject_MockObject
*/
public $association;
/**
* Set up
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->source = new TestTable;
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true,
'sourceTable' => $this->source,
'joinType' => 'INNER'
];
$this->association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
])
->setConstructorArgs(['Foo', $config])
->getMock();
}
/**
* Tear down
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
TableRegistry::clear();
}
/**
* Tests that _options acts as a callback where subclasses can add their own
* initialization code based on the passed configuration array
*
* @return void
*/
public function testOptionsIsCalled()
{
$options = ['foo' => 'bar'];
$this->association->expects($this->once())->method('_options')->with($options);
$this->association->__construct('Name', $options);
}
/**
* Tests that name() returns the correct configure association name
*
* @group deprecated
* @return void
*/
public function testName()
{
$this->deprecated(function () {
$this->assertEquals('Foo', $this->association->name());
$this->association->name('Bar');
$this->assertEquals('Bar', $this->association->name());
});
}
/**
* Tests that setName()
*
* @return void
*/
public function testSetName()
{
$this->assertEquals('Foo', $this->association->getName());
$this->assertSame($this->association, $this->association->setName('Bar'));
$this->assertEquals('Bar', $this->association->getName());
}
/**
* Tests that setName() succeeds before the target table is resolved.
*
* @return void
*/
public function testSetNameBeforeTarget()
{
$this->association->setName('Bar');
$this->assertEquals('Bar', $this->association->getName());
}
/**
* Tests that setName() fails after the target table is resolved.
*
* @return void
*/
public function testSetNameAfterTarger()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Association name does not match target table alias.');
$this->association->getTarget();
$this->association->setName('Bar');
}
/**
* Tests that setName() succeeds if name equals target table alias.
*
* @return void
*/
public function testSetNameToTargetAlias()
{
$alias = $this->association->getTarget()->getAlias();
$this->association->setName($alias);
$this->assertEquals($alias, $this->association->getName());
}
/**
* Tests that className() returns the correct association className
*
* @return void
*/
public function testClassName()
{
$this->assertEquals('\Cake\Test\TestCase\ORM\TestTable', $this->association->className());
}
/**
* Tests that className() returns the correct (unnormalized) className
*
* @return void
*/
public function testClassNameUnnormalized()
{
$config = [
'className' => 'Test',
];
$this->association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
])
->setConstructorArgs(['Foo', $config])
->getMock();
$this->assertEquals('Test', $this->association->className());
}
/**
* Tests that an exception is thrown when invalid target table is fetched
* from a registry.
*
* @return void
*/
public function testInvalidTableFetchedFromRegistry()
{
$this->expectException(\RuntimeException::class);
TableRegistry::get('Test');
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
];
$this->association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
])
->setConstructorArgs(['Test', $config])
->getMock();
$this->association->getTarget();
}
/**
* Tests that a descendant table could be fetched from a registry.
*
* @return void
*/
public function testTargetTableDescendant()
{
TableRegistry::get('Test', [
'className' => '\Cake\Test\TestCase\ORM\TestTable'
]);
$className = '\Cake\ORM\Table';
$config = [
'className' => $className,
];
$this->association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
])
->setConstructorArgs(['Test', $config])
->getMock();
$target = $this->association->getTarget();
$this->assertInstanceOf($className, $target);
}
/**
* Tests that cascadeCallbacks() returns the correct configured value
*
* @group deprecated
* @return void
*/
public function testCascadeCallbacks()
{
$this->deprecated(function () {
$this->assertFalse($this->association->cascadeCallbacks());
$this->association->cascadeCallbacks(true);
$this->assertTrue($this->association->cascadeCallbacks());
});
}
/**
* Tests that cascadeCallbacks() returns the correct configured value
*
* @return void
*/
public function testSetCascadeCallbacks()
{
$this->assertFalse($this->association->getCascadeCallbacks());
$this->assertSame($this->association, $this->association->setCascadeCallbacks(true));
$this->assertTrue($this->association->getCascadeCallbacks());
}
/**
* Tests the bindingKey method as a setter/getter
*
* @group deprecated
* @return void
*/
public function testBindingKey()
{
$this->deprecated(function () {
$this->association->bindingKey('foo_id');
$this->assertEquals('foo_id', $this->association->bindingKey());
});
}
/**
* Tests the bindingKey method as a setter/getter
*
* @return void
*/
public function testSetBindingKey()
{
$this->assertSame($this->association, $this->association->setBindingKey('foo_id'));
$this->assertEquals('foo_id', $this->association->getBindingKey());
}
/**
* Tests the bindingKey() method when called with its defaults
*
* @return void
*/
public function testBindingKeyDefault()
{
$this->source->setPrimaryKey(['id', 'site_id']);
$this->association
->expects($this->once())
->method('isOwningSide')
->will($this->returnValue(true));
$result = $this->association->getBindingKey();
$this->assertEquals(['id', 'site_id'], $result);
}
/**
* Tests the bindingKey() method when the association source is not the
* owning side
*
* @return void
*/
public function testBindingDefaultNoOwningSide()
{
$target = new Table;
$target->setPrimaryKey(['foo', 'site_id']);
$this->association->setTarget($target);
$this->association
->expects($this->once())
->method('isOwningSide')
->will($this->returnValue(false));
$result = $this->association->getBindingKey();
$this->assertEquals(['foo', 'site_id'], $result);
}
/**
* Tests that name() returns the correct configured value
*
* @group deprecated
* @return void
*/
public function testForeignKey()
{
$this->deprecated(function () {
$this->assertEquals('a_key', $this->association->foreignKey());
$this->association->foreignKey('another_key');
$this->assertEquals('another_key', $this->association->foreignKey());
});
}
/**
* Tests setForeignKey()
*
* @return void
*/
public function testSetForeignKey()
{
$this->assertEquals('a_key', $this->association->getForeignKey());
$this->assertSame($this->association, $this->association->setForeignKey('another_key'));
$this->assertEquals('another_key', $this->association->getForeignKey());
}
/**
* Tests that conditions() returns the correct configured value
*
* @group deprecated
* @return void
*/
public function testConditions()
{
$this->deprecated(function () {
$this->assertEquals(['field' => 'value'], $this->association->conditions());
$conds = ['another_key' => 'another value'];
$this->association->conditions($conds);
$this->assertEquals($conds, $this->association->conditions());
});
}
/**
* Tests setConditions()
*
* @return void
*/
public function testSetConditions()
{
$this->assertEquals(['field' => 'value'], $this->association->getConditions());
$conds = ['another_key' => 'another value'];
$this->assertSame($this->association, $this->association->setConditions($conds));
$this->assertEquals($conds, $this->association->getConditions());
}
/**
* Tests that canBeJoined() returns the correct configured value
*
* @return void
*/
public function testCanBeJoined()
{
$this->assertTrue($this->association->canBeJoined());
}
/**
* Tests that target() returns the correct Table object
*
* @group deprecated
* @return void
*/
public function testTarget()
{
$this->deprecated(function () {
$table = $this->association->target();
$this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
$other = new Table;
$this->association->target($other);
$this->assertSame($other, $this->association->target());
});
}
/**
* Tests that setTarget()
*
* @return void
*/
public function testSetTarget()
{
$table = $this->association->getTarget();
$this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
$other = new Table;
$this->assertSame($this->association, $this->association->setTarget($other));
$this->assertSame($other, $this->association->getTarget());
}
/**
* Tests that target() returns the correct Table object for plugins
*
* @return void
*/
public function testTargetPlugin()
{
Plugin::load('TestPlugin');
$config = [
'className' => 'TestPlugin.Comments',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true,
'sourceTable' => $this->source,
'joinType' => 'INNER'
];
$this->association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
'requiresKeys'
])
->setConstructorArgs(['ThisAssociationName', $config])
->getMock();
$table = $this->association->getTarget();
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $table);
$this->assertTrue(
TableRegistry::exists('TestPlugin.ThisAssociationName'),
'The association class will use this registry key'
);
$this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The association class will NOT use this key');
$this->assertFalse(TableRegistry::exists('Comments'), 'Should also not be set');
$this->assertFalse(TableRegistry::exists('ThisAssociationName'), 'Should also not be set');
$plugin = TableRegistry::get('TestPlugin.ThisAssociationName');
$this->assertSame($table, $plugin, 'Should be an instance of TestPlugin.Comments');
$this->assertSame('TestPlugin.ThisAssociationName', $table->getRegistryAlias());
$this->assertSame('comments', $table->getTable());
$this->assertSame('ThisAssociationName', $table->getAlias());
}
/**
* Tests that source() returns the correct Table object
*
* @group deprecated
* @return void
*/
public function testSource()
{
$this->deprecated(function () {
$table = $this->association->source();
$this->assertSame($this->source, $table);
$other = new Table;
$this->association->source($other);
$this->assertSame($other, $this->association->source());
});
}
/**
* Tests that source() returns the correct Table object
*
* @return void
*/
public function testSetSource()
{
$table = $this->association->getSource();
$this->assertSame($this->source, $table);
$other = new Table;
$this->assertSame($this->association, $this->association->setSource($other));
$this->assertSame($other, $this->association->getSource());
}
/**
* Tests joinType method
*
* @group deprecated
* @return void
*/
public function testJoinType()
{
$this->deprecated(function () {
$this->assertEquals('INNER', $this->association->joinType());
$this->association->joinType('LEFT');
$this->assertEquals('LEFT', $this->association->joinType());
});
}
/**
* Tests setJoinType method
*
* @return void
*/
public function testSetJoinType()
{
$this->assertEquals('INNER', $this->association->getJoinType());
$this->assertSame($this->association, $this->association->setJoinType('LEFT'));
$this->assertEquals('LEFT', $this->association->getJoinType());
}
/**
* Tests dependent method
*
* @group deprecated
* @return void
*/
public function testDependent()
{
$this->deprecated(function () {
$this->assertTrue($this->association->dependent());
$this->association->dependent(false);
$this->assertFalse($this->association->dependent());
});
}
/**
* Tests property method
*
* @group deprecated
* @return void
*/
public function testProperty()
{
$this->deprecated(function () {
$this->assertEquals('foo', $this->association->property());
$this->association->property('thing');
$this->assertEquals('thing', $this->association->property());
});
}
/**
* Tests property method
*
* @return void
*/
public function testSetProperty()
{
$this->assertEquals('foo', $this->association->getProperty());
$this->assertSame($this->association, $this->association->setProperty('thing'));
$this->assertEquals('thing', $this->association->getProperty());
}
/**
* Test that warning is shown if property name clashes with table field.
*
* @return void
*/
public function testPropertyNameClash()
{
$this->expectException(\PHPUnit\Framework\Error\Warning::class);
$this->expectExceptionMessageRegExp('/^Association property name "foo" clashes with field of same name of table "test"/');
$this->source->setSchema(['foo' => ['type' => 'string']]);
$this->assertEquals('foo', $this->association->getProperty());
}
/**
* Test that warning is not shown if "propertyName" option is explicitly specified.
*
* @return void
*/
public function testPropertyNameExplicitySet()
{
$this->source->setSchema(['foo' => ['type' => 'string']]);
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true,
'sourceTable' => $this->source,
'joinType' => 'INNER',
'propertyName' => 'foo'
];
$association = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
])
->setConstructorArgs(['Foo', $config])
->getMock();
$this->assertEquals('foo', $association->getProperty());
}
/**
* Tests strategy method
*
* @group deprecated
* @return void
*/
public function testStrategy()
{
$this->deprecated(function () {
$this->assertEquals('join', $this->association->strategy());
$this->association->strategy('select');
$this->assertEquals('select', $this->association->strategy());
$this->association->strategy('subquery');
$this->assertEquals('subquery', $this->association->strategy());
});
}
/**
* Tests strategy method
*
* @return void
*/
public function testSetStrategy()
{
$this->assertEquals('join', $this->association->getStrategy());
$this->association->setStrategy('select');
$this->assertEquals('select', $this->association->getStrategy());
$this->association->setStrategy('subquery');
$this->assertEquals('subquery', $this->association->getStrategy());
}
/**
* Tests that providing an invalid strategy throws an exception
*
* @return void
*/
public function testInvalidStrategy()
{
$this->expectException(\InvalidArgumentException::class);
$this->association->setStrategy('anotherThing');
}
/**
* Tests test finder() method as getter and setter
*
* @group deprecated
* @return void
*/
public function testFinderMethod()
{
$this->deprecated(function () {
$this->assertEquals('all', $this->association->finder());
$this->assertEquals('published', $this->association->finder('published'));
$this->assertEquals('published', $this->association->finder());
});
}
/**
* Tests test setFinder() method
*
* @return void
*/
public function testSetFinderMethod()
{
$this->assertEquals('all', $this->association->getFinder());
$this->assertSame($this->association, $this->association->setFinder('published'));
$this->assertEquals('published', $this->association->getFinder());
}
/**
* Tests that `finder` is a valid option for the association constructor
*
* @return void
*/
public function testFinderInConstructor()
{
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true,
'sourceTable' => $this->source,
'joinType' => 'INNER',
'finder' => 'published'
];
$assoc = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
'requiresKeys'
])
->setConstructorArgs(['Foo', $config])
->getMock();
$this->assertEquals('published', $assoc->getFinder());
}
/**
* Tests that the defined custom finder is used when calling find
* in the association
*
* @return void
*/
public function testCustomFinderIsUsed()
{
$this->association->setFinder('published');
$this->assertEquals(
['this' => 'worked'],
$this->association->find()->getOptions()
);
}
/**
* Tests that `locator` is a valid option for the association constructor
*
* @return void
*/
public function testLocatorInConstructor()
{
$locator = $this->getMockBuilder('Cake\ORM\Locator\LocatorInterface')->getMock();
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'tableLocator' => $locator
];
$assoc = $this->getMockBuilder('\Cake\ORM\Association')
->setMethods([
'type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated',
'requiresKeys'
])
->setConstructorArgs(['Foo', $config])
->getMock();
$this->assertEquals($locator, $assoc->getTableLocator());
}
}