-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiQueryParserTestCase.php
More file actions
259 lines (215 loc) · 6.95 KB
/
Copy pathMultiQueryParserTestCase.php
File metadata and controls
259 lines (215 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php declare(strict_types = 1);
namespace Nextras\MultiQueryParser;
use LogicException;
use Nextras\MultiQueryParser\Exception\RuntimeException;
use Tester\Assert;
use Tester\TestCase;
abstract class MultiQueryParserTestCase extends TestCase
{
abstract protected function createParser(bool $preserveLeadingComments = false): IMultiQueryParser;
/**
* Returns the path to the SQL data file for file-based tests.
*/
abstract protected function getDataFilePath(): string;
/**
* Returns the expected number of queries in the SQL data file.
*/
abstract protected function getExpectedFileQueryCount(): int;
/**
* @return list<array{string, list<string>}>
*/
abstract protected function provideSuperfluousSemicolonsData(): array;
/**
* @return list<array{string, list<string>}>
*/
abstract protected function provideEdgeCasesData(): array;
/**
* @return list<array{list<string>, list<string>}>
*/
abstract protected function provideChunkBoundaryData(): array;
/**
* @dataProvider provideSuperfluousSemicolonsData
* @param list<string> $expectedQueries
*/
public function testSuperfluousSemicolons(string $content, array $expectedQueries): void
{
$parser = $this->createParser();
$queries = iterator_to_array($parser->parseString($content));
Assert::same($expectedQueries, $queries);
}
/**
* @dataProvider provideEdgeCasesData
* @param list<string> $expectedQueries
*/
public function testEdgeCases(string $content, array $expectedQueries): void
{
$parser = $this->createParser();
$queries = iterator_to_array($parser->parseString($content));
Assert::same($expectedQueries, $queries);
}
/**
* @dataProvider provideChunkBoundaryData
* @param list<string> $chunks
* @param list<string> $expectedQueries
*/
public function testChunkBoundary(array $chunks, array $expectedQueries): void
{
$parser = $this->createParser();
$queries = iterator_to_array($parser->parseStringStream(new \ArrayIterator($chunks)));
Assert::same($expectedQueries, $queries);
}
/**
* Dialect-agnostic leading-comment cases (line and block comments), shared by every
* parser. Dialect-specific comment styles are tested in the subclasses.
*
* @dataProvider provideCommonPreserveLeadingCommentsData
* @param list<string> $expectedQueries
*/
public function testPreserveLeadingComments(string $content, array $expectedQueries): void
{
$parser = $this->createParser(preserveLeadingComments: true);
$queries = iterator_to_array($parser->parseString($content));
Assert::same($expectedQueries, $queries);
}
/**
* The restructured leading-comment pattern must keep streaming chunk-safe:
* every two-chunk split of the input must reproduce the whole-string result.
*/
public function testPreserveLeadingCommentsChunkBoundary(): void
{
$parser = $this->createParser(preserveLeadingComments: true);
$content = implode("\n", [
'-- header comment',
'-- second line',
'SELECT 1;',
'',
'SELECT 2;',
'/* block ; with semi */',
'SELECT 3;',
'SELECT 4; -- trailing',
'-- leading before 5',
'SELECT 5;',
]);
$expected = iterator_to_array($parser->parseString($content));
$len = strlen($content);
for ($i = 0; $i <= $len; $i++) {
$chunks = [substr($content, 0, $i), substr($content, $i)];
$queries = iterator_to_array($parser->parseStringStream(new \ArrayIterator($chunks)));
Assert::same($expected, $queries, "Failed with chunk boundary at offset $i");
}
}
/**
* @return list<array{string, list<string>}>
*/
protected function provideCommonPreserveLeadingCommentsData(): array
{
return [
// A single -- comment kept as a prefix of the following query
[
"-- create the users table\nCREATE TABLE users (id INT);",
["-- create the users table\nCREATE TABLE users (id INT)"],
],
// Multiple consecutive -- comment lines
[
"-- line 1\n-- line 2\nSELECT 1;",
["-- line 1\n-- line 2\nSELECT 1"],
],
// Each query keeps only its own leading comment
[
"-- first\nSELECT 1;\n-- second\nSELECT 2;",
["-- first\nSELECT 1", "-- second\nSELECT 2"],
],
// A comment between two queries attaches to the following query
[
"SELECT 1; -- between\nSELECT 2;",
["SELECT 1", "-- between\nSELECT 2"],
],
// /* */ block comments are preserved too
[
"/* block */ SELECT 1;",
["/* block */ SELECT 1"],
],
// Mixed comment types preserve their original formatting
[
"-- a\n/* b */\nSELECT 1;",
["-- a\n/* b */\nSELECT 1"],
],
// Pure leading whitespace / blank lines before the comment are stripped
[
"\n\n-- spaced\n\nSELECT 1;",
["-- spaced\n\nSELECT 1"],
],
// Comment-only input yields nothing (no query to attach to)
["-- only a comment", []],
["-- line 1\n-- line 2\n", []],
["/* only a block */", []],
// A trailing comment after the last query (no following query) is dropped
[
"SELECT 1;\n-- trailing",
["SELECT 1"],
],
// Pure whitespace produces no leading prefix
[
"\n\nSELECT 1;\n\n",
["SELECT 1"],
],
];
}
public function testFile(): void
{
$parser = $this->createParser();
$queries = iterator_to_array($parser->parseFile($this->getDataFilePath()));
Assert::count($this->getExpectedFileQueryCount(), $queries);
}
public function testFileWithRandomizedChunking(): void
{
$content = file_get_contents($this->getDataFilePath());
if ($content === false) {
throw new LogicException('Failed to read file content');
}
$parser = $this->createParser();
$expected = iterator_to_array($parser->parseString($content));
for ($i = 0; $i < 100; $i++) {
$chunks = self::randomChunks($content);
$queries = iterator_to_array($parser->parseStringStream(new \ArrayIterator($chunks)));
Assert::same($expected, $queries, "Failed with chunk sizes: " . implode(', ', array_map('strlen', $chunks)));
}
}
public function testFileWithAllTwoChunkCombinations(): void
{
$content = file_get_contents($this->getDataFilePath());
if ($content === false) {
throw new LogicException('Failed to read file content');
}
$parser = $this->createParser();
$expected = iterator_to_array($parser->parseString($content));
$len = strlen($content);
for ($i = 0; $i <= $len; $i++) {
$chunks = [substr($content, 0, $i), substr($content, $i)];
$queries = iterator_to_array($parser->parseStringStream(new \ArrayIterator($chunks)));
Assert::same($expected, $queries, "Failed with chunk boundary at offset $i");
}
}
public function testParseFileThrowsOnNonExistentFile(): void
{
$parser = $this->createParser();
Assert::exception(function () use ($parser) {
$parser->parseFile(__DIR__ . '/../data/nonexistent.sql');
}, RuntimeException::class);
}
/**
* @return list<string>
*/
protected static function randomChunks(string $s): array
{
$chunks = [];
$offset = 0;
$len = strlen($s);
while ($offset < $len) {
$size = random_int(1, max(1, min(256, $len - $offset)));
$chunks[] = substr($s, $offset, $size);
$offset += $size;
}
return $chunks;
}
}