1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace CrateTest\PDO; |
24
|
|
|
|
25
|
|
|
use Crate\PDO\Exception\InvalidArgumentException; |
26
|
|
|
use Crate\PDO\Exception\PDOException; |
27
|
|
|
use Crate\PDO\Exception\UnsupportedException; |
28
|
|
|
use Crate\PDO\Http\Client; |
29
|
|
|
use Crate\PDO\Http\ClientInterface; |
30
|
|
|
use Crate\PDO\PDO; |
31
|
|
|
use Crate\PDO\PDOStatement; |
32
|
|
|
use PHPUnit_Framework_TestCase; |
33
|
|
|
use ReflectionClass; |
34
|
|
|
use Crate\Stdlib\Collection; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tests for {@see \Crate\PDO\PDO} |
38
|
|
|
* |
39
|
|
|
* @coversDefaultClass \Crate\PDO\PDO |
40
|
|
|
* @covers ::<!public> |
41
|
|
|
* |
42
|
|
|
* @group unit |
43
|
|
|
*/ |
44
|
|
|
class PDOTest extends PHPUnit_Framework_TestCase |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var PDO |
48
|
|
|
*/ |
49
|
|
|
protected $pdo; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
53
|
|
|
*/ |
54
|
|
|
protected $client; |
55
|
|
|
|
56
|
|
|
protected function setUp() |
57
|
|
|
{ |
58
|
|
|
$this->client = $this->getMockBuilder(Client::class) |
59
|
|
|
->disableOriginalConstructor() |
60
|
|
|
->getMock(); |
61
|
|
|
|
62
|
|
|
$this->pdo = new PDO('crate:localhost:1234', null, null, []); |
63
|
|
|
|
64
|
|
|
$reflection = new ReflectionClass(PDO::class); |
65
|
|
|
|
66
|
|
|
$property = $reflection->getProperty('client'); |
67
|
|
|
$property->setAccessible(true); |
68
|
|
|
$property->setValue($this->pdo, $this->client); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @covers ::__construct |
73
|
|
|
*/ |
74
|
|
|
public function testInstantiation() |
75
|
|
|
{ |
76
|
|
|
$pdo = new PDO('crate:localhost:1234', null, null, []); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
79
|
|
|
$this->assertInstanceOf('PDO', $pdo); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testInstantiationWithDefaultSchema() |
83
|
|
|
{ |
84
|
|
|
$pdo = new PDO('crate:localhost:1234/my_schema', null, null, []); |
85
|
|
|
|
86
|
|
|
$this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
87
|
|
|
$this->assertInstanceOf('PDO', $pdo); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @covers ::__construct |
92
|
|
|
*/ |
93
|
|
|
public function testInstantiationWithTraversableOptions() |
94
|
|
|
{ |
95
|
|
|
$pdo = new PDO('crate:localhost:1234', null, null, new \ArrayObject([PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION])); |
|
|
|
|
96
|
|
|
$this->assertEquals(PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(PDO::ATTR_ERRMODE)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @covers ::__construct |
101
|
|
|
*/ |
102
|
|
|
public function testInstantiationWithInvalidOptions() |
103
|
|
|
{ |
104
|
|
|
$this->setExpectedException('Crate\Stdlib\Exception\InvalidArgumentException'); |
105
|
|
|
|
106
|
|
|
new PDO('crate:localhost:1234', null, null, 'a invalid value'); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @covers ::__construct |
111
|
|
|
*/ |
112
|
|
|
public function testInstantiationWithHttpAuth() { |
113
|
|
|
$user = 'crate'; |
114
|
|
|
$passwd = 'secret'; |
115
|
|
|
$pdo = new PDO('crate:localhost:44200', $user, $passwd, []); |
116
|
|
|
$this->assertEquals([$user, $passwd], $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @covers ::setAttribute |
121
|
|
|
*/ |
122
|
|
|
public function testSetAttributeWithHttpBasicAuth() |
123
|
|
|
{ |
124
|
|
|
$user = 'crate'; |
125
|
|
|
$passwd = 'secret'; |
126
|
|
|
$expectedCredentials = [$user, $passwd]; |
127
|
|
|
|
128
|
|
|
$client = $this->getMock(ClientInterface::class); |
129
|
|
|
$pdo = new PDO('crate:localhost:44200', null, null, []); |
130
|
|
|
$reflection = new ReflectionClass(PDO::class); |
131
|
|
|
$property = $reflection->getProperty('client'); |
132
|
|
|
$property->setAccessible(true); |
133
|
|
|
$property->setValue($pdo, $client); |
134
|
|
|
$client |
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('setHttpBasicAuth') |
137
|
|
|
->with($user, $passwd); |
138
|
|
|
|
139
|
|
|
$pdo->setAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH, [$user, $passwd]); |
140
|
|
|
$this->assertEquals($expectedCredentials, $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @covers ::setAttribute |
145
|
|
|
*/ |
146
|
|
|
public function testSetAttributeWithDefaultSchema() |
147
|
|
|
{ |
148
|
|
|
$client = $this->getMockBuilder(Client::class) |
149
|
|
|
->disableOriginalConstructor() |
150
|
|
|
->getMock(); |
151
|
|
|
$pdo = new PDO('crate:localhost:44200/my_schema', null, null, []); |
152
|
|
|
$reflection = new ReflectionClass(PDO::class); |
153
|
|
|
$property = $reflection->getProperty('client'); |
154
|
|
|
$property->setAccessible(true); |
155
|
|
|
$property->setValue($pdo, $client); |
156
|
|
|
$client->expects($this->once()) |
157
|
|
|
->method('setDefaultSchema') |
158
|
|
|
->with('my_schema'); |
159
|
|
|
|
160
|
|
|
$pdo->setAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA, 'my_schema'); |
161
|
|
|
$this->assertEquals('my_schema', $pdo->getAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @covers ::getAttribute |
166
|
|
|
*/ |
167
|
|
|
public function testGetAttributeWithInvalidAttribute() |
168
|
|
|
{ |
169
|
|
|
$this->setExpectedException(PDOException::class); |
170
|
|
|
$this->pdo->getAttribute('I DONT EXIST'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @covers ::setAttribute |
175
|
|
|
*/ |
176
|
|
|
public function testSetAttributeWithInvalidAttribute() |
177
|
|
|
{ |
178
|
|
|
$this->setExpectedException(PDOException::class); |
179
|
|
|
$this->pdo->setAttribute('I DONT EXIST', 'value'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @covers ::getAttribute |
184
|
|
|
* @covers ::setAttribute |
185
|
|
|
*/ |
186
|
|
|
public function testGetAndSetDefaultFetchMode() |
187
|
|
|
{ |
188
|
|
|
$this->assertEquals(PDO::FETCH_BOTH, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE)); |
189
|
|
|
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
190
|
|
|
$this->assertEquals(PDO::FETCH_ASSOC, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @covers ::getAttribute |
195
|
|
|
* @covers ::setAttribute |
196
|
|
|
*/ |
197
|
|
|
public function testGetAndSetErrorMode() |
198
|
|
|
{ |
199
|
|
|
$this->assertEquals(PDO::ERRMODE_SILENT, $this->pdo->getAttribute(PDO::ATTR_ERRMODE)); |
200
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
201
|
|
|
$this->assertEquals(PDO::ERRMODE_EXCEPTION, $this->pdo->getAttribute(PDO::ATTR_ERRMODE)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @covers ::getAttribute |
206
|
|
|
*/ |
207
|
|
|
public function testGetVersion() |
208
|
|
|
{ |
209
|
|
|
$this->assertEquals(PDO::VERSION, $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @covers ::getAttribute |
214
|
|
|
*/ |
215
|
|
|
public function testGetDriverName() |
216
|
|
|
{ |
217
|
|
|
$this->assertEquals(PDO::DRIVER_NAME, $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testGetStatementClass() |
221
|
|
|
{ |
222
|
|
|
$this->assertEquals([PDOStatement::class], $this->pdo->getAttribute(PDO::ATTR_STATEMENT_CLASS)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @covers ::getAttribute |
227
|
|
|
*/ |
228
|
|
|
public function testPersistent() |
229
|
|
|
{ |
230
|
|
|
$this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PERSISTENT)); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @covers ::getAttribute |
235
|
|
|
*/ |
236
|
|
|
public function testPreFetch() |
237
|
|
|
{ |
238
|
|
|
$this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PREFETCH)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @covers ::getAttribute |
243
|
|
|
*/ |
244
|
|
|
public function testAutoCommit() |
245
|
|
|
{ |
246
|
|
|
$this->assertTrue($this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @covers ::getAttribute |
251
|
|
|
* @covers ::setAttribute |
252
|
|
|
*/ |
253
|
|
|
public function testGetAndSetTimeout() |
254
|
|
|
{ |
255
|
|
|
$timeout = 3; |
256
|
|
|
|
257
|
|
|
$this->client |
258
|
|
|
->expects($this->once()) |
259
|
|
|
->method('setTimeout') |
260
|
|
|
->with($timeout); |
261
|
|
|
|
262
|
|
|
$this->assertEquals(5, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT)); |
263
|
|
|
|
264
|
|
|
$this->pdo->setAttribute(PDO::ATTR_TIMEOUT, $timeout); |
265
|
|
|
|
266
|
|
|
$this->assertEquals($timeout, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @covers ::quote |
271
|
|
|
*/ |
272
|
|
|
public function testQuote() |
273
|
|
|
{ |
274
|
|
|
$this->assertTrue($this->pdo->quote('1', PDO::PARAM_BOOL)); |
275
|
|
|
$this->assertFalse($this->pdo->quote('0', PDO::PARAM_BOOL)); |
276
|
|
|
|
277
|
|
|
$this->assertEquals(100, $this->pdo->quote('100', PDO::PARAM_INT)); |
278
|
|
|
$this->assertNull($this->pdo->quote('helloWorld', PDO::PARAM_NULL)); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
|
|
public function quoteExceptionProvider() |
285
|
|
|
{ |
286
|
|
|
return [ |
287
|
|
|
[PDO::PARAM_LOB, PDOException::class, 'This is not supported by crate.io'], |
288
|
|
|
[PDO::PARAM_STR, PDOException::class, 'This is not supported, please use prepared statements.'], |
289
|
|
|
[120, InvalidArgumentException::class, 'Unknown param type'], |
290
|
|
|
]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @dataProvider quoteExceptionProvider |
295
|
|
|
* @covers ::quote |
296
|
|
|
* |
297
|
|
|
* @param int $paramType |
298
|
|
|
* @param string $message |
299
|
|
|
*/ |
300
|
|
|
public function testQuoteWithExpectedException($paramType, $exception, $message) |
301
|
|
|
{ |
302
|
|
|
$this->setExpectedException($exception, $message); |
303
|
|
|
$this->pdo->quote('helloWorld', $paramType); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @covers ::prepare |
308
|
|
|
*/ |
309
|
|
|
public function testPrepareReturnsAPDOStatement() |
310
|
|
|
{ |
311
|
|
|
$statement = $this->pdo->prepare('SELECT * FROM tweets'); |
312
|
|
|
$this->assertInstanceOf(PDOStatement::class, $statement); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @covers ::getAvailableDrivers |
317
|
|
|
*/ |
318
|
|
|
public function testAvailableDriversContainsCrate() |
319
|
|
|
{ |
320
|
|
|
$this->assertContains('crate', PDO::getAvailableDrivers()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @covers ::beginTransaction |
325
|
|
|
*/ |
326
|
|
|
public function testBeginTransactionThrowsUnsupportedException() |
327
|
|
|
{ |
328
|
|
|
$this->assertTrue($this->pdo->beginTransaction()); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @covers ::commit |
333
|
|
|
*/ |
334
|
|
|
public function testCommitThrowsUnsupportedException() |
335
|
|
|
{ |
336
|
|
|
$this->assertTrue($this->pdo->commit()); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @covers ::rollback |
341
|
|
|
*/ |
342
|
|
|
public function testRollbackThrowsUnsupportedException() |
343
|
|
|
{ |
344
|
|
|
$this->setExpectedException(UnsupportedException::class); |
345
|
|
|
$this->pdo->rollBack(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @covers ::inTransaction |
350
|
|
|
*/ |
351
|
|
|
public function testInTransactionThrowsUnsupportedException() |
352
|
|
|
{ |
353
|
|
|
$this->assertFalse($this->pdo->inTransaction()); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @covers ::lastInsertId |
358
|
|
|
*/ |
359
|
|
|
public function testLastInsertIdThrowsUnsupportedException() |
360
|
|
|
{ |
361
|
|
|
$this->setExpectedException(UnsupportedException::class); |
362
|
|
|
$this->pdo->lastInsertId(); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: