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.
Completed
Push — master ( 617de4...b48bec )
by Burhan
01:53
created

CreateTableTest::testAlterEngineDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace Graze\Morphism\Parse;
4
5
use Exception;
6
use Graze\Morphism\Test\Parse\TestCase;
7
8
class CreateTableTest extends TestCase
9
{
10
    public function testConstructor()
11
    {
12
        $collation = new CollationInfo();
13
        $table = new CreateTable($collation);
14
        $this->assertThat($table, $this->isInstanceOf(__NAMESPACE__ . '\CreateTable'));
15
    }
16
17
    public function testSetDefaultEngine()
18
    {
19
        $table = new CreateTable(new CollationInfo());
20
        $table->setDefaultEngine('InnoDB');
21
        // we're really just checking no exception got thrown
22
        $this->assertTrue(true);
23
    }
24
25
    /**
26
     * @dataProvider providerParse
27
     * @param string $text
28
     * @param string $expected
29
     * @throws Exception
30
     */
31
    public function testParse($text, $expected)
32
    {
33
        $stream = $this->makeStream($text);
34
        $collation = new CollationInfo();
35
        $table = new CreateTable($collation);
36
        $table->setDefaultEngine('InnoDB');
37
38
        $threw = null;
39
        try {
40
            $table->parse($stream);
41
        } catch (Exception $e) {
42
            $threw = $e;
43
        }
44
        if (preg_match('/^exception/i', $expected)) {
45
            if (!preg_match('/^exception\\s+(\\S+)\s+"(.*)"/i', $expected, $pregMatch)) {
46
                throw new Exception("garbled exception specification: $expected");
47
            }
48
            list(, $expectedExceptionType, $expectedMessageRegex) = $pregMatch;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $expectedExceptionType exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
49
            if (is_null($threw)) {
50
                $this->fail("expected an $expectedExceptionType exception, but none was thrown");
51
            } else {
52
                $this->assertInstanceOf($expectedExceptionType, $threw, "wrong exception type thrown");
53
                $this->assertRegExp("/$expectedMessageRegex/", $e->getMessage(), "wrong exception message");
0 ignored issues
show
Bug introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
54
            }
55
        } elseif (is_null($threw)) {
56
            $ddl = $table->getDDL();
57
            $actual = $ddl[0] . ';';
58
            $this->assertCount(1, $ddl);
59
            $this->assertSame(
60
                trim(preg_replace('/\s+/', ' ', $expected)),
61
                trim(preg_replace('/\s+/', ' ', $actual))
62
            );
63
        } else {
64
            $this->fail("Unexpected exception " . get_class($threw) . ": " . $threw->getMessage());
65
        }
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function providerParse()
72
    {
73
        $tests = [];
74
75
        foreach ([
76
            'simpleCreateTable.sql',
77
            'default.sql',
78
            'primaryKey.sql',
79
            'nonUniqueIndex.sql',
80
            'fullTextIndex.sql',
81
            'uniqueIndex.sql',
82
            'foreignKey.sql',
83
            'indexes.sql',
84
            'timestamp.sql',
85
        ] as $file) {
86
            $path = __DIR__ . '/sql/' . $file;
87
            $sql = @file_get_contents(__DIR__ . '/sql/' . $file);
88
            if ($sql === false) {
89
                $this->fail("could not open $path");
90
            }
91 View Code Duplication
            foreach (preg_split('/^-- test .*$/m', $sql) as $pair) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
                if (trim($pair) != '') {
93
                    list($text, $expected) = preg_split('/(?<=;)/', $pair);
94
                    $tests[] = [
95
                        trim($text),
96
                        trim($expected)
97
                    ];
98
                }
99
            }
100
        }
101
102
        return $tests;
103
    }
104
105
    /**
106
     * @param string $firstTableText
107
     * @param string $secondTableText
108
     * @param string $expected
109
     * @dataProvider diffProvider
110
     */
111 View Code Duplication
    public function testDiff($firstTableText, $secondTableText, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $collation = new CollationInfo();
114
115
        $firstStream = $this->makeStream($firstTableText);
116
        $firstTable = new CreateTable($collation);
117
        $firstTable->setDefaultEngine('InnoDB');
118
        $firstTable->parse($firstStream);
119
120
        $secondStream = $this->makeStream($secondTableText);
121
        $secondTable = new CreateTable($collation);
122
        $secondTable->setDefaultEngine('InnoDB');
123
        $secondTable->parse($secondStream);
124
125
        $diff = $firstTable->diff($secondTable);
126
127
        $this->assertEquals($expected == "" ? [] : [$expected], $diff);
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function diffProvider()
134
    {
135
        $tests = [];
136
137
        foreach ([
138
                    'columns.sql',
139
                    'indexes.sql',
140
                    'simpleDiff.sql'
141
                 ] as $file) {
142
            $path = __DIR__ . '/sql/diff/' . $file;
143
            $sql = @file_get_contents($path);
144
            if ($sql === false) {
145
                $this->fail("could not open $path");
146
            }
147 View Code Duplication
            foreach (preg_split('/^-- test .*$/m', $sql) as $pair) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
                if (trim($pair) != '') {
149
                    list($firstText, $secondText, $expected) = preg_split('/(?<=;)/', $pair);
150
                    $tests[] = [
151
                        trim($firstText),
152
                        trim($secondText),
153
                        trim($expected)
154
                    ];
155
                }
156
            }
157
        }
158
159
        return $tests;
160
    }
161
162
    /**
163
     * Test that the "alterEngine" flag works.
164
     * @dataProvider alterEngineProvider
165
     * @param array $flags
166
     * @param string $expected
167
     */
168 View Code Duplication
    public function testAlterEngineDiff(array $flags, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $sql = 'create table t (a int)';
171
        $collation = new CollationInfo();
172
173
        $firstStream = $this->makeStream($sql);
174
        $firstTable = new CreateTable($collation);
175
        $firstTable->setDefaultEngine('InnoDb');
176
        $firstTable->parse($firstStream);
177
178
        $secondStream = $this->makeStream($sql);
179
        $secondTable = new CreateTable($collation);
180
        $secondTable->setDefaultEngine('MyISAM');
181
        $secondTable->parse($secondStream);
182
183
        $diff = $firstTable->diff($secondTable, $flags);
184
185
        $this->assertEquals($expected, $diff);
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function alterEngineProvider()
192
    {
193
        return [
194
            // [ alter engine flag, expected diff ]
195
            [ [ 'alterEngine' => true ],  ["ALTER TABLE `t`\nENGINE=MyISAM"] ],
196
            [ [ 'alterEngine' => false ], []                                 ],
197
            [ [],                         ["ALTER TABLE `t`\nENGINE=MyISAM"] ],
198
        ];
199
    }
200
}
201