summaryrefslogtreecommitdiffstats
path: root/tests/SqlFormatterTest.php
blob: 5cf65adf9de7b2c8d755d407d3795993bcb92aa3 (plain)
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
<?php
require __DIR__.'/../lib/SqlFormatter.php';

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)));
	}
	
	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 = "SELECT\n * FROM\n MyTable";
		$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 = array_slice($return, 0, 50);
		
		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 = array_slice($return, 0, 50);
		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 = array_slice($return, 0, 50);
		return $return;
	}
	
	
	
	function sqlData() {
		if(!$this->sqlData) {
			$this->sqlData = explode("\n\n",file_get_contents(__DIR__."/sql.sql"));
		}
		
		/**
		$formatHighlight = array();
		$highlight = array();
		$format = array();
		
		foreach($this->sqlData as $sql) {
			$formatHighlight[] = trim(SqlFormatter::format($sql));
			$highlight[] = trim(SqlFormatter::highlight($sql));
			$format[] = trim(SqlFormatter::format($sql, 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));
		/**/
		
		return $this->sqlData;
	}
	
}