6
6
7
7
class SqlFormatterTest extends PHPUnit_Framework_TestCase {
8
8
protected $ sqlData ;
9
-
9
+
10
10
/**
11
11
* @dataProvider formatHighlightData
12
12
*/
@@ -54,43 +54,43 @@ function testCliHighlight($sql, $html) {
54
54
function testCompress ($ sql , $ html ) {
55
55
$ this ->assertEquals (trim ($ html ), trim (SqlFormatter::compress ($ sql )));
56
56
}
57
-
57
+
58
58
function testUsePre () {
59
59
SqlFormatter::$ use_pre = false ;
60
60
$ actual = SqlFormatter::highlight ("test " );
61
61
$ expected = '<span style="color: #333;">test</span> ' ;
62
62
$ this ->assertEquals ($ actual ,$ expected );
63
-
63
+
64
64
SqlFormatter::$ use_pre = true ;
65
65
$ actual = SqlFormatter::highlight ("test " );
66
66
$ expected = '<pre style="color: black; background-color: white;"><span style="color: #333;">test</span></pre> ' ;
67
67
$ this ->assertEquals ($ actual ,$ expected );
68
68
}
69
-
69
+
70
70
function testSplitQuery () {
71
71
$ expected = array (
72
72
"SELECT 'test' FROM MyTable; " ,
73
73
"SELECT Column2 FROM SomeOther Table WHERE (test = true); "
74
74
);
75
-
75
+
76
76
$ actual = SqlFormatter::splitQuery (implode ('; ' ,$ expected ));
77
-
77
+
78
78
$ this ->assertEquals ($ expected , $ actual );
79
79
}
80
-
80
+
81
81
function testSplitQueryEmpty () {
82
82
$ sql = "SELECT 1;SELECT 2; \n-- This is a comment \n;SELECT 3 " ;
83
83
$ expected = array ("SELECT 1; " ,"SELECT 2; " ,"SELECT 3 " );
84
84
$ actual = SqlFormatter::splitQuery ($ sql );
85
-
85
+
86
86
$ this ->assertEquals ($ expected , $ actual );
87
87
}
88
-
88
+
89
89
function testRemoveComments () {
90
90
$ expected = SqlFormatter::format ("SELECT \n * FROM \n MyTable " ,false );
91
91
$ sql = "/* this is a comment */SELECT#This is another comment \n * FROM-- One final comment \n MyTable " ;
92
92
$ actual = SqlFormatter::removeComments ($ sql );
93
-
93
+
94
94
$ this ->assertEquals ($ expected , $ actual );
95
95
}
96
96
@@ -100,120 +100,127 @@ function testRemoveOpenComments() {
100
100
101
101
$ this ->assertEquals ("" , $ actual );
102
102
}
103
-
103
+
104
+ function testTrailingClosingComments () {
105
+ $ sql = "SELECT 1 */ " ;
106
+ $ actual = SqlFormatter::removeComments ($ sql );
107
+
108
+ $ this ->assertEquals ("SELECT \n 1 */ " , $ actual );
109
+ }
110
+
104
111
function testCacheStats () {
105
112
$ stats = SqlFormatter::getCacheStats ();
106
113
$ this ->assertGreaterThan (1 ,$ stats ['hits ' ]);
107
114
}
108
-
115
+
109
116
function formatHighlightData () {
110
117
$ formatHighlightData = explode ("\n\n" ,file_get_contents (__DIR__ ."/format-highlight.html " ));
111
118
$ sqlData = $ this ->sqlData ();
112
-
119
+
113
120
$ return = array ();
114
121
foreach ($ formatHighlightData as $ i =>$ data ) {
115
122
$ return [] = array (
116
123
$ sqlData [$ i ],
117
124
$ data
118
125
);
119
126
}
120
-
127
+
121
128
return $ return ;
122
129
}
123
-
130
+
124
131
function highlightCliData () {
125
132
$ clidata = explode ("\n\n" ,file_get_contents (__DIR__ ."/clihighlight.html " ));
126
133
$ sqlData = $ this ->sqlData ();
127
-
134
+
128
135
$ return = array ();
129
136
foreach ($ clidata as $ i =>$ data ) {
130
137
$ return [] = array (
131
138
$ sqlData [$ i ],
132
139
$ data
133
140
);
134
141
}
135
-
142
+
136
143
return $ return ;
137
144
}
138
-
145
+
139
146
function formatData () {
140
147
$ formatData = explode ("\n\n" ,file_get_contents (__DIR__ ."/format.html " ));
141
148
$ sqlData = $ this ->sqlData ();
142
-
149
+
143
150
$ return = array ();
144
151
foreach ($ formatData as $ i =>$ data ) {
145
152
$ return [] = array (
146
153
$ sqlData [$ i ],
147
154
$ data
148
155
);
149
156
}
150
-
157
+
151
158
return $ return ;
152
159
}
153
-
160
+
154
161
function compressData () {
155
162
$ compressData = explode ("\n\n" ,file_get_contents (__DIR__ ."/compress.html " ));
156
163
$ sqlData = $ this ->sqlData ();
157
-
164
+
158
165
$ return = array ();
159
166
foreach ($ compressData as $ i =>$ data ) {
160
167
$ return [] = array (
161
168
$ sqlData [$ i ],
162
169
$ data
163
170
);
164
171
}
165
-
172
+
166
173
return $ return ;
167
174
}
168
-
175
+
169
176
function highlightData () {
170
177
$ highlightData = explode ("\n\n" ,file_get_contents (__DIR__ ."/highlight.html " ));
171
178
$ sqlData = $ this ->sqlData ();
172
-
179
+
173
180
$ return = array ();
174
181
foreach ($ highlightData as $ i =>$ data ) {
175
182
$ return [] = array (
176
183
$ sqlData [$ i ],
177
184
$ data
178
185
);
179
186
}
180
-
187
+
181
188
return $ return ;
182
189
}
183
-
184
-
185
-
190
+
191
+
192
+
186
193
function sqlData () {
187
194
if (!$ this ->sqlData ) {
188
195
$ this ->sqlData = explode ("\n\n" ,file_get_contents (__DIR__ ."/sql.sql " ));
189
196
}
190
-
197
+
191
198
/**
192
199
$formatHighlight = array();
193
200
$highlight = array();
194
201
$format = array();
195
202
$compress = array();
196
203
$clihighlight = array();
197
-
204
+
198
205
foreach($this->sqlData as $sql) {
199
206
$formatHighlight[] = trim(SqlFormatter::format($sql));
200
207
$highlight[] = trim(SqlFormatter::highlight($sql));
201
208
$format[] = trim(SqlFormatter::format($sql, false));
202
209
$compress[] = trim(SqlFormatter::compress($sql));
203
-
210
+
204
211
SqlFormatter::$cli = true;
205
212
$clihighlight[] = trim(SqlFormatter::format($sql));
206
213
SqlFormatter::$cli = false;
207
214
}
208
-
215
+
209
216
file_put_contents(__DIR__."/format-highlight.html", implode("\n\n",$formatHighlight));
210
217
file_put_contents(__DIR__."/highlight.html", implode("\n\n",$highlight));
211
218
file_put_contents(__DIR__."/format.html", implode("\n\n",$format));
212
219
file_put_contents(__DIR__."/compress.html", implode("\n\n",$compress));
213
220
file_put_contents(__DIR__."/clihighlight.html", implode("\n\n",$clihighlight));
214
221
/**/
215
-
222
+
216
223
return $ this ->sqlData ;
217
224
}
218
-
225
+
219
226
}
0 commit comments