Passed
Pull Request — master (#1312)
by Michael
05:26
created

TablesTest::testAddToQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xmf\Test\Database;
6
7
use Xmf\Database\Tables;
8
9
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
require_once dirname(__DIR__, 4) . '/init_new.php';
12
13
require_once(XOOPS_TU_ROOT_PATH . '/class/logger/xoopslogger.php');
14
require_once(XOOPS_TU_ROOT_PATH . '/class/xoopsload.php');
15
require_once(XOOPS_TU_ROOT_PATH . '/class/preload.php');
16
require_once(XOOPS_TU_ROOT_PATH . '/class/database/databasefactory.php');
17
18
//$xoops       = \Xoops::getInstance();
19
//$xoopsLogger = $xoops->logger();
20
//$xoops->events();
21
//$psr4loader = new \Xoops\Core\Psr4ClassLoader();
22
//$psr4loader->register();
23
//$xoops->events()
24
//      ->triggerEvent('core.include.common.psr4loader', $psr4loader);
25
//$xoops->events()
26
//      ->triggerEvent('core.include.common.classmaps');
27
28
class TablesTest extends TestCase
29
{
30
    /**
31
     * @var Tables
32
     */
33
    protected $object;
34
    /**
35
     * @var string
36
     */
37
    protected $prefix;
38
39
    /**
40
     * Sets up the fixture, for example, opens a network connection.
41
     * This method is called before a test is executed.
42
     */
43
    protected function setUp(): void
44
    {
45
        $this->object = new Tables();
46
        $this->prefix = \XoopsDatabaseFactory::getDatabaseConnection()->prefix();
47
    }
48
49
    /**
50
     * Tears down the fixture, for example, closes a network connection.
51
     * This method is called after a test is executed.
52
     */
53
    protected function tearDown(): void
54
    {
55
    }
56
57
    protected function prefix($table)
58
    {
59
        return $this->prefix . '_' . $table;
60
    }
61
62
    public function testName()
63
    {
64
        $tableName = 'users';
65
        $actual    = $this->object->useTable($tableName);
66
        $this->assertTrue($actual);
67
68
        $tables = $this->object->dumpTables();
69
        $this->assertArrayHasKey($tableName, $tables);
70
        $this->assertEquals($this->prefix($tableName), $tables[$tableName]['name']);
71
    }
72
73
    public function testAddColumn()
74
    {
75
        $tableName = 'users';
76
        $actual    = $this->object->useTable($tableName);
77
        $this->assertTrue($actual);
78
79
        $columnName = 'not_real_column';
80
        $columnAttr = 'int NOT NULL';
81
82
        $this->object->addColumn($tableName, $columnName, $columnAttr);
83
        $queue    = $this->object->dumpQueue();
84
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` ADD COLUMN `{$columnName}` {$columnAttr}";
85
        $this->assertEquals($expected, $queue[0]);
86
    }
87
88
    public function testAddPrimaryKey()
89
    {
90
        $tableName = 'users';
91
        $actual    = $this->object->useTable($tableName);
92
        $this->assertTrue($actual);
93
94
        $columnName = 'uid';
95
96
        $this->object->addPrimaryKey($tableName, $columnName);
97
        $queue = $this->object->dumpQueue();
98
        //var_dump($queue);
99
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` ADD PRIMARY KEY(`{$columnName}`)";
100
        $this->assertEquals($expected, $queue[0]);
101
    }
102
103
    public function testAddTable()
104
    {
105
        // Remove the following lines when you implement this test.
106
        $this->markTestIncomplete(
107
            'This test has not been implemented yet.'
108
        );
109
    }
110
111
    public function testUseTable()
112
    {
113
        $actual = $this->object->useTable('users');
114
        $this->assertTrue($actual);
115
116
        $actual = $this->object->useTable('system_nosuch_table');
117
        $this->assertFalse($actual);
118
    }
119
120
    public function testGetColumnAttributes()
121
    {
122
        $tableName = 'users';
123
        $actual    = $this->object->useTable($tableName);
124
        $this->assertTrue($actual);
125
126
        $columnName = 'uid';
127
128
        $actual = $this->object->getColumnAttributes($tableName, $columnName);
129
130
        $this->assertNotFalse(stristr($actual, 'mediumint(8)'));
0 ignored issues
show
Bug introduced by
It seems like $actual can also be of type false; however, parameter $haystack of stristr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

130
        $this->assertNotFalse(stristr(/** @scrutinizer ignore-type */ $actual, 'mediumint(8)'));
Loading history...
131
        $this->assertNotFalse(stristr($actual, 'unsigned'));
132
        $this->assertNotFalse(stristr($actual, 'NOT NULL'));
133
        $this->assertNotFalse(stristr($actual, 'auto_increment'));
134
    }
135
136
    public function testGetTableIndexes()
137
    {
138
        $tableName = 'users';
139
        $this->object->useTable($tableName);
140
        $actual = $this->object->getTableIndexes($tableName);
141
        $this->assertIsArray($actual);
142
        $this->assertArrayHasKey('PRIMARY', $actual);
143
144
        $actual = $this->object->getTableIndexes('system_bogus_table_name');
145
        $this->assertFalse($actual);
146
    }
147
148
    public function testAlterColumn()
149
    {
150
        $tableName = 'users';
151
        $actual    = $this->object->useTable($tableName);
152
        $this->assertTrue($actual);
153
154
        $columnName    = 'pass';
155
        $newColumnName = 'password';
156
        $attributes    = 'varchar(255) NOT NULL DEFAULT \'\'';
157
158
        $this->object->alterColumn($tableName, $columnName, $attributes, $newColumnName);
159
        $queue = $this->object->dumpQueue();
160
        //var_dump($queue);
161
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` CHANGE COLUMN `{$columnName}` `{$newColumnName}` {$attributes} ";
162
        $this->assertEquals($expected, $queue[0]);
163
    }
164
165
    public function testCopyTable()
166
    {
167
        $tableName = 'users';
168
        $actual    = $this->object->useTable($tableName);
169
        $this->assertTrue($actual);
170
171
        $copyName = 'notsystem_user';
172
        $actual   = $this->object->useTable($copyName);
173
        $this->assertFalse($actual);
174
175
        $actual = $this->object->copyTable($tableName, $copyName, false);
176
        $this->assertTrue($actual);
177
178
        $tables = $this->object->dumpTables();
179
        $this->assertEquals($tables[$tableName]['columns'], $tables[$copyName]['columns']);
180
        $this->assertEquals($tables[$tableName]['keys'], $tables[$copyName]['keys']);
181
    }
182
183
    public function testCreateIndex()
184
    {
185
        $tableName = 'users';
186
        $actual    = $this->object->useTable($tableName);
187
        $this->assertTrue($actual);
188
189
        $columnName = 'actkey';
190
        $indexName  = 'user_actkey';
191
192
        $this->object->addIndex($indexName, $tableName, $columnName);
193
        $queue = $this->object->dumpQueue();
194
195
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` ADD INDEX `{$indexName}` (`{$columnName}`)";
196
        $this->assertEquals($expected, $queue[0]);
197
    }
198
199
    public function testDropColumn()
200
    {
201
        $tableName = 'users';
202
        $actual    = $this->object->useTable($tableName);
203
        $this->assertTrue($actual);
204
205
        $columnName = 'actkey';
206
207
        $this->object->dropColumn($tableName, $columnName);
208
        $queue = $this->object->dumpQueue();
209
210
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` DROP COLUMN `{$columnName}`";
211
        $this->assertEquals($expected, $queue[0]);
212
    }
213
214
    public function testDropIndex()
215
    {
216
        $tableName = 'users';
217
        $actual    = $this->object->useTable($tableName);
218
        $this->assertTrue($actual);
219
220
        $indexName = 'blahblah';
221
222
        $this->object->dropIndex($indexName, $tableName);
223
        $queue = $this->object->dumpQueue();
224
225
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` DROP INDEX `{$indexName}`";
226
        $this->assertEquals($expected, $queue[0]);
227
    }
228
229
    public function testDropIndexes()
230
    {
231
        // Remove the following lines when you implement this test.
232
        $this->markTestIncomplete(
233
            'This test has not been implemented yet.'
234
        );
235
    }
236
237
    public function testDropPrimaryKey()
238
    {
239
        $tableName = 'users';
240
        $actual    = $this->object->useTable($tableName);
241
        $this->assertTrue($actual);
242
243
        $this->object->dropPrimaryKey($tableName);
244
        $queue = $this->object->dumpQueue();
245
246
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` DROP PRIMARY KEY ";
247
        $this->assertEquals($expected, $queue[0]);
248
    }
249
250
    public function testDropTable()
251
    {
252
        $tableName = 'users';
253
        $actual    = $this->object->useTable($tableName);
254
        $this->assertTrue($actual);
255
256
        $this->object->dropTable($tableName);
257
        $queue = $this->object->dumpQueue();
258
259
        $expected = "DROP TABLE `{$this->prefix}_{$tableName}` ";
260
        $this->assertEquals($expected, $queue[0]);
261
    }
262
263
    public function testRenameTable()
264
    {
265
        $tableName = 'users';
266
        $actual    = $this->object->useTable($tableName);
267
        $this->assertTrue($actual);
268
269
        $newName = 'system_abuser';
270
        $this->object->renameTable($tableName, $newName);
271
        $queue = $this->object->dumpQueue();
272
273
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` RENAME TO `{$this->prefix}_{$newName}`";
274
        $this->assertEquals($expected, $queue[0]);
275
    }
276
277
    public function testSetTableOptions()
278
    {
279
        $tableName = 'users';
280
        $actual    = $this->object->useTable($tableName);
281
        $this->assertTrue($actual);
282
283
        $options = 'ENGINE=MEMORY DEFAULT CHARSET=utf8;';
284
        $this->object->setTableOptions($tableName, $options);
285
        $queue = $this->object->dumpQueue();
286
287
        $expected = "ALTER TABLE `{$this->prefix}_{$tableName}` {$options} ";
288
        $this->assertEquals($expected, $queue[0]);
289
    }
290
291
    public function testExecuteQueue()
292
    {
293
        // Remove the following lines when you implement this test.
294
        $this->markTestIncomplete(
295
            'This test has not been implemented yet.'
296
        );
297
    }
298
299
    public function testDelete()
300
    {
301
        // Remove the following lines when you implement this test.
302
        $this->markTestIncomplete(
303
            'This test has not been implemented yet.'
304
        );
305
    }
306
307
    public function testInsert()
308
    {
309
        // Remove the following lines when you implement this test.
310
        $this->markTestIncomplete(
311
            'This test has not been implemented yet.'
312
        );
313
    }
314
315
    public function testUpdate()
316
    {
317
        // Remove the following lines when you implement this test.
318
        $this->markTestIncomplete(
319
            'This test has not been implemented yet.'
320
        );
321
    }
322
323
    public function testTruncate()
324
    {
325
        // Remove the following lines when you implement this test.
326
        $this->markTestIncomplete(
327
            'This test has not been implemented yet.'
328
        );
329
    }
330
331
    public function testRenderTableCreate()
332
    {
333
        // Remove the following lines when you implement this test.
334
        $this->markTestIncomplete(
335
            'This test has not been implemented yet.'
336
        );
337
    }
338
339
    public function testGetLastError()
340
    {
341
        // Remove the following lines when you implement this test.
342
        $this->markTestIncomplete(
343
            'This test has not been implemented yet.'
344
        );
345
    }
346
347
    public function testGetLastErrNo()
348
    {
349
        // Remove the following lines when you implement this test.
350
        $this->markTestIncomplete(
351
            'This test has not been implemented yet.'
352
        );
353
    }
354
355
    public function testDumpTables()
356
    {
357
        // Remove the following lines when you implement this test.
358
        $this->markTestIncomplete(
359
            'This test has not been implemented yet.'
360
        );
361
    }
362
363
    public function testAddToQueue()
364
    {
365
        $this->object->resetQueue();
366
        $queue = $this->object->dumpQueue();
367
        $this->assertIsArray($queue);
368
        $this->assertEmpty($queue);
369
370
        $expected = 'SELECT * FROM TEST.DUMMY';
371
        $this->object->addToQueue($expected);
372
373
        $queue = $this->object->dumpQueue();
374
        $this->assertIsArray($queue);
375
        $this->assertSame(1, count($queue));
376
        $this->assertEquals($expected, reset($queue));
377
378
        $this->object->resetQueue();
379
        $queue = $this->object->dumpQueue();
380
        $this->assertIsArray($queue);
381
        $this->assertEmpty($queue);
382
    }
383
}
384