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\ClientInterface; |
29
|
|
|
use Crate\PDO\PDO; |
30
|
|
|
use Crate\PDO\PDOStatement; |
31
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
32
|
|
|
use PHPUnit_Framework_TestCase; |
33
|
|
|
use ReflectionClass; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tests for {@see \Crate\PDO\PDO} |
37
|
|
|
* |
38
|
|
|
* @coversDefaultClass \Crate\PDO\PDO |
39
|
|
|
* @covers ::<!public> |
40
|
|
|
* |
41
|
|
|
* @group unit |
42
|
|
|
*/ |
43
|
|
|
class PDOTest extends PHPUnit_Framework_TestCase |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var PDO |
47
|
|
|
*/ |
48
|
|
|
protected $pdo; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject|ClientInterface |
52
|
|
|
*/ |
53
|
|
|
protected $client; |
54
|
|
|
|
55
|
|
|
protected function setUp() |
56
|
|
|
{ |
57
|
|
|
$this->client = $this->getMock(ClientInterface::class); |
58
|
|
|
|
59
|
|
|
$this->pdo = new PDO('crate:localhost:1234', null, null, []); |
60
|
|
|
|
61
|
|
|
$reflection = new ReflectionClass(PDO::class); |
62
|
|
|
|
63
|
|
|
$property = $reflection->getProperty('client'); |
64
|
|
|
$property->setAccessible(true); |
65
|
|
|
$property->setValue($this->pdo, $this->client); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers ::__construct |
70
|
|
|
*/ |
71
|
|
|
public function testInstantiation() |
72
|
|
|
{ |
73
|
|
|
$pdo = new PDO('crate:localhost:1234', null, null, []); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
76
|
|
|
$this->assertInstanceOf('PDO', $pdo); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testInstantiationWithDefaultSchema() |
80
|
|
|
{ |
81
|
|
|
$pdo = new PDO('crate:localhost:1234/my_schema', null, null, []); |
82
|
|
|
|
83
|
|
|
$this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
84
|
|
|
$this->assertInstanceOf('PDO', $pdo); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @covers ::__construct |
89
|
|
|
*/ |
90
|
|
|
public function testInstantiationWithTraversableOptions() |
91
|
|
|
{ |
92
|
|
|
$pdo = new PDO('crate:localhost:1234', null, null, new \ArrayObject([PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION])); |
|
|
|
|
93
|
|
|
$this->assertEquals(PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(PDO::ATTR_ERRMODE)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @covers ::__construct |
98
|
|
|
*/ |
99
|
|
|
public function testInstantiationWithInvalidOptions() |
100
|
|
|
{ |
101
|
|
|
$this->setExpectedException('Crate\Stdlib\Exception\InvalidArgumentException'); |
102
|
|
|
|
103
|
|
|
new PDO('crate:localhost:1234', null, null, 'a invalid value'); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @covers ::__construct |
108
|
|
|
*/ |
109
|
|
|
public function testInstantiationWithHttpAuth() { |
110
|
|
|
$user = 'crate'; |
111
|
|
|
$passwd = 'secret'; |
112
|
|
|
$pdo = new PDO('crate:localhost:44200', $user, $passwd, []); |
113
|
|
|
$this->assertEquals([$user, $passwd], $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers ::setAttribute |
118
|
|
|
*/ |
119
|
|
|
public function testSetAttributeWithHttpBasicAuth() |
120
|
|
|
{ |
121
|
|
|
$user = 'crate'; |
122
|
|
|
$passwd = 'secret'; |
123
|
|
|
$expectedCredentials = [$user, $passwd]; |
124
|
|
|
|
125
|
|
|
$client = $this->getMock(ClientInterface::class); |
126
|
|
|
$pdo = new PDO('crate:localhost:44200', null, null, []); |
127
|
|
|
$reflection = new ReflectionClass(PDO::class); |
128
|
|
|
$property = $reflection->getProperty('client'); |
129
|
|
|
$property->setAccessible(true); |
130
|
|
|
$property->setValue($pdo, $client); |
131
|
|
|
$client |
132
|
|
|
->expects($this->once()) |
133
|
|
|
->method('setHttpBasicAuth') |
134
|
|
|
->with($user, $passwd); |
135
|
|
|
|
136
|
|
|
$pdo->setAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH, [$user, $passwd]); |
137
|
|
|
$this->assertEquals($expectedCredentials, $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @covers ::getAttribute |
142
|
|
|
*/ |
143
|
|
|
public function testGetAttributeWithInvalidAttribute() |
144
|
|
|
{ |
145
|
|
|
$this->setExpectedException(PDOException::class); |
146
|
|
|
$this->pdo->getAttribute('I DONT EXIST'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers ::setAttribute |
151
|
|
|
*/ |
152
|
|
|
public function testSetAttributeWithInvalidAttribute() |
153
|
|
|
{ |
154
|
|
|
$this->setExpectedException(PDOException::class); |
155
|
|
|
$this->pdo->setAttribute('I DONT EXIST', 'value'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @covers ::getAttribute |
160
|
|
|
* @covers ::setAttribute |
161
|
|
|
*/ |
162
|
|
|
public function testGetAndSetDefaultFetchMode() |
163
|
|
|
{ |
164
|
|
|
$this->assertEquals(PDO::FETCH_BOTH, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE)); |
165
|
|
|
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
166
|
|
|
$this->assertEquals(PDO::FETCH_ASSOC, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @covers ::getAttribute |
171
|
|
|
* @covers ::setAttribute |
172
|
|
|
*/ |
173
|
|
|
public function testGetAndSetErrorMode() |
174
|
|
|
{ |
175
|
|
|
$this->assertEquals(PDO::ERRMODE_SILENT, $this->pdo->getAttribute(PDO::ATTR_ERRMODE)); |
176
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
177
|
|
|
$this->assertEquals(PDO::ERRMODE_EXCEPTION, $this->pdo->getAttribute(PDO::ATTR_ERRMODE)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @covers ::getAttribute |
182
|
|
|
*/ |
183
|
|
|
public function testGetVersion() |
184
|
|
|
{ |
185
|
|
|
$this->assertEquals(PDO::VERSION, $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @covers ::getAttribute |
190
|
|
|
*/ |
191
|
|
|
public function testGetDriverName() |
192
|
|
|
{ |
193
|
|
|
$this->assertEquals(PDO::DRIVER_NAME, $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetStatementClass() |
197
|
|
|
{ |
198
|
|
|
$this->assertEquals([PDOStatement::class], $this->pdo->getAttribute(PDO::ATTR_STATEMENT_CLASS)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @covers ::getAttribute |
203
|
|
|
*/ |
204
|
|
|
public function testPersistent() |
205
|
|
|
{ |
206
|
|
|
$this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PERSISTENT)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @covers ::getAttribute |
211
|
|
|
*/ |
212
|
|
|
public function testPreFetch() |
213
|
|
|
{ |
214
|
|
|
$this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PREFETCH)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @covers ::getAttribute |
219
|
|
|
*/ |
220
|
|
|
public function testAutoCommit() |
221
|
|
|
{ |
222
|
|
|
$this->assertTrue($this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @covers ::getAttribute |
227
|
|
|
* @covers ::setAttribute |
228
|
|
|
*/ |
229
|
|
|
public function testGetAndSetTimeout() |
230
|
|
|
{ |
231
|
|
|
$timeout = 3; |
232
|
|
|
|
233
|
|
|
$this->client |
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('setTimeout') |
236
|
|
|
->with($timeout); |
237
|
|
|
|
238
|
|
|
$this->assertEquals(5, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT)); |
239
|
|
|
|
240
|
|
|
$this->pdo->setAttribute(PDO::ATTR_TIMEOUT, $timeout); |
241
|
|
|
|
242
|
|
|
$this->assertEquals($timeout, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @covers ::quote |
247
|
|
|
*/ |
248
|
|
|
public function testQuote() |
249
|
|
|
{ |
250
|
|
|
$this->assertTrue($this->pdo->quote('1', PDO::PARAM_BOOL)); |
251
|
|
|
$this->assertFalse($this->pdo->quote('0', PDO::PARAM_BOOL)); |
252
|
|
|
|
253
|
|
|
$this->assertEquals(100, $this->pdo->quote('100', PDO::PARAM_INT)); |
254
|
|
|
$this->assertNull($this->pdo->quote('helloWorld', PDO::PARAM_NULL)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function quoteExceptionProvider() |
261
|
|
|
{ |
262
|
|
|
return [ |
263
|
|
|
[PDO::PARAM_LOB, PDOException::class, 'This is not supported by crate.io'], |
264
|
|
|
[PDO::PARAM_STR, PDOException::class, 'This is not supported, please use prepared statements.'], |
265
|
|
|
[120, InvalidArgumentException::class, 'Unknown param type'], |
266
|
|
|
]; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @dataProvider quoteExceptionProvider |
271
|
|
|
* @covers ::quote |
272
|
|
|
* |
273
|
|
|
* @param int $paramType |
274
|
|
|
* @param string $message |
275
|
|
|
*/ |
276
|
|
|
public function testQuoteWithExpectedException($paramType, $exception, $message) |
277
|
|
|
{ |
278
|
|
|
$this->setExpectedException($exception, $message); |
279
|
|
|
$this->pdo->quote('helloWorld', $paramType); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @covers ::prepare |
284
|
|
|
*/ |
285
|
|
|
public function testPrepareReturnsAPDOStatement() |
286
|
|
|
{ |
287
|
|
|
$statement = $this->pdo->prepare('SELECT * FROM tweets'); |
288
|
|
|
$this->assertInstanceOf(PDOStatement::class, $statement); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @covers ::getAvailableDrivers |
293
|
|
|
*/ |
294
|
|
|
public function testAvailableDriversContainsCrate() |
295
|
|
|
{ |
296
|
|
|
$this->assertContains('crate', PDO::getAvailableDrivers()); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @covers ::beginTransaction |
301
|
|
|
*/ |
302
|
|
|
public function testBeginTransactionThrowsUnsupportedException() |
303
|
|
|
{ |
304
|
|
|
$this->assertTrue($this->pdo->beginTransaction()); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @covers ::commit |
309
|
|
|
*/ |
310
|
|
|
public function testCommitThrowsUnsupportedException() |
311
|
|
|
{ |
312
|
|
|
$this->assertTrue($this->pdo->commit()); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @covers ::rollback |
317
|
|
|
*/ |
318
|
|
|
public function testRollbackThrowsUnsupportedException() |
319
|
|
|
{ |
320
|
|
|
$this->setExpectedException(UnsupportedException::class); |
321
|
|
|
$this->pdo->rollBack(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @covers ::inTransaction |
326
|
|
|
*/ |
327
|
|
|
public function testInTransactionThrowsUnsupportedException() |
328
|
|
|
{ |
329
|
|
|
$this->assertFalse($this->pdo->inTransaction()); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @covers ::lastInsertId |
334
|
|
|
*/ |
335
|
|
|
public function testLastInsertIdThrowsUnsupportedException() |
336
|
|
|
{ |
337
|
|
|
$this->setExpectedException(UnsupportedException::class); |
338
|
|
|
$this->pdo->lastInsertId(); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
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: