12
12
namespace FOD \DBALClickHouse \Tests ;
13
13
14
14
use Doctrine \DBAL \FetchMode ;
15
+ use Doctrine \DBAL \Platforms \TrimMode ;
15
16
use FOD \DBALClickHouse \Connection ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
@@ -44,7 +45,7 @@ public function setUp()
44
45
$ this ->connection ->exec ($ sql );
45
46
}
46
47
47
- $ this ->connection ->exec ("INSERT INTO test_select_table(id, payload, hits) VALUES (1, 'v1', 101), (2, 'v2', 202), (3, 'v3', 303), (4, 'v4', 404), (5, 'v4', 505) " );
48
+ $ this ->connection ->exec ("INSERT INTO test_select_table(id, payload, hits) VALUES (1, 'v1', 101), (2, 'v2', 202), (3, 'v3', 303), (4, 'v4', 404), (5, 'v4', 505), (6, ' t1 ', 606), (7, 'aat2aaa', 707) " );
48
49
}
49
50
50
51
public function tearDown ()
@@ -76,14 +77,14 @@ public function testFetchNumSelect()
76
77
{
77
78
$ stmt = $ this ->connection ->query ('SELECT MAX(hits) FROM test_select_table ' );
78
79
$ result = $ stmt ->fetch (FetchMode::ASSOCIATIVE );
79
- $ this ->assertEquals (['MAX(hits) ' => 505 ], $ result );
80
+ $ this ->assertEquals (['MAX(hits) ' => 707 ], $ result );
80
81
}
81
82
82
83
public function testFetchObjSelect ()
83
84
{
84
85
$ stmt = $ this ->connection ->query ('SELECT MAX(hits) as maxHits FROM test_select_table ' );
85
86
$ result = $ stmt ->fetch (FetchMode::STANDARD_OBJECT );
86
- $ this ->assertEquals ((object )['maxHits ' => 505 ], $ result );
87
+ $ this ->assertEquals ((object )['maxHits ' => 707 ], $ result );
87
88
}
88
89
89
90
public function testFetchKeyPairSelect ()
@@ -98,51 +99,60 @@ public function testFetchAllBothSelect()
98
99
$ stmt = $ this ->connection ->query ("SELECT * FROM test_select_table WHERE id IN (1, 3) " );
99
100
$ result = $ stmt ->fetchAll ();
100
101
101
- $ this ->assertEquals ([[
102
- 'id ' => 1 ,
103
- 'payload ' => 'v1 ' ,
104
- 'hits ' => 101 ,
105
- ], [
106
- 'id ' => 3 ,
107
- 'payload ' => 'v3 ' ,
108
- 'hits ' => 303 ,
109
- ]], $ result );
102
+ $ this ->assertEquals ([
103
+ [
104
+ 'id ' => 1 ,
105
+ 'payload ' => 'v1 ' ,
106
+ 'hits ' => 101 ,
107
+ ],
108
+ [
109
+ 'id ' => 3 ,
110
+ 'payload ' => 'v3 ' ,
111
+ 'hits ' => 303 ,
112
+ ]
113
+ ], $ result );
110
114
}
111
115
112
116
public function testFetchAllNumSelect ()
113
117
{
114
118
$ stmt = $ this ->connection ->query ("SELECT AVG(hits) FROM test_select_table " );
115
119
$ result = $ stmt ->fetchAll (FetchMode::NUMERIC );
116
120
117
- $ this ->assertEquals ([[303 ]], $ result );
121
+ $ this ->assertEquals ([[404 ]], $ result );
118
122
}
119
123
120
124
public function testFetchAllObjSelect ()
121
125
{
122
126
$ stmt = $ this ->connection ->query ("SELECT * FROM test_select_table WHERE id IN (2, 4) " );
123
127
$ result = $ stmt ->fetchAll (FetchMode::STANDARD_OBJECT );
124
128
125
- $ this ->assertEquals ([(object )[
126
- 'id ' => 2 ,
127
- 'payload ' => 'v2 ' ,
128
- 'hits ' => 202 ,
129
- ], (object )[
130
- 'id ' => 4 ,
131
- 'payload ' => 'v4 ' ,
132
- 'hits ' => 404 ,
133
- ]], $ result );
129
+ $ this ->assertEquals ([
130
+ (object )[
131
+ 'id ' => 2 ,
132
+ 'payload ' => 'v2 ' ,
133
+ 'hits ' => 202 ,
134
+ ],
135
+ (object )[
136
+ 'id ' => 4 ,
137
+ 'payload ' => 'v4 ' ,
138
+ 'hits ' => 404 ,
139
+ ]
140
+ ], $ result );
134
141
}
135
142
136
143
public function testFetchAllKeyPairSelect ()
137
144
{
138
145
$ stmt = $ this ->connection ->query ("SELECT payload, hits FROM test_select_table WHERE id IN (2, 4) ORDER BY id " );
139
146
$ result = $ stmt ->fetchAll (\PDO ::FETCH_KEY_PAIR );
140
147
141
- $ this ->assertEquals ([[
142
- 'v2 ' => 202 ,
143
- ], [
144
- 'v4 ' => 404 ,
145
- ]], $ result );
148
+ $ this ->assertEquals ([
149
+ [
150
+ 'v2 ' => 202 ,
151
+ ],
152
+ [
153
+ 'v4 ' => 404 ,
154
+ ]
155
+ ], $ result );
146
156
}
147
157
148
158
public function testFetchColumnValidOffsetSelect ()
@@ -163,8 +173,7 @@ public function testFetchColumnInvalidOffsetSelect()
163
173
while ($ result = $ stmt ->fetchColumn (2 )) {
164
174
$ results [] = $ result ;
165
175
}
166
-
167
- $ this ->assertEquals (['v2 ' , 'v3 ' , 'v4 ' , 'v4 ' ], $ results );
176
+ $ this ->assertEquals (['v2 ' , 'v3 ' , 'v4 ' , 'v4 ' , ' t1 ' , 'aat2aaa ' ], $ results );
168
177
}
169
178
170
179
public function testQueryBuilderSelect ()
@@ -181,13 +190,16 @@ public function testQueryBuilderSelect()
181
190
->execute ()
182
191
->fetchAll ();
183
192
184
- $ this ->assertEquals ([[
185
- 'payload ' => 'v3 ' ,
186
- 'uniques ' => '1 ' ,
187
- ], [
188
- 'payload ' => 'v4 ' ,
189
- 'uniques ' => '2 ' ,
190
- ]], $ result );
193
+ $ this ->assertEquals ([
194
+ [
195
+ 'payload ' => ' t1 ' ,
196
+ 'uniques ' => '1 ' ,
197
+ ],
198
+ [
199
+ 'payload ' => 'aat2aaa ' ,
200
+ 'uniques ' => '1 ' ,
201
+ ]
202
+ ], $ result );
191
203
}
192
204
193
205
public function testDynamicParametersSelect ()
@@ -201,6 +213,14 @@ public function testDynamicParametersSelect()
201
213
[
202
214
'payload ' => 'v4 ' ,
203
215
'avg_hits ' => 454.5 ,
216
+ ],
217
+ [
218
+ 'payload ' => ' t1 ' ,
219
+ 'avg_hits ' => 606 ,
220
+ ],
221
+ [
222
+ 'payload ' => 'aat2aaa ' ,
223
+ 'avg_hits ' => 707 ,
204
224
]
205
225
], $ stmt ->fetchAll ());
206
226
}
@@ -212,5 +232,57 @@ public function testColumnCount()
212
232
213
233
$ this ->assertEquals (3 , $ stmt ->columnCount ());
214
234
}
235
+
236
+ public function testTrim ()
237
+ {
238
+ $ stmt = $ this ->connection ->prepare (
239
+ sprintf (
240
+ 'SELECT %s FROM test_select_table WHERE id = 6 ' ,
241
+ $ this ->connection ->getDatabasePlatform ()->getTrimExpression ('payload ' )
242
+ )
243
+ );
244
+ $ stmt ->execute ();
245
+
246
+ $ this ->assertEquals ('t1 ' , $ stmt ->fetchColumn ());
247
+ }
248
+
249
+ public function testTrimLeft ()
250
+ {
251
+ $ stmt = $ this ->connection ->prepare (
252
+ sprintf (
253
+ 'SELECT %s FROM test_select_table WHERE id = 6 ' ,
254
+ $ this ->connection ->getDatabasePlatform ()->getTrimExpression ('payload ' , TrimMode::LEADING )
255
+ )
256
+ );
257
+ $ stmt ->execute ();
258
+
259
+ $ this ->assertEquals ('t1 ' , $ stmt ->fetchColumn ());
260
+ }
261
+
262
+ public function testTrimRight ()
263
+ {
264
+ $ stmt = $ this ->connection ->prepare (
265
+ sprintf (
266
+ 'SELECT %s FROM test_select_table WHERE id = 6 ' ,
267
+ $ this ->connection ->getDatabasePlatform ()->getTrimExpression ('payload ' , TrimMode::TRAILING )
268
+ )
269
+ );
270
+ $ stmt ->execute ();
271
+
272
+ $ this ->assertEquals (' t1 ' , $ stmt ->fetchColumn ());
273
+ }
274
+
275
+ public function testTrimChar ()
276
+ {
277
+ $ stmt = $ this ->connection ->prepare (
278
+ sprintf (
279
+ 'SELECT %s FROM test_select_table WHERE id = 7 ' ,
280
+ $ this ->connection ->getDatabasePlatform ()->getTrimExpression ('payload ' , TrimMode::UNSPECIFIED , 'a ' )
281
+ )
282
+ );
283
+ $ stmt ->execute ();
284
+
285
+ $ this ->assertEquals ('t2 ' , $ stmt ->fetchColumn ());
286
+ }
215
287
}
216
288
0 commit comments