summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/db/DatabaseSQLTest.php
blob: e37cd445faa6221fc33091eda4d6e0d8be10267b (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
<?php

/**
 * Test the abstract database layer
 * Using Mysql for the sql at the moment TODO
 *
 * @group Database
 */
class DatabaseSQLTest extends MediaWikiTestCase {

	public function setUp() {
		// TODO support other DBMS or find another way to do it
		if( $this->db->getType() !== 'mysql' ) {
			$this->markTestSkipped( 'No mysql database' );
		}
	}

	/**
	 * @dataProvider dataSelectSQLText
	 */
	function testSelectSQLText( $sql, $sqlText ) {
		$this->assertEquals( trim( $this->db->selectSQLText(
			isset( $sql['tables'] ) ? $sql['tables'] : array(),
			isset( $sql['fields'] ) ? $sql['fields'] : array(),
			isset( $sql['conds'] ) ? $sql['conds'] : array(),
			__METHOD__,
			isset( $sql['options'] ) ? $sql['options'] : array(),
			isset( $sql['join_conds'] ) ? $sql['join_conds'] : array()
		) ), $sqlText );
	}

	function dataSelectSQLText() {
		return array(
			array(
				array(
					'tables' => 'table',
					'fields' => array( 'field', 'alias' => 'field2' ),
					'conds' => array( 'alias' => 'text' ),
				),
				"SELECT  field,field2 AS alias  " .
				"FROM `unittest_table`  " .
				"WHERE alias = 'text'"
			),
			array(
				array(
					'tables' => 'table',
					'fields' => array( 'field', 'alias' => 'field2' ),
					'conds' => array( 'alias' => 'text' ),
					'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
				),
				"SELECT  field,field2 AS alias  " .
				"FROM `unittest_table`  " .
				"WHERE alias = 'text'  " .
				"ORDER BY field " .
				"LIMIT 1"
			),
			array(
				array(
					'tables' => array( 'table', 't2' => 'table2' ),
					'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
					'conds' => array( 'alias' => 'text' ),
					'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
					'join_conds' => array( 't2' => array(
						'LEFT JOIN', 'tid = t2.id'
					)),
				),
				"SELECT  tid,field,field2 AS alias,t2.id  " .
				"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
				"WHERE alias = 'text'  " .
				"ORDER BY field " .
				"LIMIT 1"
			),
			array(
				array(
					'tables' => array( 'table', 't2' => 'table2' ),
					'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
					'conds' => array( 'alias' => 'text' ),
					'options' => array( 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ),
					'join_conds' => array( 't2' => array(
						'LEFT JOIN', 'tid = t2.id'
					)),
				),
				"SELECT  tid,field,field2 AS alias,t2.id  " .
				"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
				"WHERE alias = 'text'  " .
				"GROUP BY field HAVING COUNT(*) > 1 " .
				"LIMIT 1"
			),
			array(
				array(
					'tables' => array( 'table', 't2' => 'table2' ),
					'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
					'conds' => array( 'alias' => 'text' ),
					'options' => array( 'LIMIT' => 1, 'GROUP BY' => array( 'field', 'field2' ), 'HAVING' => array( 'COUNT(*) > 1', 'field' => 1 ) ),
					'join_conds' => array( 't2' => array(
						'LEFT JOIN', 'tid = t2.id'
					)),
				),
				"SELECT  tid,field,field2 AS alias,t2.id  " .
				"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
				"WHERE alias = 'text'  " .
				"GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
				"LIMIT 1"
			),
		);
	}

	/**
	 * @dataProvider dataConditional
	 */
	function testConditional( $sql, $sqlText ) {
		$this->assertEquals( trim( $this->db->conditional(
			$sql['conds'],
			$sql['true'],
			$sql['false']
		) ), $sqlText );
	}

	function dataConditional() {
		return array(
			array(
				array(
					'conds' => array( 'field' => 'text' ),
					'true' => 1,
					'false' => 'NULL',
				),
				"(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
			),
			array(
				array(
					'conds' => array( 'field' => 'text', 'field2' => 'anothertext' ),
					'true' => 1,
					'false' => 'NULL',
				),
				"(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
			),
			array(
				array(
					'conds' => 'field=1',
					'true' => 1,
					'false' => 'NULL',
				),
				"(CASE WHEN field=1 THEN 1 ELSE NULL END)"
			),
		);
	}
}