graze /
morphism
| 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; |
||
| 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
Comprehensibility
Best Practice
introduced
by
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 | foreach (preg_split('/^-- test .*$/m', $sql) as $pair) { |
||
| 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 | public function testDiff($firstTableText, $secondTableText, $expected) |
||
| 112 | { |
||
| 113 | $collation = new CollationInfo(); |
||
| 114 | |||
| 115 | $firstStream = $this->makeStream($firstTableText); |
||
| 116 | $firstTable = new CreateTable($collation); |
||
| 117 | $firstTable->setDefaultEngine('InnoDB'); |
||
| 118 | $firstTable->setAddIndexForForeignKey(false); |
||
| 119 | $firstTable->parse($firstStream); |
||
| 120 | |||
| 121 | $secondStream = $this->makeStream($secondTableText); |
||
| 122 | $secondTable = new CreateTable($collation); |
||
| 123 | $secondTable->setDefaultEngine('InnoDB'); |
||
| 124 | $secondTable->parse($secondStream); |
||
| 125 | |||
| 126 | $diff = $firstTable->diff($secondTable); |
||
| 127 | |||
| 128 | $this->assertEquals($expected == "" ? [] : [$expected], $diff); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function diffProvider() |
||
| 135 | { |
||
| 136 | $tests = []; |
||
| 137 | |||
| 138 | foreach ([ |
||
| 139 | 'columns.sql', |
||
| 140 | 'foreignKeys.sql', |
||
| 141 | 'indexes.sql', |
||
| 142 | 'simpleDiff.sql' |
||
| 143 | ] as $file) { |
||
| 144 | $path = __DIR__ . '/sql/diff/' . $file; |
||
| 145 | $sql = @file_get_contents($path); |
||
| 146 | if ($sql === false) { |
||
| 147 | $this->fail("could not open $path"); |
||
| 148 | } |
||
| 149 | foreach (preg_split('/^-- test .*$/m', $sql) as $pair) { |
||
| 150 | if (trim($pair) != '') { |
||
| 151 | list($firstText, $secondText, $expected) = preg_split('/(?<=;)/', $pair); |
||
| 152 | $tests[] = [ |
||
| 153 | trim($firstText), |
||
| 154 | trim($secondText), |
||
| 155 | trim($expected) |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return $tests; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Test that the "alterEngine" flag works. |
||
| 166 | * @dataProvider alterEngineProvider |
||
| 167 | * @param array $flags |
||
| 168 | * @param string $expected |
||
| 169 | */ |
||
| 170 | public function testAlterEngineDiff(array $flags, $expected) |
||
| 171 | { |
||
| 172 | $sql = 'create table t (a int)'; |
||
| 173 | $collation = new CollationInfo(); |
||
| 174 | |||
| 175 | $firstStream = $this->makeStream($sql); |
||
| 176 | $firstTable = new CreateTable($collation); |
||
| 177 | $firstTable->setDefaultEngine('InnoDb'); |
||
| 178 | $firstTable->parse($firstStream); |
||
| 179 | |||
| 180 | $secondStream = $this->makeStream($sql); |
||
| 181 | $secondTable = new CreateTable($collation); |
||
| 182 | $secondTable->setDefaultEngine('MyISAM'); |
||
| 183 | $secondTable->parse($secondStream); |
||
| 184 | |||
| 185 | $diff = $firstTable->diff($secondTable, $flags); |
||
| 186 | |||
| 187 | $this->assertEquals($expected, $diff); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function alterEngineProvider() |
||
| 194 | { |
||
| 195 | return [ |
||
| 196 | // [ alter engine flag, expected diff ] |
||
| 197 | [ [ 'alterEngine' => true ], ["ALTER TABLE `t`\nENGINE=MyISAM"] ], |
||
| 198 | [ [ 'alterEngine' => false ], [] ], |
||
| 199 | [ [], ["ALTER TABLE `t`\nENGINE=MyISAM"] ], |
||
| 200 | ]; |
||
| 201 | } |
||
| 202 | } |
||
| 203 |