1<?php 2require __DIR__.'/../lib/SqlFormatter.php'; 3 4// Force SqlFormatter to run in non-CLI mode for tests 5SqlFormatter::$cli = false; 6 7class SqlFormatterTest extends PHPUnit_Framework_TestCase { 8 protected $sqlData; 9 10 /** 11 * @dataProvider formatHighlightData 12 */ 13 function testFormatHighlight($sql, $html) { 14 $this->assertEquals(trim($html), trim(SqlFormatter::format($sql))); 15 } 16 /** 17 * @dataProvider formatData 18 */ 19 function testFormat($sql, $html) { 20 $this->assertEquals(trim($html), trim(SqlFormatter::format($sql, false))); 21 } 22 /** 23 * @dataProvider highlightData 24 */ 25 function testHighlight($sql, $html) { 26 $this->assertEquals(trim($html), trim(SqlFormatter::highlight($sql))); 27 } 28 /** 29 * @dataProvider highlightCliData 30 */ 31 function testCliHighlight($sql, $html) { 32 SqlFormatter::$cli = true; 33 $this->assertEquals(trim($html), trim(SqlFormatter::format($sql))); 34 SqlFormatter::$cli = false; 35 } 36 /** 37 * @dataProvider compressData 38 */ 39 function testCompress($sql, $html) { 40 $this->assertEquals(trim($html), trim(SqlFormatter::compress($sql))); 41 } 42 43 function testUsePre() { 44 SqlFormatter::$use_pre = false; 45 $actual = SqlFormatter::highlight("test"); 46 $expected = '<span style="color: #333;">test</span>'; 47 $this->assertEquals($actual,$expected); 48 49 SqlFormatter::$use_pre = true; 50 $actual = SqlFormatter::highlight("test"); 51 $expected = '<pre style="color: black; background-color: white;"><span style="color: #333;">test</span></pre>'; 52 $this->assertEquals($actual,$expected); 53 } 54 55 function testSplitQuery() { 56 $expected = array( 57 "SELECT 'test' FROM MyTable;", 58 "SELECT Column2 FROM SomeOther Table WHERE (test = true);" 59 ); 60 61 $actual = SqlFormatter::splitQuery(implode(';',$expected)); 62 63 $this->assertEquals($expected, $actual); 64 } 65 66 function testSplitQueryEmpty() { 67 $sql = "SELECT 1;SELECT 2;\n-- This is a comment\n;SELECT 3"; 68 $expected = array("SELECT 1;","SELECT 2;","SELECT 3"); 69 $actual = SqlFormatter::splitQuery($sql); 70 71 $this->assertEquals($expected, $actual); 72 } 73 74 function testRemoveComments() { 75 $expected = SqlFormatter::format("SELECT\n * FROM\n MyTable",false); 76 $sql = "/* this is a comment */SELECT#This is another comment\n * FROM-- One final comment\n MyTable"; 77 $actual = SqlFormatter::removeComments($sql); 78 79 $this->assertEquals($expected, $actual); 80 } 81 82 function testCacheStats() { 83 $stats = SqlFormatter::getCacheStats(); 84 $this->assertGreaterThan(1,$stats['hits']); 85 } 86 87 function formatHighlightData() { 88 $formatHighlightData = explode("\n\n",file_get_contents(__DIR__."/format-highlight.html")); 89 $sqlData = $this->sqlData(); 90 91 $return = array(); 92 foreach($formatHighlightData as $i=>$data) { 93 $return[] = array( 94 $sqlData[$i], 95 $data 96 ); 97 } 98 99 return $return; 100 } 101 102 function highlightCliData() { 103 $clidata = explode("\n\n",file_get_contents(__DIR__."/clihighlight.html")); 104 $sqlData = $this->sqlData(); 105 106 $return = array(); 107 foreach($clidata as $i=>$data) { 108 $return[] = array( 109 $sqlData[$i], 110 $data 111 ); 112 } 113 114 return $return; 115 } 116 117 function formatData() { 118 $formatData = explode("\n\n",file_get_contents(__DIR__."/format.html")); 119 $sqlData = $this->sqlData(); 120 121 $return = array(); 122 foreach($formatData as $i=>$data) { 123 $return[] = array( 124 $sqlData[$i], 125 $data 126 ); 127 } 128 129 return $return; 130 } 131 132 function compressData() { 133 $compressData = explode("\n\n",file_get_contents(__DIR__."/compress.html")); 134 $sqlData = $this->sqlData(); 135 136 $return = array(); 137 foreach($compressData as $i=>$data) { 138 $return[] = array( 139 $sqlData[$i], 140 $data 141 ); 142 } 143 144 return $return; 145 } 146 147 function highlightData() { 148 $highlightData = explode("\n\n",file_get_contents(__DIR__."/highlight.html")); 149 $sqlData = $this->sqlData(); 150 151 $return = array(); 152 foreach($highlightData as $i=>$data) { 153 $return[] = array( 154 $sqlData[$i], 155 $data 156 ); 157 } 158 159 return $return; 160 } 161 162 163 164 function sqlData() { 165 if(!$this->sqlData) { 166 $this->sqlData = explode("\n\n",file_get_contents(__DIR__."/sql.sql")); 167 } 168 169 /** 170 $formatHighlight = array(); 171 $highlight = array(); 172 $format = array(); 173 $compress = array(); 174 $clihighlight = array(); 175 176 foreach($this->sqlData as $sql) { 177 $formatHighlight[] = trim(SqlFormatter::format($sql)); 178 $highlight[] = trim(SqlFormatter::highlight($sql)); 179 $format[] = trim(SqlFormatter::format($sql, false)); 180 $compress[] = trim(SqlFormatter::compress($sql)); 181 182 SqlFormatter::$cli = true; 183 $clihighlight[] = trim(SqlFormatter::format($sql)); 184 SqlFormatter::$cli = false; 185 } 186 187 file_put_contents(__DIR__."/format-highlight.html", implode("\n\n",$formatHighlight)); 188 file_put_contents(__DIR__."/highlight.html", implode("\n\n",$highlight)); 189 file_put_contents(__DIR__."/format.html", implode("\n\n",$format)); 190 file_put_contents(__DIR__."/compress.html", implode("\n\n",$compress)); 191 file_put_contents(__DIR__."/clihighlight.html", implode("\n\n",$clihighlight)); 192 /**/ 193 194 return $this->sqlData; 195 } 196 197} 198