tests - add autogenerated comment blocks
[civicrm-core.git] / tests / phpunit / CRM / Utils / SQL / SelectTest.php
CommitLineData
e2b5e5b1
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3
aba1cd8b
EM
4/**
5 * Class CRM_Utils_SQL_SelectTest
6 */
e2b5e5b1
TO
7class CRM_Utils_SQL_SelectTest extends CiviUnitTestCase {
8 function testGetDefault() {
9 $select = CRM_Utils_SQL_Select::from('foo bar');
10 $this->assertLike('SELECT * FROM foo bar', $select->toSQL());
11 }
12
13 function testGetFields() {
14 $select = CRM_Utils_SQL_Select::from('foo')
15 ->select('bar')
16 ->select(array('whiz', 'bang'));
17 $this->assertLike('SELECT bar, whiz, bang FROM foo', $select->toSQL());
18 }
19
20 function testWherePlain() {
21 $select = CRM_Utils_SQL_Select::from('foo')
22 ->where('foo = bar')
23 ->where(array('whiz = bang', 'frob > nicate'));
24 $this->assertLike('SELECT * FROM foo WHERE (foo = bar) AND (whiz = bang) AND (frob > nicate)', $select->toSQL());
25 }
26
27 function testWhereArg() {
28 $select = CRM_Utils_SQL_Select::from('foo')
29 ->where('foo = @value', array('@value' => 'not"valid'))
30 ->where(array('whiz > @base', 'frob != @base'), array('@base' => 'in"valid'));
31 $this->assertLike('SELECT * FROM foo WHERE (foo = "not\\"valid") AND (whiz > "in\\"valid") AND (frob != "in\\"valid")', $select->toSQL());
32 }
33
34 function testGroupByPlain() {
35 $select = CRM_Utils_SQL_Select::from('foo')
36 ->groupBy("bar_id")
37 ->groupBy(array('whiz_id*2', 'lower(bang)'));
38 $this->assertLike('SELECT * FROM foo GROUP BY bar_id, whiz_id*2, lower(bang)', $select->toSQL());
39 }
40
41 function testHavingPlain() {
42 $select = CRM_Utils_SQL_Select::from('foo')
43 ->groupBy("bar_id")
44 ->having('count(id) > 2')
45 ->having(array('sum(id) > 10', 'avg(id) < 200'));
46 $this->assertLike('SELECT * FROM foo GROUP BY bar_id HAVING (count(id) > 2) AND (sum(id) > 10) AND (avg(id) < 200)', $select->toSQL());
47 }
48
49 function testHavingArg() {
50 $select = CRM_Utils_SQL_Select::from('foo')
51 ->groupBy("bar_id")
52 ->having('count(id) > #mincnt', array('#mincnt' => 2))
53 ->having(array('sum(id) > #whiz', 'avg(id) < #whiz'), array('#whiz' => 10));
54 $this->assertLike('SELECT * FROM foo GROUP BY bar_id HAVING (count(id) > 2) AND (sum(id) > 10) AND (avg(id) < 10)', $select->toSQL());
55 }
56
c3a8e3e5
TO
57 function testOrderByPlain() {
58 $select = CRM_Utils_SQL_Select::from('foo bar')
59 ->orderBy('first asc')
60 ->orderBy(array('second desc', 'third'));
61 $this->assertLike('SELECT * FROM foo bar ORDER BY first asc, second desc, third', $select->toSQL());
62 }
63
64 function testLimit_defaultOffset() {
65 $select = CRM_Utils_SQL_Select::from('foo bar')
66 ->limit(20);
67 $this->assertLike('SELECT * FROM foo bar LIMIT 20 OFFSET 0', $select->toSQL());
68 }
69
70 function testLimit_withOffset() {
71 $select = CRM_Utils_SQL_Select::from('foo bar')
72 ->limit(20, 60);
73 $this->assertLike('SELECT * FROM foo bar LIMIT 20 OFFSET 60', $select->toSQL());
74 }
75
76 function testLimit_disable() {
77 $select = CRM_Utils_SQL_Select::from('foo bar')
78 ->limit(20, 60)
79 ->limit(NULL, NULL);
80 $this->assertLike('SELECT * FROM foo bar', $select->toSQL());
81 }
82
e2b5e5b1
TO
83 function testBig() {
84 $select = CRM_Utils_SQL_Select::from('foo')
85 ->select('foo.id')
86 ->join('rel1', 'INNER JOIN rel1_table rel1 ON foo.id = rel1.foo_id')
87 ->join('rel2', 'LEFT JOIN rel2_table rel2 ON foo.id = rel2.foo_id')
88 ->where('foo.type = @type', array('@type' => 'mytype'))
89 ->groupBy("foo.id")
c3a8e3e5
TO
90 ->having('sum(rel1.stat) > 10')
91 ->orderBy('rel2.whiz')
92 ->limit(100, 300);
e2b5e5b1
TO
93 $this->assertLike(
94 "SELECT foo.id FROM foo"
95 . " INNER JOIN rel1_table rel1 ON foo.id = rel1.foo_id"
96 . " LEFT JOIN rel2_table rel2 ON foo.id = rel2.foo_id "
97 . " WHERE (foo.type = \"mytype\")"
98 . " GROUP BY foo.id"
c3a8e3e5
TO
99 . " HAVING (sum(rel1.stat) > 10)"
100 . " ORDER BY rel2.whiz"
101 . " LIMIT 100 OFFSET 300",
e2b5e5b1
TO
102 $select->toSQL()
103 );
104 }
105
106 function testInterpolate() {
107 $actual = CRM_Utils_SQL_Select::from('ignore')->interpolate(
108 '@escaped !unescaped #validated',
109 array(
110 '@escaped' => 'foo"bar',
111 '!unescaped' => 'concat(foo,bar)',
112 '#validated' => 15.2
113 )
114 );
115 $this->assertLike('"foo\"bar" concat(foo,bar) 15.2', $actual);
116 }
117
118 function testInterpolateArray() {
119 $actual = CRM_Utils_SQL_Select::from('ignore')->interpolate(
120 '(@escaped) (!unescaped) (#validated)',
121 array(
122 '@escaped' => array('foo"bar', "whiz", "null", null, "bang"),
123 '!unescaped' => array('foo"bar', 'bar'),
124 '#validated' => array(1, 10, null, 100.1)
125 )
126 );
127 $this->assertLike('("foo\\"bar", "whiz", "null", NULL, "bang") (foo"bar, bar) (1, 10, NULL, 100.1)', $actual);
128 }
129
130 function testInterpolateBadNumber() {
131 try {
132 $result = CRM_Utils_SQL_Select::from('ignore')->interpolate('#num', array(
133 '#num' => '5not-a-number5'
134 ));
135 $this->fail('Expected exception; got: ' . var_export($result, TRUE));
136 } catch (CRM_Core_Exception $e) {
137 $this->assertTrue(TRUE, "Caught expected exception");
138 }
139
140 try {
141 $result = CRM_Utils_SQL_Select::from('ignore')->interpolate('#num', array(
142 '#num' => array(1, '5not-a-number5', 2)
143 ));
144 $this->fail('Expected exception; got: ' . var_export($result, TRUE));
145 } catch (CRM_Core_Exception $e) {
146 $this->assertTrue(TRUE, "Caught expected exception");
147 }
148 }
149
150 function testInterpolateBadKey() {
151 try {
152 $result = CRM_Utils_SQL_Select::from('ignore')->interpolate('this is a {var}', array(
153 '{var}' => 'not a well-formed variable name'
154 ));
155 $this->fail('Expected exception; got: ' . var_export($result, TRUE));
156 } catch (CRM_Core_Exception $e) {
157 $this->assertTrue(TRUE, "Caught expected exception");
158 }
159 }
160
4cbe18b8
EM
161 /**
162 * @param $expected
163 * @param $actual
164 * @param string $message
165 */
e2b5e5b1
TO
166 function assertLike($expected, $actual, $message = '') {
167 $expected = trim((preg_replace('/[ \r\n\t]+/', ' ', $expected)));
168 $actual = trim((preg_replace('/[ \r\n\t]+/', ' ', $actual)));
169 $this->assertEquals($expected, $actual, $message);
170 }
4cbe18b8 171}