4 * Test the abstract database layer
5 * Using Mysql for the sql at the moment TODO
9 class DatabaseSQLTest
extends MediaWikiTestCase
{
11 protected function setUp() {
12 // TODO support other DBMS or find another way to do it
13 if ( $this->db
->getType() !== 'mysql' ) {
14 $this->markTestSkipped( 'No mysql database' );
19 * @dataProvider provideSelectSQLText
21 function testSelectSQLText( $sql, $sqlText ) {
22 $this->assertEquals( trim( $this->db
->selectSQLText(
23 isset( $sql['tables'] ) ?
$sql['tables'] : array(),
24 isset( $sql['fields'] ) ?
$sql['fields'] : array(),
25 isset( $sql['conds'] ) ?
$sql['conds'] : array(),
27 isset( $sql['options'] ) ?
$sql['options'] : array(),
28 isset( $sql['join_conds'] ) ?
$sql['join_conds'] : array()
32 public static function provideSelectSQLText() {
37 'fields' => array( 'field', 'alias' => 'field2' ),
38 'conds' => array( 'alias' => 'text' ),
40 "SELECT field,field2 AS alias " .
41 "FROM `unittest_table` " .
42 "WHERE alias = 'text'"
47 'fields' => array( 'field', 'alias' => 'field2' ),
48 'conds' => array( 'alias' => 'text' ),
49 'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
51 "SELECT field,field2 AS alias " .
52 "FROM `unittest_table` " .
53 "WHERE alias = 'text' " .
59 'tables' => array( 'table', 't2' => 'table2' ),
60 'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
61 'conds' => array( 'alias' => 'text' ),
62 'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
63 'join_conds' => array( 't2' => array(
64 'LEFT JOIN', 'tid = t2.id'
67 "SELECT tid,field,field2 AS alias,t2.id " .
68 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
69 "WHERE alias = 'text' " .
75 'tables' => array( 'table', 't2' => 'table2' ),
76 'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
77 'conds' => array( 'alias' => 'text' ),
78 'options' => array( 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ),
79 'join_conds' => array( 't2' => array(
80 'LEFT JOIN', 'tid = t2.id'
83 "SELECT tid,field,field2 AS alias,t2.id " .
84 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
85 "WHERE alias = 'text' " .
86 "GROUP BY field HAVING COUNT(*) > 1 " .
91 'tables' => array( 'table', 't2' => 'table2' ),
92 'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
93 'conds' => array( 'alias' => 'text' ),
94 'options' => array( 'LIMIT' => 1, 'GROUP BY' => array( 'field', 'field2' ), 'HAVING' => array( 'COUNT(*) > 1', 'field' => 1 ) ),
95 'join_conds' => array( 't2' => array(
96 'LEFT JOIN', 'tid = t2.id'
99 "SELECT tid,field,field2 AS alias,t2.id " .
100 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
101 "WHERE alias = 'text' " .
102 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
109 * @dataProvider provideConditional
111 function testConditional( $sql, $sqlText ) {
112 $this->assertEquals( trim( $this->db
->conditional(
119 public static function provideConditional() {
123 'conds' => array( 'field' => 'text' ),
127 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
131 'conds' => array( 'field' => 'text', 'field2' => 'anothertext' ),
135 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
139 'conds' => 'field=1',
143 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"