File: SqlFormatterTest.php

package info (click to toggle)
php-sql-formatter 1.2.17-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 432 kB
  • ctags: 298
  • sloc: php: 924; sql: 221; makefile: 17
file content (197 lines) | stat: -rw-r--r-- 4,997 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
require 'SqlFormatter/autoload.php';

// Force SqlFormatter to run in non-CLI mode for tests
SqlFormatter::$cli = false;

class SqlFormatterTest extends PHPUnit_Framework_TestCase {
	protected $sqlData;
	
	/**
	 * @dataProvider formatHighlightData
	 */
	function testFormatHighlight($sql, $html) {
		$this->assertEquals(trim($html), trim(SqlFormatter::format($sql)));
	}
	/**
	 * @dataProvider formatData
	 */
	function testFormat($sql, $html) {
		$this->assertEquals(trim($html), trim(SqlFormatter::format($sql, false)));
	}
	/**
	 * @dataProvider highlightData
	 */
	function testHighlight($sql, $html) {
		$this->assertEquals(trim($html), trim(SqlFormatter::highlight($sql)));
	}
	/**
	 * @dataProvider highlightCliData
	 */
	function testCliHighlight($sql, $html) {
		SqlFormatter::$cli = true;
		$this->assertEquals(trim($html), trim(SqlFormatter::format($sql)));
		SqlFormatter::$cli = false;
	}
	/**
	 * @dataProvider compressData
	 */
	function testCompress($sql, $html) {
		$this->assertEquals(trim($html), trim(SqlFormatter::compress($sql)));
	}
	
	function testUsePre() {
		SqlFormatter::$use_pre = false;
		$actual = SqlFormatter::highlight("test");
		$expected = '<span style="color: #333;">test</span>';
		$this->assertEquals($actual,$expected);
		
		SqlFormatter::$use_pre = true;
		$actual = SqlFormatter::highlight("test");
		$expected = '<pre style="color: black; background-color: white;"><span style="color: #333;">test</span></pre>';
		$this->assertEquals($actual,$expected);
	}
	
	function testSplitQuery() {
		$expected = array(
			"SELECT 'test' FROM MyTable;",
			"SELECT Column2 FROM SomeOther Table WHERE (test = true);"
		);
		
		$actual = SqlFormatter::splitQuery(implode(';',$expected));
		
		$this->assertEquals($expected, $actual);
	}
	
	function testSplitQueryEmpty() {
		$sql = "SELECT 1;SELECT 2;\n-- This is a comment\n;SELECT 3";
		$expected = array("SELECT 1;","SELECT 2;","SELECT 3");
		$actual = SqlFormatter::splitQuery($sql);
		
		$this->assertEquals($expected, $actual);
	}
	
	function testRemoveComments() {
		$expected = SqlFormatter::format("SELECT\n * FROM\n MyTable",false);
		$sql = "/* this is a comment */SELECT#This is another comment\n * FROM-- One final comment\n MyTable";
		$actual = SqlFormatter::removeComments($sql);
		
		$this->assertEquals($expected, $actual);
	}
	
	function testCacheStats() {
		$stats = SqlFormatter::getCacheStats();
		$this->assertGreaterThan(1,$stats['hits']);
	}
	
	function formatHighlightData() {
		$formatHighlightData = explode("\n\n",file_get_contents(__DIR__."/format-highlight.html"));
		$sqlData = $this->sqlData();
		
		$return = array();
		foreach($formatHighlightData as $i=>$data) {
			$return[] = array(
				$sqlData[$i],
				$data
			);
		}
		
		return $return;
	}
	
	function highlightCliData() {
		$clidata = explode("\n\n",file_get_contents(__DIR__."/clihighlight.html"));
		$sqlData = $this->sqlData();
		
		$return = array();
		foreach($clidata as $i=>$data) {
			$return[] = array(
				$sqlData[$i],
				$data
			);
		}
		
		return $return;
	}
	
	function formatData() {
		$formatData = explode("\n\n",file_get_contents(__DIR__."/format.html"));
		$sqlData = $this->sqlData();
		
		$return = array();
		foreach($formatData as $i=>$data) {
			$return[] = array(
				$sqlData[$i],
				$data
			);
		}
		
		return $return;
	}
	
	function compressData() {
		$compressData = explode("\n\n",file_get_contents(__DIR__."/compress.html"));
		$sqlData = $this->sqlData();
		
		$return = array();
		foreach($compressData as $i=>$data) {
			$return[] = array(
				$sqlData[$i],
				$data
			);
		}
		
		return $return;
	}
	
	function highlightData() {
		$highlightData = explode("\n\n",file_get_contents(__DIR__."/highlight.html"));
		$sqlData = $this->sqlData();
		
		$return = array();
		foreach($highlightData as $i=>$data) {
			$return[] = array(
				$sqlData[$i],
				$data
			);
		}
		
		return $return;
	}
	
	
	
	function sqlData() {
		if(!$this->sqlData) {
			$this->sqlData = explode("\n\n",file_get_contents(__DIR__."/sql.sql"));
		}
		
		/**
		$formatHighlight = array();
		$highlight = array();
		$format = array();
		$compress = array();
		$clihighlight = array();
		
		foreach($this->sqlData as $sql) {
			$formatHighlight[] = trim(SqlFormatter::format($sql));
			$highlight[] = trim(SqlFormatter::highlight($sql));
			$format[] = trim(SqlFormatter::format($sql, false));
			$compress[] = trim(SqlFormatter::compress($sql));
			
			SqlFormatter::$cli = true;
			$clihighlight[] = trim(SqlFormatter::format($sql));
			SqlFormatter::$cli = false;
		}
		
		file_put_contents(__DIR__."/format-highlight.html", implode("\n\n",$formatHighlight));
		file_put_contents(__DIR__."/highlight.html", implode("\n\n",$highlight));
		file_put_contents(__DIR__."/format.html", implode("\n\n",$format));
		file_put_contents(__DIR__."/compress.html", implode("\n\n",$compress));
		file_put_contents(__DIR__."/clihighlight.html", implode("\n\n",$clihighlight));
		/**/
		
		return $this->sqlData;
	}
	
}