Passed
Push — amo/scrutinizer-new-engine ( 927449 )
by Andreas
10:16
created

SchemaManagerTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
eloc 129
c 3
b 0
f 1
dl 0
loc 227
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestTable() 0 9 1
A testListTableIndexes() 0 14 1
A assertHasTable() 0 10 3
A testListTables() 0 21 3
A createTestTable() 0 9 2
A createListTableColumns() 0 41 1
A testListTableColumns() 0 66 1
A getTestCompositeTable() 0 10 1
A testCreateSchema() 0 6 1
A setUp() 0 4 1
A tearDown() 0 4 2
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);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::STRING has been deprecated: Use {@see Types::STRING} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
        $table->addColumn('text', /** @scrutinizer ignore-deprecated */ Type::STRING);

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.

Loading history...
79
        $table->addColumn('ts', TimestampType::NAME);
80
        $table->addColumn('num_float_double', Type::FLOAT);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::FLOAT has been deprecated: Use {@see Types::FLOAT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
        $table->addColumn('num_float_double', /** @scrutinizer ignore-deprecated */ Type::FLOAT);

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.

Loading history...
81
        $table->addColumn('num_short', Type::SMALLINT);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::SMALLINT has been deprecated: Use {@see Types::SMALLINT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
        $table->addColumn('num_short', /** @scrutinizer ignore-deprecated */ Type::SMALLINT);

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.

Loading history...
82
        $table->addColumn('num_int', Type::INTEGER);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::INTEGER has been deprecated: Use {@see Types::INTEGER} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
        $table->addColumn('num_int', /** @scrutinizer ignore-deprecated */ Type::INTEGER);

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.

Loading history...
83
        $table->addColumn('num_long', Type::BIGINT);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::BIGINT has been deprecated: Use {@see Types::BIGINT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

83
        $table->addColumn('num_long', /** @scrutinizer ignore-deprecated */ Type::BIGINT);

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.

Loading history...
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,
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::FLOAT has been deprecated: Use {@see Types::FLOAT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
            'type' => /** @scrutinizer ignore-deprecated */ Type::FLOAT,

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $idGeneratorType of Doctrine\DBAL\Schema\Table::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
        $table = new Table($name, array(), array(), array(), /** @scrutinizer ignore-type */ false, $options);
Loading history...
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());
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $idGeneratorType of Doctrine\DBAL\Schema\Table::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

223
        $table = new Table($name, array(), array(), array(), /** @scrutinizer ignore-type */ false, array());
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $tableName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

233
    protected function assertHasTable($tables, /** @scrutinizer ignore-unused */ $tableName)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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