1818use function defined ;
1919use function explode ;
2020use function file_get_contents ;
21+ use function file_put_contents ;
22+ use function getenv ;
2123use function implode ;
2224use function pack ;
2325use 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