1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
namespace Crate\Test\DBAL\Functional\Schema; |
23
|
|
|
|
24
|
|
|
use Crate\DBAL\Types\MapType; |
25
|
|
|
use Crate\DBAL\Types\TimestampType; |
26
|
|
|
use Crate\Test\DBAL\DBALFunctionalTestCase; |
27
|
|
|
use Doctrine\DBAL\Schema\Column; |
28
|
|
|
use Doctrine\DBAL\Schema\Table; |
29
|
|
|
use Doctrine\DBAL\Types\IntegerType; |
30
|
|
|
use Doctrine\DBAL\Types\Type; |
31
|
|
|
|
32
|
|
|
class SchemaManagerTest extends DBALFunctionalTestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager |
36
|
|
|
*/ |
37
|
|
|
protected $_sm; |
38
|
|
|
|
39
|
|
|
public function setUp() : void |
40
|
|
|
{ |
41
|
|
|
parent::setUp(); |
42
|
|
|
$this->_sm = $this->_conn->getSchemaManager(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function tearDown() : void |
46
|
|
|
{ |
47
|
|
|
foreach ($this->_sm->listTableNames() as $tableName) { |
48
|
|
|
$this->_sm->dropTable($tableName); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testListTables() |
53
|
|
|
{ |
54
|
|
|
$this->createTestTable('list_tables_test'); |
55
|
|
|
$tables = $this->_sm->listTables(); |
56
|
|
|
|
57
|
|
|
$this->assertIsArray($tables); |
58
|
|
|
$this->assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'."); |
59
|
|
|
|
60
|
|
|
$foundTable = false; |
61
|
|
|
foreach ($tables AS $table) { |
62
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table); |
63
|
|
|
if (strtolower($table->getName()) == 'list_tables_test') { |
64
|
|
|
$foundTable = true; |
65
|
|
|
|
66
|
|
|
$this->assertTrue($table->hasColumn('id')); |
67
|
|
|
$this->assertTrue($table->hasColumn('test')); |
68
|
|
|
$this->assertTrue($table->hasColumn('foreign_key_test')); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->assertTrue( $foundTable , "The 'list_tables_test' table has to be found."); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function createListTableColumns() |
76
|
|
|
{ |
77
|
|
|
$table = new Table('list_table_columns'); |
78
|
|
|
$table->addColumn('text', Type::STRING); |
|
|
|
|
79
|
|
|
$table->addColumn('ts', TimestampType::NAME); |
80
|
|
|
$table->addColumn('num_float_double', Type::FLOAT); |
|
|
|
|
81
|
|
|
$table->addColumn('num_short', Type::SMALLINT); |
|
|
|
|
82
|
|
|
$table->addColumn('num_int', Type::INTEGER); |
|
|
|
|
83
|
|
|
$table->addColumn('num_long', Type::BIGINT); |
|
|
|
|
84
|
|
|
$table->addColumn('id', 'integer', array('notnull' => true)); |
85
|
|
|
$table->setPrimaryKey(array('id')); |
86
|
|
|
|
87
|
|
|
// OBJECT schema definition via platform options |
88
|
|
|
$mapOpts = array( |
89
|
|
|
'type' => MapType::STRICT, |
90
|
|
|
'fields' => array( |
91
|
|
|
new Column('id', Type::getType('integer'), array()), |
92
|
|
|
new Column('name', Type::getType('string'), array()), |
93
|
|
|
), |
94
|
|
|
); |
95
|
|
|
$table->addColumn('obj', 'map', |
96
|
|
|
array('platformOptions'=>$mapOpts)); |
97
|
|
|
|
98
|
|
|
// OBJECT schema definition via columnDefinition |
99
|
|
|
$table->addColumn('obj2', 'map', |
100
|
|
|
array('columnDefinition'=>'OBJECT (STRICT) AS ( id INTEGER, name STRING )')); |
101
|
|
|
|
102
|
|
|
// ARRAY schema definition via platform options |
103
|
|
|
$arrOpts = array( |
104
|
|
|
'type' => Type::FLOAT, |
|
|
|
|
105
|
|
|
); |
106
|
|
|
$table->addColumn('arr_float', 'array', |
107
|
|
|
array('platformOptions'=>$arrOpts)); |
108
|
|
|
|
109
|
|
|
// ARRAY schema definition via columnDefinition |
110
|
|
|
$table->addColumn('arr_str', 'array', |
111
|
|
|
array('columnDefinition'=>'ARRAY (STRING)')); |
112
|
|
|
$table->addColumn('arr_obj', 'array', |
113
|
|
|
array('columnDefinition'=>'ARRAY (OBJECT (IGNORED) AS ( id INTEGER, name STRING ))')); |
114
|
|
|
|
115
|
|
|
return $table; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testListTableColumns() |
119
|
|
|
{ |
120
|
|
|
$table = $this->createListTableColumns(); |
121
|
|
|
|
122
|
|
|
$this->_sm->dropAndCreateTable($table); |
123
|
|
|
|
124
|
|
|
$columns = $this->_sm->listTableColumns('list_table_columns'); |
125
|
|
|
$columnsKeys = array_keys($columns); |
126
|
|
|
|
127
|
|
|
self::assertArrayHasKey('id', $columns); |
128
|
|
|
self::assertEquals(0, array_search('id', $columnsKeys)); |
129
|
|
|
self::assertEquals('id', strtolower($columns['id']->getname())); |
130
|
|
|
self::assertInstanceOf(IntegerType::class, $columns['id']->gettype()); |
131
|
|
|
self::assertFalse($columns['id']->getunsigned()); |
132
|
|
|
self::assertTrue($columns['id']->getnotnull()); |
133
|
|
|
self::assertNull($columns['id']->getdefault()); |
134
|
|
|
self::assertIsArray($columns['id']->getPlatformOptions()); |
135
|
|
|
|
136
|
|
|
$this->assertArrayHasKey('text', $columns); |
137
|
|
|
$this->assertEquals('text', strtolower($columns['text']->getname())); |
138
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $columns['text']->gettype()); |
139
|
|
|
|
140
|
|
|
$this->assertEquals('ts', strtolower($columns['ts']->getname())); |
141
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\TimestampType', $columns['ts']->gettype()); |
142
|
|
|
|
143
|
|
|
$this->assertEquals('num_float_double', strtolower($columns['num_float_double']->getname())); |
144
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\FloatType', $columns['num_float_double']->gettype()); |
145
|
|
|
|
146
|
|
|
$this->assertArrayHasKey('num_short', $columns); |
147
|
|
|
$this->assertEquals('num_short', strtolower($columns['num_short']->getname())); |
148
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\SmallIntType', $columns['num_short']->gettype()); |
149
|
|
|
|
150
|
|
|
$this->assertArrayHasKey('num_int', $columns); |
151
|
|
|
$this->assertEquals('num_int', strtolower($columns['num_int']->getname())); |
152
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $columns['num_int']->gettype()); |
153
|
|
|
|
154
|
|
|
$this->assertArrayHasKey('num_long', $columns); |
155
|
|
|
$this->assertEquals('num_long', strtolower($columns['num_long']->getname())); |
156
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\BigIntType', $columns['num_long']->gettype()); |
157
|
|
|
|
158
|
|
|
$this->assertEquals('obj', strtolower($columns['obj']->getname())); |
159
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\MapType', $columns['obj']->gettype()); |
160
|
|
|
|
161
|
|
|
$this->assertEquals("obj['id']", strtolower($columns["obj['id']"]->getname())); |
162
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $columns["obj['id']"]->gettype()); |
163
|
|
|
|
164
|
|
|
$this->assertEquals("obj['name']", strtolower($columns["obj['name']"]->getname())); |
165
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $columns["obj['name']"]->gettype()); |
166
|
|
|
|
167
|
|
|
$this->assertEquals('obj2', strtolower($columns['obj2']->getname())); |
168
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\MapType', $columns['obj2']->gettype()); |
169
|
|
|
|
170
|
|
|
$this->assertEquals("obj2['id']", strtolower($columns["obj2['id']"]->getname())); |
171
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $columns["obj2['id']"]->gettype()); |
172
|
|
|
|
173
|
|
|
$this->assertEquals("obj2['name']", strtolower($columns["obj2['name']"]->getname())); |
174
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $columns["obj2['name']"]->gettype()); |
175
|
|
|
|
176
|
|
|
$this->assertEquals('arr_float', strtolower($columns['arr_float']->getname())); |
177
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\ArrayType', $columns['arr_float']->gettype()); |
178
|
|
|
|
179
|
|
|
$this->assertEquals('arr_str', strtolower($columns['arr_str']->getname())); |
180
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\ArrayType', $columns['arr_str']->gettype()); |
181
|
|
|
|
182
|
|
|
$this->assertEquals('arr_obj', strtolower($columns['arr_obj']->getname())); |
183
|
|
|
$this->assertInstanceOf('Crate\DBAL\Types\ArrayType', $columns['arr_obj']->gettype()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
public function testCreateSchema() |
188
|
|
|
{ |
189
|
|
|
$this->createTestTable('test_table'); |
190
|
|
|
|
191
|
|
|
$schema = $this->_sm->createSchema(); |
192
|
|
|
$this->assertTrue($schema->hasTable('test_table')); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $name |
197
|
|
|
* @param array $data |
198
|
|
|
*/ |
199
|
|
|
protected function createTestTable($name = 'test_table', $data = array()) |
200
|
|
|
{ |
201
|
|
|
$options = array(); |
202
|
|
|
if (isset($data['options'])) { |
203
|
|
|
$options = $data['options']; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$table = $this->getTestTable($name, $options); |
207
|
|
|
$this->_sm->dropAndCreateTable($table); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function getTestTable($name, $options=array()) |
211
|
|
|
{ |
212
|
|
|
$table = new Table($name, array(), array(), array(), false, $options); |
|
|
|
|
213
|
|
|
$table->setSchemaConfig($this->_sm->createSchemaConfig()); |
214
|
|
|
$table->addColumn('id', 'integer', array('notnull' => true)); |
215
|
|
|
$table->setPrimaryKey(array('id')); |
216
|
|
|
$table->addColumn('test', 'string', array('length' => 255)); |
217
|
|
|
$table->addColumn('foreign_key_test', 'integer'); |
218
|
|
|
return $table; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function getTestCompositeTable($name) |
222
|
|
|
{ |
223
|
|
|
$table = new Table($name, array(), array(), array(), false, array()); |
|
|
|
|
224
|
|
|
$table->setSchemaConfig($this->_sm->createSchemaConfig()); |
225
|
|
|
$table->addColumn('id', 'integer', array('notnull' => true)); |
226
|
|
|
$table->addColumn('other_id', 'integer', array('notnull' => true)); |
227
|
|
|
$table->setPrimaryKey(array('id', 'other_id')); |
228
|
|
|
$table->addColumn('test', 'string', array('length' => 255)); |
229
|
|
|
$table->addColumn('test_other', 'string', array('length' => 255)); |
230
|
|
|
return $table; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
protected function assertHasTable($tables, $tableName) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$foundTable = false; |
236
|
|
|
foreach ($tables AS $table) { |
237
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.'); |
238
|
|
|
if (strtolower($table->getName()) == 'list_tables_test_new_name') { |
239
|
|
|
$foundTable = true; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
$this->assertTrue($foundTable, "Could not find new table"); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testListTableIndexes() |
246
|
|
|
{ |
247
|
|
|
$table = $this->getTestCompositeTable('list_table_indexes_test'); |
248
|
|
|
|
249
|
|
|
$this->_sm->dropAndCreateTable($table); |
250
|
|
|
|
251
|
|
|
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test'); |
252
|
|
|
|
253
|
|
|
self::assertEquals(1, count($tableIndexes)); |
254
|
|
|
|
255
|
|
|
self::assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.'); |
256
|
|
|
self::assertEquals(['id', 'other_id'], array_map('strtolower', $tableIndexes['primary']->getColumns())); |
257
|
|
|
self::assertTrue($tableIndexes['primary']->isUnique()); |
258
|
|
|
self::assertTrue($tableIndexes['primary']->isPrimary()); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.