1 | <?php |
||
43 | class PDOTest extends PHPUnit_Framework_TestCase |
||
44 | { |
||
45 | /** |
||
46 | * @var PDO |
||
47 | */ |
||
48 | protected $pdo; |
||
49 | |||
50 | /** |
||
51 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
52 | */ |
||
53 | protected $client; |
||
54 | |||
55 | protected function setUp() |
||
56 | { |
||
57 | $this->client = $this->getMockBuilder(Client::class) |
||
58 | ->disableOriginalConstructor() |
||
59 | ->getMock(); |
||
60 | |||
61 | $this->pdo = new PDO('crate:localhost:1234', null, null, []); |
||
62 | |||
63 | $reflection = new ReflectionClass(PDO::class); |
||
64 | |||
65 | $property = $reflection->getProperty('client'); |
||
66 | $property->setAccessible(true); |
||
67 | $property->setValue($this->pdo, $this->client); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @covers ::__construct |
||
72 | */ |
||
73 | public function testInstantiation() |
||
74 | { |
||
75 | $pdo = new PDO('crate:localhost:1234', null, null, []); |
||
76 | |||
77 | $this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
||
78 | $this->assertInstanceOf('PDO', $pdo); |
||
79 | } |
||
80 | |||
81 | public function testInstantiationWithDefaultSchema() |
||
82 | { |
||
83 | $pdo = new PDO('crate:localhost:1234/my_schema', null, null, []); |
||
84 | |||
85 | $this->assertInstanceOf('Crate\PDO\PDO', $pdo); |
||
86 | $this->assertInstanceOf('PDO', $pdo); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @covers ::__construct |
||
91 | */ |
||
92 | public function testInstantiationWithTraversableOptions() |
||
97 | |||
98 | /** |
||
99 | * @covers ::__construct |
||
100 | */ |
||
101 | public function testInstantiationWithInvalidOptions() |
||
102 | { |
||
103 | $this->setExpectedException('Crate\Stdlib\Exception\InvalidArgumentException'); |
||
104 | |||
105 | new PDO('crate:localhost:1234', null, null, 'a invalid value'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @covers ::__construct |
||
110 | */ |
||
111 | public function testInstantiationWithHttpAuth() { |
||
112 | $user = 'crate'; |
||
113 | $passwd = 'secret'; |
||
114 | $pdo = new PDO('crate:localhost:44200', $user, $passwd, []); |
||
115 | $this->assertEquals([$user, $passwd], $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @covers ::setAttribute |
||
120 | */ |
||
121 | public function testSetAttributeWithHttpBasicAuth() |
||
122 | { |
||
123 | $user = 'crate'; |
||
124 | $passwd = 'secret'; |
||
125 | $expectedCredentials = [$user, $passwd]; |
||
126 | |||
127 | $client = $this->getMock(ClientInterface::class); |
||
128 | $pdo = new PDO('crate:localhost:44200', null, null, []); |
||
129 | $reflection = new ReflectionClass(PDO::class); |
||
130 | $property = $reflection->getProperty('client'); |
||
131 | $property->setAccessible(true); |
||
132 | $property->setValue($pdo, $client); |
||
133 | $client |
||
134 | ->expects($this->once()) |
||
135 | ->method('setHttpBasicAuth') |
||
136 | ->with($user, $passwd); |
||
137 | |||
138 | $pdo->setAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH, [$user, $passwd]); |
||
139 | $this->assertEquals($expectedCredentials, $pdo->getAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH)); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @covers ::setAttribute |
||
144 | */ |
||
145 | public function testSetAttributeWithDefaultSchema() |
||
146 | { |
||
147 | $client = $this->getMockBuilder(Client::class) |
||
148 | ->disableOriginalConstructor() |
||
149 | ->getMock(); |
||
150 | $pdo = new PDO('crate:localhost:44200/my_schema', null, null, []); |
||
151 | $reflection = new ReflectionClass(PDO::class); |
||
152 | $property = $reflection->getProperty('client'); |
||
153 | $property->setAccessible(true); |
||
154 | $property->setValue($pdo, $client); |
||
155 | $client->expects($this->once()) |
||
156 | ->method('setDefaultSchema') |
||
157 | ->with('my_schema'); |
||
158 | |||
159 | $pdo->setAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA, 'my_schema'); |
||
160 | $this->assertEquals('my_schema', $pdo->getAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA)); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @covers ::getAttribute |
||
165 | */ |
||
166 | public function testGetAttributeWithInvalidAttribute() |
||
167 | { |
||
168 | $this->setExpectedException(PDOException::class); |
||
169 | $this->pdo->getAttribute('I DONT EXIST'); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @covers ::setAttribute |
||
174 | */ |
||
175 | public function testSetAttributeWithInvalidAttribute() |
||
176 | { |
||
177 | $this->setExpectedException(PDOException::class); |
||
178 | $this->pdo->setAttribute('I DONT EXIST', 'value'); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @covers ::getAttribute |
||
183 | * @covers ::setAttribute |
||
184 | */ |
||
185 | public function testGetAndSetDefaultFetchMode() |
||
191 | |||
192 | /** |
||
193 | * @covers ::getAttribute |
||
194 | * @covers ::setAttribute |
||
195 | */ |
||
196 | public function testGetAndSetErrorMode() |
||
202 | |||
203 | /** |
||
204 | * @covers ::getAttribute |
||
205 | */ |
||
206 | public function testGetVersion() |
||
210 | |||
211 | /** |
||
212 | * @covers ::getAttribute |
||
213 | */ |
||
214 | public function testGetDriverName() |
||
215 | { |
||
216 | $this->assertEquals(PDO::DRIVER_NAME, $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); |
||
217 | } |
||
218 | |||
219 | public function testGetStatementClass() |
||
220 | { |
||
221 | $this->assertEquals([PDOStatement::class], $this->pdo->getAttribute(PDO::ATTR_STATEMENT_CLASS)); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @covers ::getAttribute |
||
226 | */ |
||
227 | public function testPersistent() |
||
231 | |||
232 | /** |
||
233 | * @covers ::getAttribute |
||
234 | */ |
||
235 | public function testPreFetch() |
||
239 | |||
240 | /** |
||
241 | * @covers ::getAttribute |
||
242 | */ |
||
243 | public function testAutoCommit() |
||
247 | |||
248 | /** |
||
249 | * @covers ::getAttribute |
||
250 | * @covers ::setAttribute |
||
251 | */ |
||
252 | public function testGetAndSetTimeout() |
||
267 | |||
268 | /** |
||
269 | * @covers ::quote |
||
270 | */ |
||
271 | public function testQuote() |
||
272 | { |
||
273 | $this->assertTrue($this->pdo->quote('1', PDO::PARAM_BOOL)); |
||
274 | $this->assertFalse($this->pdo->quote('0', PDO::PARAM_BOOL)); |
||
275 | |||
276 | $this->assertEquals(100, $this->pdo->quote('100', PDO::PARAM_INT)); |
||
277 | $this->assertNull($this->pdo->quote('helloWorld', PDO::PARAM_NULL)); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @return array |
||
282 | */ |
||
283 | public function quoteExceptionProvider() |
||
291 | |||
292 | /** |
||
293 | * @dataProvider quoteExceptionProvider |
||
294 | * @covers ::quote |
||
295 | * |
||
296 | * @param int $paramType |
||
297 | * @param string $message |
||
298 | */ |
||
299 | public function testQuoteWithExpectedException($paramType, $exception, $message) |
||
304 | |||
305 | /** |
||
306 | * @covers ::prepare |
||
307 | */ |
||
308 | public function testPrepareReturnsAPDOStatement() |
||
313 | |||
314 | /** |
||
315 | * @covers ::getAvailableDrivers |
||
316 | */ |
||
317 | public function testAvailableDriversContainsCrate() |
||
321 | |||
322 | /** |
||
323 | * @covers ::beginTransaction |
||
324 | */ |
||
325 | public function testBeginTransactionThrowsUnsupportedException() |
||
329 | |||
330 | /** |
||
331 | * @covers ::commit |
||
332 | */ |
||
333 | public function testCommitThrowsUnsupportedException() |
||
337 | |||
338 | /** |
||
339 | * @covers ::rollback |
||
340 | */ |
||
341 | public function testRollbackThrowsUnsupportedException() |
||
346 | |||
347 | /** |
||
348 | * @covers ::inTransaction |
||
349 | */ |
||
350 | public function testInTransactionThrowsUnsupportedException() |
||
354 | |||
355 | /** |
||
356 | * @covers ::lastInsertId |
||
357 | */ |
||
358 | public function testLastInsertIdThrowsUnsupportedException() |
||
363 | |||
364 | /** |
||
365 | * @covers ::lastInsertId |
||
366 | */ |
||
367 | public function testGetServerVersion() |
||
372 | } |
||
373 |
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: