summaryrefslogtreecommitdiff
path: root/tests/unit/Testing/Data/Analysis/TSimpleDbStatementAnalysisTest.php
blob: 09ab0f48539ef129010340248a30285fe87bb2d5 (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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
require_once dirname(__FILE__).'/../../../phpunit.php';

Prado::using('System.Testing.Data.Analysis.TSimpleDbStatementAnalysis');

/**
 * @package System.Testing.Data.Analysis
 */
class TSimpleDbStatementAnalysisTest extends PHPUnit_Framework_TestCase
{
	private $analyser;

	public function setUp()
	{
		$this->analyser = new TSimpleDbStatementAnalysis();
	}

	public function tearDown()
	{
		$this->analyser = null;
	}

	public function testDriverName()
	{
		self::assertNull($this->analyser->getDriverName());

		$this->analyser->setDriverName('mysql');
		self::assertEquals('mysql', $this->analyser->getDriverName());

		$this->analyser->setDriverName('mssql');
		self::assertEquals('mssql', $this->analyser->getDriverName());

		$this->analyser->setDriverName(null);
		self::assertNull($this->analyser->getDriverName());
	}

	public function testClassificationAnalysisDDL()
	{
		$parameter = new TDbStatementAnalysisParameter('CREATE DATABASE `prado_system_data_sqlmap` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
		self::assertEquals(TDbStatementClassification::DDL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('DROP TABLE IF EXISTS `dynamicparametertest1`');
		self::assertEquals(TDbStatementClassification::DDL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('
			CREATE TABLE `dynamicparametertest1` (
				`testname` varchar(50) NOT NULL,
				`teststring` varchar(50) NOT NULL,
				`testinteger` int(11) NOT NULL
			) ENGINE=MyISAM DEFAULT CHARSET=utf8
		');
		self::assertEquals(TDbStatementClassification::DDL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('
			CREATE TABLE `tab3`
			/* 1 multiline comment in one line */
			SELECT
				t1.*,
				t2.`foo` AS `bar`
			FROM
				# 1 single line shell comment
				`tab1` t1
				# 2 single line shell comment
				RIGHT JOIN `tab2` t2 ON (
					t2.tab1_id=t1.tab1_ref
					AND
					t2.`disabled` IS NULL
					AND
					(t2.`flags`&?)=?
				)
			-- 1 single line comment
			WHERE
			/*
				2 multiline comment
				in two lines
			*/
				t1.`idx`=?
				AND
			-- 2 single line comment
				t1.`disabled`IS NULL
			GROUP BY
				t2.`foo`
			HAVING
				t2.tab1_id=1,
				t2.disabled IS NULL
			ORDER BY
				`bar` DESC
		');
		self::assertEquals(TDbStatementClassification::DDL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('DROP TABLE `tab3`');
		self::assertEquals(TDbStatementClassification::DDL, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisDML()
	{
		$parameter = new TDbStatementAnalysisParameter('TRUNCATE TABLE `dynamicparametertest1`');
		self::assertEquals(TDbStatementClassification::DML, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('
			UPDATE `dynamicparametertest1` SET
				`testinteger`=FLOOR(7 + (RAND() * 5))
			WHERE
				`testname` IN(
					SELECT `testname` FROM `dynamicparametertest2`
				)
		');
		self::assertEquals(TDbStatementClassification::DML, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('
			INSERT INTO `tab3`
			/* 1 multiline comment in one line */
			SELECT
				t1.*,
				t2.`foo` AS `bar`
			FROM
				# 1 single line shell comment
				`tab1` t1
				# 2 single line shell comment
				RIGHT JOIN `tab2` t2 ON (
					t2.tab1_id=t1.tab1_ref
					AND
					t2.`disabled` IS NULL
					AND
					(t2.`flags`&?)=?
				)
			-- 1 single line comment
			WHERE
			/*
				2 multiline comment
				in two lines
			*/
				t1.`idx`=?
				AND
			-- 2 single line comment
				t1.`disabled`IS NULL
			GROUP BY
				t2.`foo`
			HAVING
				t2.tab1_id=1,
				t2.disabled IS NULL
			ORDER BY
				`bar` DESC
		');
		self::assertEquals(TDbStatementClassification::DML, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisSQL()
	{
		$parameter = new TDbStatementAnalysisParameter('
			/* 1 multiline comment in one line */
			SELECT
				t1.*,
				t2.`foo` AS `bar`
			FROM
				# 1 single line shell comment
				`tab1` t1
				# 2 single line shell comment
				RIGHT JOIN `tab2` t2 ON (
					t2.tab1_id=t1.tab1_ref
					AND
					t2.`disabled` IS NULL
					AND
					(t2.`flags`&?)=?
				)
			-- 1 single line comment
			WHERE
			/*
				2 multiline comment
				in two lines
			*/
				t1.`idx`=?
				AND
			-- 2 single line comment
				t1.`disabled`IS NULL
			GROUP BY
				t2.`foo`
			HAVING
				t2.tab1_id=1,
				t2.disabled IS NULL
			ORDER BY
				`bar` DESC
		');
		self::assertEquals(TDbStatementClassification::SQL, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisDCL()
	{
		$parameter = new TDbStatementAnalysisParameter('
			GRANT ALL ON `prado_system_data_sqlmap`.*
				TO "prado_unitest"@"localhost"
				IDENTIFIED BY "prado_system_data_sqlmap_unitest"');
		self::assertEquals(TDbStatementClassification::DCL, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisTCL()
	{
		$parameter = new TDbStatementAnalysisParameter('START TRANSACTION');
		self::assertEquals(TDbStatementClassification::TCL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('BEGIN');
		self::assertEquals(TDbStatementClassification::TCL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('COMMIT');
		self::assertEquals(TDbStatementClassification::TCL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('RELEASE SAVEPOINT');
		self::assertEquals(TDbStatementClassification::TCL, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('XA START');
		self::assertEquals(TDbStatementClassification::TCL, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisUNKNOWN()
	{
		$parameter = new TDbStatementAnalysisParameter('CALL `sp_my_storedprocedure`("foobar")');
		self::assertEquals(TDbStatementClassification::UNKNOWN, $this->analyser->getClassificationAnalysis($parameter));
	}

	public function testClassificationAnalysisCONTEXT()
	{
		$parameter = new TDbStatementAnalysisParameter('SET NAMES "utf8"');
		self::assertEquals(TDbStatementClassification::CONTEXT, $this->analyser->getClassificationAnalysis($parameter));

		$parameter = new TDbStatementAnalysisParameter('USE `prado_system_data_sqlmap`');
		self::assertEquals(TDbStatementClassification::CONTEXT, $this->analyser->getClassificationAnalysis($parameter));
	}
}
?>