-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathQueryHelperTest.php
More file actions
173 lines (148 loc) · 4.37 KB
/
QueryHelperTest.php
File metadata and controls
173 lines (148 loc) · 4.37 KB
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
<?php
/**
* QueryHelper Class Unit Test
*
* @package Tutor\Test
* @since 2.1.9
*/
namespace TutorTest;
use Tutor\Helpers\QueryHelper;
/**
* QueryHelper utility methods testing
*/
class QueryHelperTest extends \WP_UnitTestCase {
/**
* Test QueryHelper::prepare_set_clause
*
* @return void
*/
public function test_prepare_set_clause() {
// Single elements.
$array1 = array( 'id' => 1 );
$actual1 = QueryHelper::prepare_set_clause( $array1 );
$expect = 'SET id = 1';
// Multiple elements.
$array2 = array(
'id' => 1,
'title' => 'title',
'status' => 'publish',
);
$actual2 = QueryHelper::prepare_set_clause( $array2 );
$expect2 = "SET id = 1,title = 'title',status = 'publish'";
// Multi-dimension array.
$array3 = array(
'id' => 1,
'title' => 'title',
'status' => 'publish',
'post_parent' => array(
1,
2,
3,
),
);
$actual3 = QueryHelper::prepare_set_clause( $array3 );
// Since multi-dimension is not allowed post_parent will be omitted.
$expect3 = "SET id = 1,title = 'title',status = 'publish'";
$array4 = array();
$actual4 = QueryHelper::prepare_set_clause( $array4 );
$expect4 = '';
$this->assertSame( $expect, trim( $actual1 ) );
$this->assertSame( $expect2, trim( $actual2 ) );
$this->assertSame( $expect3, trim( $actual3 ) );
$this->assertSame( $expect4, trim( $actual4 ) );
}
/**
* Test QueryHelper::prepare_where_clause()
*
* @since 3.6.0
*
* @return void
*/
public function test_prepare_where_clause() {
$test1 = array(
'age' => 18,
'height' => array( '>=', '5feet7inch' ),
'hobbies' => array( 'coding', 'biking', 'swimming' ),
);
$expect1 = "age = 18 AND height >= '5feet7inch' AND hobbies IN ('coding','biking','swimming')";
$actual1 = QueryHelper::prepare_where_clause( $test1 );
$test2 = array(
'salary' => array( 'BETWEEN', array( 10, 20 ) ),
'name' => array( 'NOT BETWEEN', array( 'b', 'c' ) ),
);
$expect2 = "salary BETWEEN 10 AND 20 AND name NOT BETWEEN 'b' AND 'c'";
$actual2 = QueryHelper::prepare_where_clause( $test2 );
$test3 = array(
'name' => array( 'LIKE', 'test' ),
'age' => array( 'NOT IN', array( 18, 19, 20 ) ),
);
$expect3 = "name LIKE 'test' AND age NOT IN (18,19,20)";
$actual3 = QueryHelper::prepare_where_clause( $test3 );
$test4 = array(
'name' => 'NULL',
'age' => array( 'IS NOT', 'NULL' ),
);
$expect4 = 'name IS NULL AND age IS NOT NULL';
$actual4 = QueryHelper::prepare_where_clause( $test4 );
$this->assertEquals( $expect1, trim( $actual1 ) );
$this->assertEquals( $expect2, trim( $actual2 ) );
$this->assertEquals( $expect3, trim( $actual3 ) );
$this->assertEquals( $expect4, trim( $actual4 ) );
}
/**
* Test QueryHelper Raw query.
*
* @since 3.6.0
*
* @return void
*/
public function test_prepare_raw_query() {
$case_1 = array(
"username = '%s'" => array(
'RAW',
array(
'admin',
),
),
);
$case_2 = array(
'age >= %d' => array(
'RAW',
array(
20,
),
),
);
$case_3 = array(
'id' => array(
'BETWEEN',
array( 10, 20 ),
),
'DATE(value) = CAST(value as date) AND id = %d' => array(
'RAW',
array( 10 ),
),
);
$expect_1 = "username = 'admin'";
$expect_2 = 'age >= 20';
$expect_3 = 'id BETWEEN 10 AND 20 AND DATE(value) = CAST(value as date) AND id = 10';
$this->assertEquals( $expect_1, QueryHelper::prepare_where_clause( $case_1 ) );
$this->assertEquals( $expect_2, QueryHelper::prepare_where_clause( $case_2 ) );
$this->assertEquals( $expect_3, QueryHelper::prepare_where_clause( $case_3 ) );
}
/**
* Test QueryHelper::prepare_in_clause
*
* @return void
* @since 2.1.1
*/
public function test_prepare_in_clause() {
$expected_query = "SELECT * from abc WHERE id IN(3,4,5,'hello')";
$raw_sql_query = 'SELECT * from abc WHERE id IN(' . QueryHelper::prepare_in_clause( array( 3, 4, 5, 'hello' ) ) . ')';
$this->assertEquals( $expected_query, $raw_sql_query );
$this->assertEquals( "'A','B','C'", QueryHelper::prepare_in_clause( array( 'A', 'B', 'C' ) ) );
$this->assertEquals( '10,20,40', QueryHelper::prepare_in_clause( array( 10, 20, 40 ) ) );
$this->assertEquals( "'jhon',3,4.55,'adam'", QueryHelper::prepare_in_clause( array( 'jhon', 3, 4.55, 'adam' ) ) );
$this->assertEquals( '2,3,5.996', QueryHelper::prepare_in_clause( array( 2, 3, 5.996 ) ) );
}
}