|
1 | 1 | <?php |
2 | | - use PHPUnit\Framework\TestCase; |
3 | | - |
4 | | - final class FullTest extends TestCase{ |
5 | | - |
6 | | - function full_test($sql, $expected){ |
7 | | - |
8 | | - $obj = new iamcal\SQLParser(); |
9 | | - $obj->parse($sql); |
10 | | - |
11 | | - $lines = array(); |
12 | | - foreach ($obj->tables as $table){ |
13 | | - $lines[] = "TABLE:{$table['name']}"; |
14 | | - $lines[] = "SQL:{$table['sql']}"; |
15 | | - foreach ($table['fields'] as $field){ |
16 | | - $lines[] = "-FIELD:{$field['name']}:{$field['type']}"; |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - $this->assertEquals($lines, $expected); |
21 | | - } |
22 | | - |
23 | | - |
24 | | - function testBasicCases(){ |
25 | | - |
26 | | - $this->full_test("CREATE TABLE table_name (a INT);\n" . |
27 | | - "-- ignored comment\n\n" . |
28 | | - "CREATE TABLE t2 (b VARCHAR)\n\n;\n", |
29 | | - array( |
30 | | - "TABLE:table_name", |
31 | | - "SQL:CREATE TABLE table_name (a INT);", |
32 | | - "-FIELD:a:INT", |
33 | | - "TABLE:t2", |
34 | | - "SQL:CREATE TABLE t2 (b VARCHAR)\n\n;", |
35 | | - "-FIELD:b:VARCHAR", |
36 | | - )); |
37 | | - } |
38 | | - } |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +final class FullTest extends TestCase |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @param $sql |
| 9 | + * @param $expected |
| 10 | + * @dataProvider parseProvider |
| 11 | + */ |
| 12 | + public function testBasicCases($sql, $expected) |
| 13 | + { |
| 14 | + $obj = new iamcal\SQLParser(); |
| 15 | + $tables = $obj->parse($sql); |
| 16 | + |
| 17 | + $this->assertEquals($expected, $tables); |
| 18 | + } |
| 19 | + |
| 20 | + public function parseProvider() |
| 21 | + { |
| 22 | + return [ |
| 23 | + [ |
| 24 | + "CREATE TABLE table_name (a INT);\n" |
| 25 | + . "-- ignored comment\n\n" |
| 26 | + . "CREATE TABLE t2 (b VARCHAR)\n\n;\n", |
| 27 | + [ |
| 28 | + 'table_name' => [ |
| 29 | + 'name' => 'table_name', |
| 30 | + 'database' => null, |
| 31 | + 'fields' => [ |
| 32 | + [ |
| 33 | + 'name' => 'a', |
| 34 | + 'type' => 'INT', |
| 35 | + ], |
| 36 | + ], |
| 37 | + 'indexes' => [], |
| 38 | + 'props' => [], |
| 39 | + 'more' => [], |
| 40 | + 'sql' => 'CREATE TABLE table_name (a INT);', |
| 41 | + ], |
| 42 | + 't2' => [ |
| 43 | + 'name' => 't2', |
| 44 | + 'database' => null, |
| 45 | + 'fields' => [ |
| 46 | + [ |
| 47 | + 'name' => 'b', |
| 48 | + 'type' => 'VARCHAR', |
| 49 | + ], |
| 50 | + ], |
| 51 | + 'indexes' => [], |
| 52 | + 'props' => [], |
| 53 | + 'more' => [], |
| 54 | + 'sql' => "CREATE TABLE t2 (b VARCHAR)\n\n;", |
| 55 | + ], |
| 56 | + |
| 57 | + ] |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'CREATE TABLE DbName.TableName ( |
| 61 | + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 62 | + errcnt INT(10) UNSIGNED NOT NULL DEFAULT \'0\', |
| 63 | + user_id INT UNSIGNED NOT NULL, |
| 64 | + photo_id INT UNSIGNED NOT NULL, |
| 65 | + place_id INT UNSIGNED NOT NULL, |
| 66 | + next_processing_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 67 | + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 68 | + PRIMARY KEY (id), |
| 69 | + KEY (place_id, next_processing_time), |
| 70 | + UNIQUE KEY (user_id, place_id, photo_id) |
| 71 | + );', |
| 72 | + [ |
| 73 | + 'DbName.TableName' => [ |
| 74 | + 'name' => 'TableName', |
| 75 | + 'database' => 'DbName', |
| 76 | + 'fields' => [ |
| 77 | + [ |
| 78 | + 'name' => 'id', |
| 79 | + 'type' => 'BIGINT', |
| 80 | + 'unsigned' => true, |
| 81 | + 'null' => false, |
| 82 | + 'auto_increment' => true, |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'name' => 'errcnt', |
| 86 | + 'type' => 'INT', |
| 87 | + 'length' => '10', |
| 88 | + 'unsigned' => true, |
| 89 | + 'null' => false, |
| 90 | + 'default' => '0', |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'name' => 'user_id', |
| 94 | + 'type' => 'INT', |
| 95 | + 'unsigned' => true, |
| 96 | + 'null' => false, |
| 97 | + ], |
| 98 | + [ |
| 99 | + 'name' => 'photo_id', |
| 100 | + 'type' => 'INT', |
| 101 | + 'unsigned' => true, |
| 102 | + 'null' => false, |
| 103 | + ], |
| 104 | + [ |
| 105 | + 'name' => 'place_id', |
| 106 | + 'type' => 'INT', |
| 107 | + 'unsigned' => true, |
| 108 | + 'null' => false, |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'name' => 'next_processing_time', |
| 112 | + 'type' => 'TIMESTAMP', |
| 113 | + 'null' => false, |
| 114 | + 'default' => 'CURRENT_TIMESTAMP', |
| 115 | + ], |
| 116 | + [ |
| 117 | + 'name' => 'created', |
| 118 | + 'type' => 'TIMESTAMP', |
| 119 | + 'null' => false, |
| 120 | + 'default' => 'CURRENT_TIMESTAMP', |
| 121 | + ], |
| 122 | + ], |
| 123 | + 'indexes' => [ |
| 124 | + [ |
| 125 | + 'type' => 'PRIMARY', |
| 126 | + 'cols' => |
| 127 | + [ |
| 128 | + [ |
| 129 | + 'name' => 'id', |
| 130 | + ], |
| 131 | + ], |
| 132 | + ], |
| 133 | + [ |
| 134 | + 'type' => 'INDEX', |
| 135 | + 'cols' => |
| 136 | + [ |
| 137 | + [ |
| 138 | + 'name' => 'place_id', |
| 139 | + ], |
| 140 | + [ |
| 141 | + 'name' => 'next_processing_time', |
| 142 | + ], |
| 143 | + ], |
| 144 | + ], |
| 145 | + [ |
| 146 | + 'type' => 'UNIQUE', |
| 147 | + 'cols' => |
| 148 | + [ |
| 149 | + [ |
| 150 | + 'name' => 'user_id', |
| 151 | + ], |
| 152 | + [ |
| 153 | + 'name' => 'place_id', |
| 154 | + ], |
| 155 | + [ |
| 156 | + 'name' => 'photo_id', |
| 157 | + ], |
| 158 | + ], |
| 159 | + ], |
| 160 | + ], |
| 161 | + 'props' => [], |
| 162 | + 'more' => [], |
| 163 | + 'sql' => 'CREATE TABLE DbName.TableName ( |
| 164 | + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 165 | + errcnt INT(10) UNSIGNED NOT NULL DEFAULT \'0\', |
| 166 | + user_id INT UNSIGNED NOT NULL, |
| 167 | + photo_id INT UNSIGNED NOT NULL, |
| 168 | + place_id INT UNSIGNED NOT NULL, |
| 169 | + next_processing_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 170 | + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 171 | + PRIMARY KEY (id), |
| 172 | + KEY (place_id, next_processing_time), |
| 173 | + UNIQUE KEY (user_id, place_id, photo_id) |
| 174 | + );', |
| 175 | + ], |
| 176 | + |
| 177 | + ] |
| 178 | + ] |
| 179 | + |
| 180 | + ]; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments