Skip to content

Commit c33f7c1

Browse files
Merge pull request #1 from keboola/najlos-trailing-comment
fix: trailing open block comment
2 parents 51c1cc4 + da5373d commit c33f7c1

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
language: php
22
php:
33
- 5.6
4-
- 5.5
5-
- 5.4
6-
- 5.3
7-
- hhvm
84

95
script: phpunit --coverage-text

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
ini_set('display_errors', true);
4+
ini_set('error_reporting', E_ALL);

lib/SqlFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ protected static function getNextToken($string, $previous = null)
214214
}
215215

216216
// Comment
217-
if ($string[0] === '#' || (isset($string[1])&&($string[0]==='-'&&$string[1]==='-') || ($string[0]==='/'&&$string[1]==='*'))) {
217+
if ($string[0] === '#' || (isset($string[1])&&($string[0]==='-'&&$string[1]==='-') || ($string[0]==='/'&&isset($string[1])&&$string[1]==='*'))) {
218218
// Comment until end of line
219219
if ($string[0] === '-' || $string[0] === '#') {
220220
$last = strpos($string, "\n");

phpunit.xml.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit colors="true">
3+
<phpunit
4+
colors="true"
5+
bootstrap="bootstrap.php">
46
<testsuites>
57
<testsuite name="SqlFormatter">
68
<directory suffix="Test.php">./tests</directory>

tests/SqlFormatterTest.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class SqlFormatterTest extends PHPUnit_Framework_TestCase {
88
protected $sqlData;
9-
9+
1010
/**
1111
* @dataProvider formatHighlightData
1212
*/
@@ -54,43 +54,43 @@ function testCliHighlight($sql, $html) {
5454
function testCompress($sql, $html) {
5555
$this->assertEquals(trim($html), trim(SqlFormatter::compress($sql)));
5656
}
57-
57+
5858
function testUsePre() {
5959
SqlFormatter::$use_pre = false;
6060
$actual = SqlFormatter::highlight("test");
6161
$expected = '<span style="color: #333;">test</span>';
6262
$this->assertEquals($actual,$expected);
63-
63+
6464
SqlFormatter::$use_pre = true;
6565
$actual = SqlFormatter::highlight("test");
6666
$expected = '<pre style="color: black; background-color: white;"><span style="color: #333;">test</span></pre>';
6767
$this->assertEquals($actual,$expected);
6868
}
69-
69+
7070
function testSplitQuery() {
7171
$expected = array(
7272
"SELECT 'test' FROM MyTable;",
7373
"SELECT Column2 FROM SomeOther Table WHERE (test = true);"
7474
);
75-
75+
7676
$actual = SqlFormatter::splitQuery(implode(';',$expected));
77-
77+
7878
$this->assertEquals($expected, $actual);
7979
}
80-
80+
8181
function testSplitQueryEmpty() {
8282
$sql = "SELECT 1;SELECT 2;\n-- This is a comment\n;SELECT 3";
8383
$expected = array("SELECT 1;","SELECT 2;","SELECT 3");
8484
$actual = SqlFormatter::splitQuery($sql);
85-
85+
8686
$this->assertEquals($expected, $actual);
8787
}
88-
88+
8989
function testRemoveComments() {
9090
$expected = SqlFormatter::format("SELECT\n * FROM\n MyTable",false);
9191
$sql = "/* this is a comment */SELECT#This is another comment\n * FROM-- One final comment\n MyTable";
9292
$actual = SqlFormatter::removeComments($sql);
93-
93+
9494
$this->assertEquals($expected, $actual);
9595
}
9696

@@ -100,120 +100,127 @@ function testRemoveOpenComments() {
100100

101101
$this->assertEquals("", $actual);
102102
}
103-
103+
104+
function testTrailingClosingComments() {
105+
$sql = "SELECT 1 */";
106+
$actual = SqlFormatter::removeComments($sql);
107+
108+
$this->assertEquals("SELECT \n 1 */", $actual);
109+
}
110+
104111
function testCacheStats() {
105112
$stats = SqlFormatter::getCacheStats();
106113
$this->assertGreaterThan(1,$stats['hits']);
107114
}
108-
115+
109116
function formatHighlightData() {
110117
$formatHighlightData = explode("\n\n",file_get_contents(__DIR__."/format-highlight.html"));
111118
$sqlData = $this->sqlData();
112-
119+
113120
$return = array();
114121
foreach($formatHighlightData as $i=>$data) {
115122
$return[] = array(
116123
$sqlData[$i],
117124
$data
118125
);
119126
}
120-
127+
121128
return $return;
122129
}
123-
130+
124131
function highlightCliData() {
125132
$clidata = explode("\n\n",file_get_contents(__DIR__."/clihighlight.html"));
126133
$sqlData = $this->sqlData();
127-
134+
128135
$return = array();
129136
foreach($clidata as $i=>$data) {
130137
$return[] = array(
131138
$sqlData[$i],
132139
$data
133140
);
134141
}
135-
142+
136143
return $return;
137144
}
138-
145+
139146
function formatData() {
140147
$formatData = explode("\n\n",file_get_contents(__DIR__."/format.html"));
141148
$sqlData = $this->sqlData();
142-
149+
143150
$return = array();
144151
foreach($formatData as $i=>$data) {
145152
$return[] = array(
146153
$sqlData[$i],
147154
$data
148155
);
149156
}
150-
157+
151158
return $return;
152159
}
153-
160+
154161
function compressData() {
155162
$compressData = explode("\n\n",file_get_contents(__DIR__."/compress.html"));
156163
$sqlData = $this->sqlData();
157-
164+
158165
$return = array();
159166
foreach($compressData as $i=>$data) {
160167
$return[] = array(
161168
$sqlData[$i],
162169
$data
163170
);
164171
}
165-
172+
166173
return $return;
167174
}
168-
175+
169176
function highlightData() {
170177
$highlightData = explode("\n\n",file_get_contents(__DIR__."/highlight.html"));
171178
$sqlData = $this->sqlData();
172-
179+
173180
$return = array();
174181
foreach($highlightData as $i=>$data) {
175182
$return[] = array(
176183
$sqlData[$i],
177184
$data
178185
);
179186
}
180-
187+
181188
return $return;
182189
}
183-
184-
185-
190+
191+
192+
186193
function sqlData() {
187194
if(!$this->sqlData) {
188195
$this->sqlData = explode("\n\n",file_get_contents(__DIR__."/sql.sql"));
189196
}
190-
197+
191198
/**
192199
$formatHighlight = array();
193200
$highlight = array();
194201
$format = array();
195202
$compress = array();
196203
$clihighlight = array();
197-
204+
198205
foreach($this->sqlData as $sql) {
199206
$formatHighlight[] = trim(SqlFormatter::format($sql));
200207
$highlight[] = trim(SqlFormatter::highlight($sql));
201208
$format[] = trim(SqlFormatter::format($sql, false));
202209
$compress[] = trim(SqlFormatter::compress($sql));
203-
210+
204211
SqlFormatter::$cli = true;
205212
$clihighlight[] = trim(SqlFormatter::format($sql));
206213
SqlFormatter::$cli = false;
207214
}
208-
215+
209216
file_put_contents(__DIR__."/format-highlight.html", implode("\n\n",$formatHighlight));
210217
file_put_contents(__DIR__."/highlight.html", implode("\n\n",$highlight));
211218
file_put_contents(__DIR__."/format.html", implode("\n\n",$format));
212219
file_put_contents(__DIR__."/compress.html", implode("\n\n",$compress));
213220
file_put_contents(__DIR__."/clihighlight.html", implode("\n\n",$clihighlight));
214221
/**/
215-
222+
216223
return $this->sqlData;
217224
}
218-
225+
219226
}

0 commit comments

Comments
 (0)