/
TableTest.php
326 lines (305 loc) · 7.33 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
<?php
/**
* PHP Version 5.4
*
* 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 CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Database\Schema;
use Cake\Database\Schema\Table;
use Cake\TestSuite\TestCase;
/**
* Test case for Table
*/
class TableTest extends TestCase {
/**
* 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 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'));
}
/**
* 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,
];
$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());
}
/**
* Dataprovider for invalid addConstraint calls.
*
* @return array
*/
public static function addConstaintErrorProvider() {
return [
// No properties
[[]],
// Empty columns
[['columns' => '']],
[['columns' => []]],
// Missing column
[['columns' => ['derp']]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
];
}
/**
* Test that an exception is raised when constraints
* are added for fields that do not exist.
*
* @dataProvider addConstaintErrorProvider
* @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
[[]],
// No columns
[['columns' => '']],
[['columns' => []]],
// Missing column
[['columns' => ['not_there']]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
];
}
/**
* 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());
}
/**
* 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());
}
/**
* 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);
}
}