GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 79124e...156f58 )
by Burhan
02:39
created

CreateDatabaseTest::testDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Morphism\Parse;
4
5
use Graze\Morphism\Test\Parse\TestCase;
6
use Mockery;
7
8
class CreateDatabaseTest extends TestCase
9
{
10
    /**
11
     * @param string $text
12
     * @param string $expectedName
13
     * @dataProvider parseNameProvider
14
     */
15
    public function testParseName($text, $expectedName)
16
    {
17
        $stream = $this->makeStream($text);
18
        $database = new CreateDatabase(new CollationInfo());
19
        $database->parse($stream);
20
21
        $this->assertEquals($expectedName, $database->name);
22
    }
23
24
    /**
25
     * @return array
26
     */
27
    public function parseNameProvider()
28
    {
29
        return [
30
            [ 'create database foo',               'foo' ],
31
            [ 'create database if not exists bar', 'bar' ],
32
        ];
33
    }
34
35
    /**
36
     * @param string $text
37
     * @param string $expectedCharset
38
     * @dataProvider parseCharsetProvider
39
     */
40
    public function testParseCharset($text, $expectedCharset)
41
    {
42
        $stream = $this->makeStream('create database foo ' . $text);
43
        $database = new CreateDatabase(new CollationInfo());
44
        $database->parse($stream);
45
46
        if ($database->getCollation()->isSpecified()) {
47
            $charset = $database->getCollation()->getCharset();
48
        } else {
49
            $charset = null;
50
        }
51
        $this->assertEquals($expectedCharset, $charset);
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function parseCharsetProvider()
58
    {
59
        return [
60
            [ 'charset = default', null ],
61
            [ 'character set = default', null ],
62
63
            [ 'charset = utf8', 'utf8' ],
64
        ];
65
    }
66
67
    /**
68
     * @param string $text
69
     * @param string $expectedCollation
70
     * @dataProvider parseCollationProvider
71
     */
72
    public function testParseCollation($text, $expectedCollation)
73
    {
74
        $stream = $this->makeStream('create database foo ' . $text);
75
        $database = new CreateDatabase(new CollationInfo());
76
        $database->parse($stream);
77
78
        if ($database->getCollation()->isSpecified()) {
79
            $collation = $database->getCollation()->getCollation();
80
        } else {
81
            $collation = null;
82
        }
83
        $this->assertEquals($expectedCollation, $collation);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function parseCollationProvider()
90
    {
91
        return [
92
            [ 'collate = default', null ],
93
            [ 'collate = utf8_general_ci', 'utf8_general_ci' ],
94
        ];
95
    }
96
97
    /**
98
     * @expectedException \RuntimeException
99
     */
100
    public function testBadParse()
101
    {
102
        $stream = $this->makeStream('foo');
103
        $database = new CreateDatabase(new CollationInfo());
104
        $database->parse($stream);
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function badParseProvider()
111
    {
112
        return [
113
            // Not a 'create database' statement
114
            [ 'foo' ],
115
116
            // No database name
117
            [ 'create database' ],
118
119
            // invalid character set syntax
120
            [ 'create database foo default charset utf8' ],
121
122
            // invalid character set
123
            [ 'create database foo default charset = bar' ],
124
        ];
125
    }
126
127
    public function testAddTable()
128
    {
129
        $tableName = 'foo';
130
131
        $createTable = Mockery::mock(CreateTable::class);
132
        $createTable->shouldReceive('getName')->andReturn($tableName);
133
134
        $database = new CreateDatabase(Mockery::mock(CollationInfo::class));
1 ignored issue
show
Bug introduced by
Mockery::mock(Graze\Morp...e\CollationInfo::class) of type Mockery\MockInterface is incompatible with the type Graze\Morphism\Parse\CollationInfo expected by parameter $defaultCollation of Graze\Morphism\Parse\CreateDatabase::__construct(). ( Ignorable by Annotation )

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

134
        $database = new CreateDatabase(/** @scrutinizer ignore-type */ Mockery::mock(CollationInfo::class));
Loading history...
135
        $database->addTable($createTable);
1 ignored issue
show
Bug introduced by
$createTable of type Mockery\MockInterface is incompatible with the type Graze\Morphism\Parse\CreateTable expected by parameter $table of Graze\Morphism\Parse\CreateDatabase::addTable(). ( Ignorable by Annotation )

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

135
        $database->addTable(/** @scrutinizer ignore-type */ $createTable);
Loading history...
136
137
        $this->assertArrayHasKey($tableName, $database->tables);
138
    }
139
140
    /**
141
     * @expectedException \RuntimeException
142
     */
143
    public function testBadAddTable()
144
    {
145
        $createTable = new CreateTable(new CollationInfo());
146
        $database = new CreateDatabase(new CollationInfo());
147
        $database->addTable($createTable);
148
    }
149
150
    /**
151
     * @param CreateDatabase $database
152
     * @param string $sql
153
     * @dataProvider testGetDDLProvider
154
     */
155
    public function testGetDDL(CreateDatabase $database, $sql)
156
    {
157
        $ddl = $database->getDDL();
158
159
        $this->assertInternalType('array', $ddl);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

159
        /** @scrutinizer ignore-deprecated */ $this->assertInternalType('array', $ddl);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
160
        $this->assertEquals(1, count($ddl));
161
        $this->assertEquals($sql, $ddl[0]);
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function testGetDDLProvider()
168
    {
169
        $testCases = [];
170
171
        // Basic database creation with out any specific collation
172
        $database = new CreateDatabase(new CollationInfo());
173
        $database->name = 'alpha';
174
        $testCases[] = [
175
            $database,
176
            'CREATE DATABASE IF NOT EXISTS `alpha`'
177
        ];
178
179
        // Explicit charset specified
180
        $database = new CreateDatabase(new CollationInfo('utf8'));
181
        $database->name = 'beta';
182
        $testCases[] = [
183
            $database,
184
            'CREATE DATABASE IF NOT EXISTS `beta` DEFAULT CHARACTER SET utf8'
185
        ];
186
187
        // Explicit collation and charset specified
188
        $database = new CreateDatabase(new CollationInfo('utf8', 'utf8_unicode_ci'));
189
        $database->name = 'gamma';
190
        $testCases[] = [
191
            $database,
192
            'CREATE DATABASE IF NOT EXISTS `gamma` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'
193
        ];
194
195
        return $testCases;
196
    }
197
198
    /**
199
     * @expectedException \RuntimeException
200
     */
201
    public function testBadGetDDL()
202
    {
203
        $database = new CreateDatabase(new CollationInfo());
204
        $database->getDDL();
205
    }
206
207
    /**
208
     * @param CreateDatabase $db1
209
     * @param CreateDatabase $db2
210
     * @param array $flags
211
     * @param array $expected
212
     * @dataProvider diffProvider
213
     */
214
    public function testDiff(CreateDatabase $db1, CreateDatabase $db2, array $flags, array $expected)
215
    {
216
        $diff = $db1->diff($db2, $flags);
217
218
        $this->assertEquals($expected, $diff);
219
    }
220
221
    /**
222
     * @return array
223
     */
224
    public function diffProvider()
225
    {
226
        // [
227
        //    [ create database 1, create database 2, diff flags, array of statements to execute ],
228
        //    ...
229
        // ]
230
        $testCases = [];
231
232
        /** @var CollationInfo|Mockery\MockInterface $collationInfo */
233
        $collationInfo = Mockery::mock(CollationInfo::class);
234
        $collationInfo->shouldReceive('isSpecified');
235
236
        // Completely empty objects
237
        $testCases[] = [
238
            new CreateDatabase($collationInfo),
1 ignored issue
show
Bug introduced by
$collationInfo of type Mockery\MockInterface is incompatible with the type Graze\Morphism\Parse\CollationInfo expected by parameter $defaultCollation of Graze\Morphism\Parse\CreateDatabase::__construct(). ( Ignorable by Annotation )

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

238
            new CreateDatabase(/** @scrutinizer ignore-type */ $collationInfo),
Loading history...
239
            new CreateDatabase($collationInfo),
240
            [],
241
            []
242
        ];
243
244
        // Named databases
245
        // Morphism does not support renaming databases
246
        $db1 = new CreateDatabase($collationInfo);
247
        $db1->name = 'foo';
248
249
        $db2 = new CreateDatabase($collationInfo);
250
        $db2->name = 'bar';
251
252
        $testCases[] = [
253
            $db1,
254
            $db2,
255
            [],
256
            []
257
        ];
258
259
        // Table added
260
        $db1 = new CreateDatabase($collationInfo);
261
262
        /** @var CreateTable|Mockery\MockInterface $tableA */
263
        $tableA = Mockery::mock(CreateTable::class);
264
        $tableA->shouldReceive('getName')->andReturn('t');
265
        $tableA->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n  `a` int(11) DEFAULT NULL\n) ENGINE=E"]);
266
267
        $db2 = new CreateDatabase($collationInfo);
268
        $db2->addTable($tableA);
1 ignored issue
show
Bug introduced by
$tableA of type Mockery\MockInterface is incompatible with the type Graze\Morphism\Parse\CreateTable expected by parameter $table of Graze\Morphism\Parse\CreateDatabase::addTable(). ( Ignorable by Annotation )

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

268
        $db2->addTable(/** @scrutinizer ignore-type */ $tableA);
Loading history...
269
270
        $testCases[] = [
271
            $db1,
272
            $db2,
273
            [],
274
            ["CREATE TABLE `t` (\n  `a` int(11) DEFAULT NULL\n) ENGINE=E"]
275
        ];
276
277
        // Table added
278
        $db1 = new CreateDatabase($collationInfo);
279
280
        $db2 = new CreateDatabase($collationInfo);
281
        $db2->addTable($tableA);
282
283
        $testCases[] = [
284
            $db1,
285
            $db2,
286
            [],
287
            ["CREATE TABLE `t` (\n  `a` int(11) DEFAULT NULL\n) ENGINE=E"]
288
        ];
289
290
        // Table added (but ignored)
291
        $db1 = new CreateDatabase($collationInfo);
292
293
        $db2 = new CreateDatabase($collationInfo);
294
        $db2->addTable($tableA);
295
296
        $testCases[] = [
297
            $db1,
298
            $db2,
299
            ['createTable' => false],
300
            []
301
        ];
302
303
        // Table removed
304
        $db1 = new CreateDatabase($collationInfo);
305
        $db1->addTable($tableA);
306
307
        $db2 = new CreateDatabase($collationInfo);
308
309
        $testCases[] = [
310
            $db1,
311
            $db2,
312
            [],
313
            ["DROP TABLE IF EXISTS `t`"]
314
        ];
315
316
        // Table removed (but ignored)
317
        $db1 = new CreateDatabase($collationInfo);
318
        $db1->addTable($tableA);
319
320
        $db2 = new CreateDatabase($collationInfo);
321
322
        $testCases[] = [
323
            $db1,
324
            $db2,
325
            ['dropTable' => false],
326
            []
327
        ];
328
329
        // Engine changed
330
        /** @var CreateTable|Mockery\MockInterface $tableWithEngineBar */
331
        $tableWithEngineBar = Mockery::mock(CreateTable::class);
332
        $tableWithEngineBar->shouldReceive('getName')->andReturn('t');
333
        $tableWithEngineBar->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n  `a` int(11) DEFAULT NULL\n) ENGINE=BAR"]);
334
335
        /** @var CreateTable|Mockery\MockInterface $tableWithEngineFoo */
336
        $tableWithEngineFoo = Mockery::mock(CreateTable::class);
337
        $tableWithEngineFoo->shouldReceive('getName')->andReturn('t');
338
        $tableWithEngineFoo->shouldReceive('getDDL')->andReturn(["CREATE TABLE `t` (\n  `a` int(11) DEFAULT NULL\n) ENGINE=FOO"]);
339
        $tableWithEngineFoo
340
            ->shouldReceive('diff')
341
            ->with(
342
                $tableWithEngineBar,
343
                ['alterEngine' => true]
344
            )
345
            ->andReturn(["ALTER TABLE `t`\nENGINE=BAR"]);
346
347
        $db1 = new CreateDatabase($collationInfo);
348
        $db1->addTable($tableWithEngineFoo);
349
350
        $db2 = new CreateDatabase($collationInfo);
351
        $db2->addTable($tableWithEngineBar);
352
353
        $testCases[] = [
354
            $db1,
355
            $db2,
356
            [],
357
            ["ALTER TABLE `t`\nENGINE=BAR"]
358
        ];
359
360
        // Engine changed (but ignored)
361
        $db1 = new CreateDatabase($collationInfo);
362
        $db1->addTable($tableWithEngineFoo);
363
364
        $tableWithEngineFoo
365
            ->shouldReceive('diff')
1 ignored issue
show
Bug introduced by
The method shouldReceive() does not exist on Graze\Morphism\Parse\CreateTable. ( Ignorable by Annotation )

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

365
            ->/** @scrutinizer ignore-call */ 
366
              shouldReceive('diff')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
366
            ->with(
367
                $tableWithEngineBar,
368
                ['alterEngine' => false]
369
            )
370
            ->andReturn([]);
371
372
        $db2 = new CreateDatabase($collationInfo);
373
        $db2->addTable($tableWithEngineBar);
374
375
        $testCases[] = [
376
            $db1,
377
            $db2,
378
            ['alterEngine' => false],
379
            []
380
        ];
381
382
        return $testCases;
383
    }
384
}
385