/
TableRegistryTest.php
543 lines (468 loc) · 18 KB
/
TableRegistryTest.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\ORM;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;
/**
* Used to test correct class is instantiated when using TableRegistry::get();
*/
class MyUsersTable extends Table
{
/**
* Overrides default table name
*
* @var string
*/
protected $_table = 'users';
}
/**
* Test case for TableRegistry
*/
class TableRegistryTest extends TestCase
{
/**
* setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
Configure::write('App.namespace', 'TestApp');
}
/**
* tear down
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
TableRegistry::clear();
}
/**
* Test config() method.
*
* @return void
*/
public function testConfig()
{
$this->assertEquals([], TableRegistry::config('Tests'));
$data = [
'connection' => 'testing',
'entityClass' => 'TestApp\Model\Entity\Article',
];
$result = TableRegistry::config('Tests', $data);
$this->assertEquals($data, $result, 'Returns config data.');
$result = TableRegistry::config();
$expected = ['Tests' => $data];
$this->assertEquals($expected, $result);
}
/**
* Test config() method with plugin syntax aliases
*
* @return void
*/
public function testConfigPlugin()
{
Plugin::load('TestPlugin');
$data = [
'connection' => 'testing',
'entityClass' => 'TestPlugin\Model\Entity\Comment',
];
$result = TableRegistry::config('TestPlugin.TestPluginComments', $data);
$this->assertEquals($data, $result, 'Returns config data.');
}
/**
* Test calling config() on existing instances throws an error.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage You cannot configure "Users", it has already been constructed.
* @return void
*/
public function testConfigOnDefinedInstance()
{
$users = TableRegistry::get('Users');
TableRegistry::config('Users', ['table' => 'my_users']);
}
/**
* Test the exists() method.
*
* @return void
*/
public function testExists()
{
$this->assertFalse(TableRegistry::exists('Articles'));
TableRegistry::config('Articles', ['table' => 'articles']);
$this->assertFalse(TableRegistry::exists('Articles'));
TableRegistry::get('Articles', ['table' => 'articles']);
$this->assertTrue(TableRegistry::exists('Articles'));
}
/**
* Test the exists() method with plugin-prefixed models.
*
* @return void
*/
public function testExistsPlugin()
{
$this->assertFalse(TableRegistry::exists('Comments'));
$this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
TableRegistry::config('TestPlugin.Comments', ['table' => 'comments']);
$this->assertFalse(TableRegistry::exists('Comments'), 'The Comments key should not be populated');
$this->assertFalse(TableRegistry::exists('TestPlugin.Comments'), 'The plugin.alias key should not be populated');
TableRegistry::get('TestPlugin.Comments', ['table' => 'comments']);
$this->assertFalse(TableRegistry::exists('Comments'), 'The Comments key should not be populated');
$this->assertTrue(TableRegistry::exists('TestPlugin.Comments'), 'The plugin.alias key should now be populated');
}
/**
* Test getting instances from the registry.
*
* @return void
*/
public function testGet()
{
$result = TableRegistry::get('Articles', [
'table' => 'my_articles',
]);
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('my_articles', $result->table());
$result2 = TableRegistry::get('Articles');
$this->assertSame($result, $result2);
$this->assertEquals('my_articles', $result->table());
}
/**
* Are auto-models instanciated correctly? How about when they have an alias?
*
* @return void
*/
public function testGetFallbacks()
{
$result = TableRegistry::get('Droids');
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('droids', $result->table());
$this->assertEquals('Droids', $result->alias());
$result = TableRegistry::get('R2D2', ['className' => 'Droids']);
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('droids', $result->table(), 'The table should be derived from the className');
$this->assertEquals('R2D2', $result->alias());
$result = TableRegistry::get('C3P0', ['className' => 'Droids', 'table' => 'rebels']);
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('rebels', $result->table(), 'The table should be taken from options');
$this->assertEquals('C3P0', $result->alias());
$result = TableRegistry::get('Stuff', ['className' => 'Cake\ORM\Table']);
$this->assertInstanceOf('Cake\ORM\Table', $result);
$this->assertEquals('stuff', $result->table(), 'The table should be drived from the alias');
$this->assertEquals('Stuff', $result->alias());
}
/**
* Test that get() uses config data set with config()
*
* @return void
*/
public function testGetWithConfig()
{
TableRegistry::config('Articles', [
'table' => 'my_articles',
]);
$result = TableRegistry::get('Articles');
$this->assertEquals('my_articles', $result->table(), 'Should use config() data.');
}
/**
* Test get with config throws an exception if the alias exists already.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage You cannot configure "Users", it already exists in the registry.
* @return void
*/
public function testGetExistingWithConfigData()
{
$users = TableRegistry::get('Users');
TableRegistry::get('Users', ['table' => 'my_users']);
}
/**
* Test get() can be called several times with the same option without
* throwing an exception.
*
* @return void
*/
public function testGetWithSameOption()
{
$result = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
$result2 = TableRegistry::get('Users', ['className' => 'Cake\Test\TestCase\ORM\MyUsersTable']);
$this->assertEquals($result, $result2);
}
/**
* Tests that tables can be instantiated based on conventions
* and using plugin notation
*
* @return void
*/
public function testGetWithConventions()
{
$table = TableRegistry::get('articles');
$this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
$table = TableRegistry::get('Articles');
$this->assertInstanceOf('TestApp\Model\Table\ArticlesTable', $table);
$table = TableRegistry::get('authors');
$this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
$table = TableRegistry::get('Authors');
$this->assertInstanceOf('TestApp\Model\Table\AuthorsTable', $table);
}
/**
* Test get() with plugin syntax aliases
*
* @return void
*/
public function testGetPlugin()
{
Plugin::load('TestPlugin');
$table = TableRegistry::get('TestPlugin.TestPluginComments', ['connection' => 'test']);
$this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $table);
$this->assertFalse(
TableRegistry::exists('TestPluginComments'),
'Short form should NOT exist'
);
$this->assertTrue(
TableRegistry::exists('TestPlugin.TestPluginComments'),
'Long form should exist'
);
$second = TableRegistry::get('TestPlugin.TestPluginComments');
$this->assertSame($table, $second, 'Can fetch long form');
}
/**
* Test get() with same-alias models in different plugins
*
* There should be no internal cache-confusion
*
* @return void
*/
public function testGetMultiplePlugins()
{
Plugin::load('TestPlugin');
Plugin::load('TestPluginTwo');
$app = TableRegistry::get('Comments');
$plugin1 = TableRegistry::get('TestPlugin.Comments');
$plugin2 = TableRegistry::get('TestPluginTwo.Comments');
$this->assertInstanceOf('Cake\ORM\Table', $app, 'Should be an app table instance');
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should be a plugin 1 table instance');
$this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should be a plugin 2 table instance');
$plugin2 = TableRegistry::get('TestPluginTwo.Comments');
$plugin1 = TableRegistry::get('TestPlugin.Comments');
$app = TableRegistry::get('Comments');
$this->assertInstanceOf('Cake\ORM\Table', $app, 'Should still be an app table instance');
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $plugin1, 'Should still be a plugin 1 table instance');
$this->assertInstanceOf('TestPluginTwo\Model\Table\CommentsTable', $plugin2, 'Should still be a plugin 2 table instance');
}
/**
* Test get() with plugin aliases + className option.
*
* @return void
*/
public function testGetPluginWithClassNameOption()
{
Plugin::load('TestPlugin');
$table = TableRegistry::get('Comments', [
'className' => 'TestPlugin.TestPluginComments',
'connection' => 'test'
]);
$class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
$this->assertInstanceOf($class, $table);
$this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
$this->assertFalse(TableRegistry::exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
$this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
$second = TableRegistry::get('Comments');
$this->assertSame($table, $second);
}
/**
* Test get() with full namespaced classname
*
* @return void
*/
public function testGetPluginWithFullNamespaceName()
{
Plugin::load('TestPlugin');
$class = 'TestPlugin\Model\Table\TestPluginCommentsTable';
$table = TableRegistry::get('Comments', [
'className' => $class,
'connection' => 'test'
]);
$this->assertInstanceOf($class, $table);
$this->assertFalse(TableRegistry::exists('TestPluginComments'), 'Class name should not exist');
$this->assertFalse(TableRegistry::exists('TestPlugin.TestPluginComments'), 'Full class alias should not exist');
$this->assertTrue(TableRegistry::exists('Comments'), 'Class name should exist');
}
/**
* Tests that table options can be pre-configured for the factory method
*
* @return void
*/
public function testConfigAndBuild()
{
TableRegistry::clear();
$map = TableRegistry::config();
$this->assertEquals([], $map);
$connection = ConnectionManager::get('test', false);
$options = ['connection' => $connection];
TableRegistry::config('users', $options);
$map = TableRegistry::config();
$this->assertEquals(['users' => $options], $map);
$this->assertEquals($options, TableRegistry::config('users'));
$schema = ['id' => ['type' => 'rubbish']];
$options += ['schema' => $schema];
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['table' => 'users']);
$this->assertInstanceOf('Cake\ORM\Table', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
$this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
TableRegistry::clear();
$this->assertEmpty(TableRegistry::config());
TableRegistry::config('users', $options);
$table = TableRegistry::get('users', ['className' => __NAMESPACE__ . '\MyUsersTable']);
$this->assertInstanceOf(__NAMESPACE__ . '\MyUsersTable', $table);
$this->assertEquals('users', $table->table());
$this->assertEquals('users', $table->alias());
$this->assertSame($connection, $table->connection());
$this->assertEquals(array_keys($schema), $table->schema()->columns());
$this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
}
/**
* Tests that table options can be pre-configured with a single validator
*
* @return void
*/
public function testConfigWithSingleValidator()
{
$validator = new Validator();
TableRegistry::config('users', ['validator' => $validator]);
$table = TableRegistry::get('users');
$this->assertSame($table->validator('default'), $validator);
}
/**
* Tests that table options can be pre-configured with multiple validators
*
* @return void
*/
public function testConfigWithMultipleValidators()
{
$validator1 = new Validator();
$validator2 = new Validator();
$validator3 = new Validator();
TableRegistry::config('users', [
'validator' => [
'default' => $validator1,
'secondary' => $validator2,
'tertiary' => $validator3,
]
]);
$table = TableRegistry::get('users');
$this->assertSame($table->validator('default'), $validator1);
$this->assertSame($table->validator('secondary'), $validator2);
$this->assertSame($table->validator('tertiary'), $validator3);
}
/**
* Test setting an instance.
*
* @return void
*/
public function testSet()
{
$mock = $this->getMock('Cake\ORM\Table');
$this->assertSame($mock, TableRegistry::set('Articles', $mock));
$this->assertSame($mock, TableRegistry::get('Articles'));
}
/**
* Test setting an instance with plugin syntax aliases
*
* @return void
*/
public function testSetPlugin()
{
Plugin::load('TestPlugin');
$mock = $this->getMock('TestPlugin\Model\Table\CommentsTable');
$this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock));
$this->assertSame($mock, TableRegistry::get('TestPlugin.Comments'));
}
/**
* Tests genericInstances
*
* @return void
*/
public function testGenericInstances()
{
$foos = TableRegistry::get('Foos');
$bars = TableRegistry::get('Bars');
TableRegistry::get('Articles');
$expected = ['Foos' => $foos, 'Bars' => $bars];
$this->assertEquals($expected, TableRegistry::genericInstances());
}
/**
* Tests remove an instance
*
* @return void
*/
public function testRemove()
{
$first = TableRegistry::get('Comments');
$this->assertTrue(TableRegistry::exists('Comments'));
TableRegistry::remove('Comments');
$this->assertFalse(TableRegistry::exists('Comments'));
$second = TableRegistry::get('Comments');
$this->assertNotSame($first, $second, 'Should be different objects, as the reference to the first was destroyed');
$this->assertTrue(TableRegistry::exists('Comments'));
}
/**
* testRemovePlugin
*
* Removing a plugin-prefixed model should not affect any other
* plugin-prefixed model, or app model.
* Removing an app model should not affect any other
* plugin-prefixed model.
*
* @return void
*/
public function testRemovePlugin()
{
Plugin::load('TestPlugin');
Plugin::load('TestPluginTwo');
$app = TableRegistry::get('Comments');
TableRegistry::get('TestPlugin.Comments');
$plugin = TableRegistry::get('TestPluginTwo.Comments');
$this->assertTrue(TableRegistry::exists('Comments'));
$this->assertTrue(TableRegistry::exists('TestPlugin.Comments'));
$this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
TableRegistry::remove('TestPlugin.Comments');
$this->assertTrue(TableRegistry::exists('Comments'));
$this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
$this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
$app2 = TableRegistry::get('Comments');
$plugin2 = TableRegistry::get('TestPluginTwo.Comments');
$this->assertSame($app, $app2, 'Should be the same Comments object');
$this->assertSame($plugin, $plugin2, 'Should be the same TestPluginTwo.Comments object');
TableRegistry::remove('Comments');
$this->assertFalse(TableRegistry::exists('Comments'));
$this->assertFalse(TableRegistry::exists('TestPlugin.Comments'));
$this->assertTrue(TableRegistry::exists('TestPluginTwo.Comments'));
$plugin3 = TableRegistry::get('TestPluginTwo.Comments');
$this->assertSame($plugin, $plugin3, 'Should be the same TestPluginTwo.Comments object');
}
}