merged master
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSQLTest.php
1 <?php
2
3 /**
4 * Test the abstract database layer
5 * Using Mysql for the sql at the moment TODO
6 *
7 * @group Database
8 */
9 class DatabaseSQLTest extends MediaWikiTestCase {
10
11 public 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' );
15 }
16 }
17
18 /**
19 * @dataProvider dataSelectSQLText
20 */
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(),
26 __METHOD__,
27 isset( $sql['options'] ) ? $sql['options'] : array(),
28 isset( $sql['join_conds'] ) ? $sql['join_conds'] : array()
29 ) ), $sqlText );
30 }
31
32 function dataSelectSQLText() {
33 return array(
34 array(
35 array(
36 'tables' => 'table',
37 'fields' => array( 'field', 'alias' => 'field2' ),
38 'conds' => array( 'alias' => 'text' ),
39 ),
40 "SELECT field,field2 AS alias " .
41 "FROM `unittest_table` " .
42 "WHERE alias = 'text'"
43 ),
44 array(
45 array(
46 'tables' => 'table',
47 'fields' => array( 'field', 'alias' => 'field2' ),
48 'conds' => array( 'alias' => 'text' ),
49 'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
50 ),
51 "SELECT field,field2 AS alias " .
52 "FROM `unittest_table` " .
53 "WHERE alias = 'text' " .
54 "ORDER BY field " .
55 "LIMIT 1"
56 ),
57 array(
58 array(
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'
65 )),
66 ),
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' " .
70 "ORDER BY field " .
71 "LIMIT 1"
72 ),
73 );
74 }
75
76 /**
77 * @dataProvider dataConditional
78 */
79 function testConditional( $sql, $sqlText ) {
80 $this->assertEquals( trim( $this->db->conditional(
81 $sql['conds'],
82 $sql['true'],
83 $sql['false']
84 ) ), $sqlText );
85 }
86
87 function dataConditional() {
88 return array(
89 array(
90 array(
91 'conds' => array( 'field' => 'text' ),
92 'true' => 1,
93 'false' => 'NULL',
94 ),
95 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
96 ),
97 array(
98 array(
99 'conds' => 'field=1',
100 'true' => 1,
101 'false' => 'NULL',
102 ),
103 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
104 ),
105 );
106 }
107 }