Skip to content

Commit 2e7a12c

Browse files
committed
Allow regenerating expected formatter outputs
1 parent 1f1bca0 commit 2e7a12c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

tests/SqlFormatterTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use function defined;
1919
use function explode;
2020
use function file_get_contents;
21+
use function file_put_contents;
22+
use function getenv;
2123
use function implode;
2224
use function pack;
2325
use function rtrim;
@@ -27,6 +29,21 @@ final class SqlFormatterTest extends TestCase
2729
{
2830
private SqlFormatter $formatter;
2931

32+
public static function setUpBeforeClass(): void
33+
{
34+
if (! getenv('UPDATE_EXPECTED')) {
35+
return;
36+
}
37+
38+
$formatter = new SqlFormatter(new HtmlHighlighter());
39+
40+
self::updateExpected('format-highlight.html', [$formatter, 'format']);
41+
self::updateExpected('format.txt', [new SqlFormatter(new NullHighlighter()), 'format']);
42+
self::updateExpected('highlight.html', [$formatter, 'highlight']);
43+
self::updateExpected('clihighlight.txt', [new SqlFormatter(new CliHighlighter()), 'format']);
44+
self::updateExpected('compress.txt', [$formatter, 'compress']);
45+
}
46+
3047
protected function setUp(): void
3148
{
3249
// Force SqlFormatter to run in non-CLI mode for tests
@@ -116,7 +133,7 @@ private static function fileDataProvider(string $file): Generator
116133
assert($contents !== false);
117134
$formatHighlightData = explode("\n---\n", rtrim($contents, "\n"));
118135
$sqlData = self::fileSqlData();
119-
if (count($formatHighlightData) !== count($sqlData)) {
136+
if (count($formatHighlightData) !== count($sqlData) && ! getenv('UPDATE_EXPECTED')) {
120137
throw new UnexpectedValueException(sprintf(
121138
'"%s" (%d sections) and sql.sql (%d sections) should have the same number of sections',
122139
$file,
@@ -175,4 +192,15 @@ public static function highlightData(): Generator
175192
{
176193
return self::fileDataProvider('highlight.html');
177194
}
195+
196+
private static function updateExpected(string $filename, callable $highlight): void
197+
{
198+
$data = [];
199+
200+
foreach (self::fileSqlData() as $sql) {
201+
$data[] = rtrim($highlight($sql), "\n");
202+
}
203+
204+
file_put_contents(__DIR__ . '/' . $filename, implode("\n---\n", $data) . "\n");
205+
}
178206
}

0 commit comments

Comments
 (0)