/
TableTest.php
685 lines (637 loc) · 18.8 KB
/
TableTest.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
<?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\Database\Schema;
use Cake\Database\Schema\Table;
use Cake\Database\Type;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* Mock class for testing baseType inheritance
*/
class FooType extends Type
{
public function getBaseType()
{
return 'integer';
}
}
/**
* Test case for Table
*/
class TableTest extends TestCase
{
public $fixtures = [
'core.articles',
'core.tags',
'core.articles_tags',
'core.orders',
'core.products'
];
protected $_map;
public function setUp()
{
$this->_map = Type::map();
parent::setUp();
}
public function tearDown()
{
TableRegistry::clear();
Type::clear();
Type::map($this->_map);
parent::tearDown();
}
/**
* Test construction with columns
*
* @return void
*/
public function testConstructWithColumns()
{
$columns = [
'id' => [
'type' => 'integer',
'length' => 11,
],
'title' => [
'type' => 'string',
'length' => 255
]
];
$table = new Table('articles', $columns);
$this->assertEquals(['id', 'title'], $table->columns());
}
/**
* Test adding columns.
*
* @return void
*/
public function testAddColumn()
{
$table = new Table('articles');
$result = $table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
]);
$this->assertSame($table, $result);
$this->assertEquals(['title'], $table->columns());
$result = $table->addColumn('body', 'text');
$this->assertSame($table, $result);
$this->assertEquals(['title', 'body'], $table->columns());
}
/**
* Test hasColumn() method.
*
* @return void
*/
public function testHasColumn()
{
$schema = new Table('articles', [
'title' => 'string'
]);
$this->assertTrue($schema->hasColumn('title'));
$this->assertFalse($schema->hasColumn('body'));
}
/**
* Test removing columns.
*
* @return void
*/
public function testRemoveColumn()
{
$table = new Table('articles');
$result = $table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
])->removeColumn('title')
->removeColumn('unknown');
$this->assertSame($table, $result);
$this->assertEquals([], $table->columns());
$this->assertNull($table->column('title'));
$this->assertSame([], $table->typeMap());
}
/**
* Test isNullable method
*
* @return void
*/
public function testIsNullable()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
])->addColumn('tagline', [
'type' => 'string',
'length' => 25,
'null' => true
]);
$this->assertFalse($table->isNullable('title'));
$this->assertTrue($table->isNullable('tagline'));
$this->assertTrue($table->isNullable('missing'));
}
/**
* Test columnType method
*
* @return void
*/
public function testColumnType()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
]);
$this->assertEquals('string', $table->columnType('title'));
$this->assertNull($table->columnType('not there'));
}
/**
* Test columnType setter method
*
* @return void
*/
public function testColumnTypeSet()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
]);
$this->assertEquals('string', $table->columnType('title'));
$table->columnType('title', 'json');
$this->assertEquals('json', $table->columnType('title'));
}
/**
* Tests getting the baseType as configured when creating the column
*
* @return void
*/
public function testBaseColumnType()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'json',
'baseType' => 'text',
'length' => 25,
'null' => false
]);
$this->assertEquals('json', $table->columnType('title'));
$this->assertEquals('text', $table->baseColumnType('title'));
}
/**
* Tests getting the base type as it is retuned by the Type class
*
* @return void
*/
public function testBaseColumnTypeInherited()
{
Type::map('foo', __NAMESPACE__ . '\FooType');
$table = new Table('articles');
$table->addColumn('thing', [
'type' => 'foo',
'null' => false
]);
$this->assertEquals('foo', $table->columnType('thing'));
$this->assertEquals('integer', $table->baseColumnType('thing'));
}
/**
* Attribute keys should be filtered and have defaults set.
*
* @return void
*/
public function testAddColumnFiltersAttributes()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string'
]);
$result = $table->column('title');
$expected = [
'type' => 'string',
'length' => null,
'precision' => null,
'default' => null,
'null' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
];
$this->assertEquals($expected, $result);
$table->addColumn('author_id', [
'type' => 'integer'
]);
$result = $table->column('author_id');
$expected = [
'type' => 'integer',
'length' => null,
'precision' => null,
'default' => null,
'null' => null,
'unsigned' => null,
'comment' => null,
'autoIncrement' => null,
];
$this->assertEquals($expected, $result);
$table->addColumn('amount', [
'type' => 'decimal'
]);
$result = $table->column('amount');
$expected = [
'type' => 'decimal',
'length' => null,
'precision' => null,
'default' => null,
'null' => null,
'unsigned' => null,
'comment' => null,
];
$this->assertEquals($expected, $result);
}
/**
* Test reading default values.
*
* @return void
*/
public function testDefaultValues()
{
$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer',
'default' => 0
])->addColumn('title', [
'type' => 'string',
'default' => 'A title'
])->addColumn('name', [
'type' => 'string',
'null' => false,
'default' => null,
])->addColumn('body', [
'type' => 'text',
'null' => true,
'default' => null,
]);
$result = $table->defaultValues();
$expected = [
'id' => 0,
'title' => 'A title',
'body' => null
];
$this->assertEquals($expected, $result);
}
/**
* Test adding an constraint.
* >
* @return void
*/
public function testAddConstraint()
{
$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer'
]);
$result = $table->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$this->assertSame($result, $table);
$this->assertEquals(['primary'], $table->constraints());
}
/**
* Test adding an constraint with an overlapping unique index
* >
* @return void
*/
public function testAddConstraintOverwriteUniqueIndex()
{
$table = new Table('articles');
$table->addColumn('project_id', [
'type' => 'integer',
'default' => null,
'limit' => 11,
'null' => false,
])->addColumn('id', [
'type' => 'integer',
'autoIncrement' => true,
'limit' => 11
])->addColumn('user_id', [
'type' => 'integer',
'default' => null,
'limit' => 11,
'null' => false,
])->addConstraint('users_idx', [
'type' => 'unique',
'columns' => ['project_id', 'user_id']
])->addConstraint('users_idx', [
'type' => 'foreign',
'references' => ['users', 'project_id', 'id'],
'columns' => ['project_id', 'user_id']
]);
$this->assertEquals(['users_idx'], $table->constraints());
}
/**
* Dataprovider for invalid addConstraint calls.
*
* @return array
*/
public static function addConstraintErrorProvider()
{
return [
// No properties
[[]],
// Empty columns
[['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
[['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
// Missing column
[['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
];
}
/**
* Test that an exception is raised when constraints
* are added for fields that do not exist.
*
* @dataProvider addConstraintErrorProvider
* @expectedException \Cake\Database\Exception
* @return void
*/
public function testAddConstraintError($props)
{
$table = new Table('articles');
$table->addColumn('author_id', 'integer');
$table->addConstraint('author_idx', $props);
}
/**
* Test adding an index.
*
* @return void
*/
public function testAddIndex()
{
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string'
]);
$result = $table->addIndex('faster', [
'type' => 'index',
'columns' => ['title']
]);
$this->assertSame($result, $table);
$this->assertEquals(['faster'], $table->indexes());
}
/**
* Dataprovider for invalid addIndex calls
*
* @return array
*/
public static function addIndexErrorProvider()
{
return [
// Empty
[[]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
// No columns
[['columns' => ''], 'type' => Table::INDEX_INDEX],
[['columns' => [], 'type' => Table::INDEX_INDEX]],
// Missing column
[['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
];
}
/**
* Test that an exception is raised when indexes
* are added for fields that do not exist.
*
* @dataProvider addIndexErrorProvider
* @expectedException \Cake\Database\Exception
* @return void
*/
public function testAddIndexError($props)
{
$table = new Table('articles');
$table->addColumn('author_id', 'integer');
$table->addIndex('author_idx', $props);
}
/**
* Test adding different kinds of indexes.
*
* @return void
*/
public function testAddIndexTypes()
{
$table = new Table('articles');
$table->addColumn('id', 'integer')
->addColumn('title', 'string')
->addColumn('author_id', 'integer');
$table->addIndex('author_idx', [
'columns' => ['author_id'],
'type' => 'index'
])->addIndex('texty', [
'type' => 'fulltext',
'columns' => ['title']
]);
$this->assertEquals(
['author_idx', 'texty'],
$table->indexes()
);
}
/**
* Test getting the primary key.
*
* @return void
*/
public function testPrimaryKey()
{
$table = new Table('articles');
$table->addColumn('id', 'integer')
->addColumn('title', 'string')
->addColumn('author_id', 'integer')
->addConstraint('author_idx', [
'columns' => ['author_id'],
'type' => 'unique'
])->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$this->assertEquals(['id'], $table->primaryKey());
$table = new Table('articles');
$table->addColumn('id', 'integer')
->addColumn('title', 'string')
->addColumn('author_id', 'integer');
$this->assertEquals([], $table->primaryKey());
}
/**
* Test the options method.
*
* @return void
*/
public function testOptions()
{
$table = new Table('articles');
$options = [
'engine' => 'InnoDB'
];
$return = $table->options($options);
$this->assertInstanceOf('Cake\Database\Schema\Table', $return);
$this->assertEquals($options, $table->options());
}
/**
* Add a basic foreign key constraint.
*
* @return void
*/
public function testAddConstraintForeignKey()
{
$table = new Table('articles');
$table->addColumn('author_id', 'integer')
->addConstraint('author_id_idx', [
'type' => Table::CONSTRAINT_FOREIGN,
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade',
]);
$this->assertEquals(['author_id_idx'], $table->constraints());
}
/**
* Test single column foreign keys constraint support
*
* @return void
*/
public function testConstraintForeignKey()
{
$table = TableRegistry::get('ArticlesTags');
$compositeConstraint = $table->schema()->constraint('tag_id_fk');
$expected = [
'type' => 'foreign',
'columns' => ['tag_id'],
'references' => ['tags', 'id'],
'update' => 'cascade',
'delete' => 'cascade',
'length' => []
];
$this->assertEquals($expected, $compositeConstraint);
$expectedSubstring = 'CONSTRAINT <tag_id_fk> FOREIGN KEY \(<tag_id>\) REFERENCES <tags> \(<id>\)';
$this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
}
/**
* Test composite foreign keys support
*
* @return void
*/
public function testConstraintForeignKeyTwoColumns()
{
$table = TableRegistry::get('Orders');
$compositeConstraint = $table->schema()->constraint('product_category_fk');
$expected = [
'type' => 'foreign',
'columns' => [
'product_category',
'product_id'
],
'references' => [
'products',
['category', 'id']
],
'update' => 'cascade',
'delete' => 'cascade',
'length' => []
];
$this->assertEquals($expected, $compositeConstraint);
$expectedSubstring = 'CONSTRAINT <product_category_fk> FOREIGN KEY \(<product_category>, <product_id>\)' .
' REFERENCES <products> \(<category>, <id>\)';
$this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
}
/**
* Provider for exceptionally bad foreign key data.
*
* @return array
*/
public static function badForeignKeyProvider()
{
return [
'references is bad' => [[
'type' => Table::CONSTRAINT_FOREIGN,
'columns' => ['author_id'],
'references' => ['authors'],
'delete' => 'derp',
]],
'bad update value' => [[
'type' => Table::CONSTRAINT_FOREIGN,
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'derp',
]],
'bad delete value' => [[
'type' => Table::CONSTRAINT_FOREIGN,
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'delete' => 'derp',
]],
];
}
/**
* Add a foreign key constraint with bad data
*
* @dataProvider badForeignKeyProvider
* @expectedException \Cake\Database\Exception
* @return void
*/
public function testAddConstraintForeignKeyBadData($data)
{
$table = new Table('articles');
$table->addColumn('author_id', 'integer')
->addConstraint('author_id_idx', $data);
}
/**
* Tests the temporary() method
*
* @return void
*/
public function testTemporary()
{
$table = new Table('articles');
$this->assertFalse($table->temporary());
$this->assertSame($table, $table->temporary(true));
$this->assertTrue($table->temporary());
$table->temporary(false);
$this->assertFalse($table->temporary());
}
/**
* Assertion for comparing a regex pattern against a query having its identifiers
* quoted. It accepts queries quoted with the characters `<` and `>`. If the third
* parameter is set to true, it will alter the pattern to both accept quoted and
* unquoted queries
*
* @param string $pattern
* @param string $query the result to compare against
* @param bool $optional
* @return void
*/
public function assertQuotedQuery($pattern, $query, $optional = false)
{
if ($optional) {
$optional = '?';
}
$pattern = str_replace('<', '[`"\[]' . $optional, $pattern);
$pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
$this->assertRegExp('#' . $pattern . '#', $query);
}
}