Completed
Push — m/get-server-version ( c48523 )
by
unknown
04:39
created

PDOTest::testGetServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
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
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()
93
    {
94
        $pdo = new PDO('crate:localhost:1234', null, null, new \ArrayObject([PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]));
0 ignored issues
show
Documentation introduced by
new \ArrayObject(array(\...DO::ERRMODE_EXCEPTION)) is of type object<ArrayObject>, but the function expects a null|array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        $this->assertEquals(PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(PDO::ATTR_ERRMODE));
96
    }
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');
0 ignored issues
show
Documentation introduced by
'a invalid value' is of type string, but the function expects a null|array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
186
    {
187
        $this->assertEquals(PDO::FETCH_BOTH, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE));
188
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
189
        $this->assertEquals(PDO::FETCH_ASSOC, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE));
190
    }
191
192
    /**
193
     * @covers ::getAttribute
194
     * @covers ::setAttribute
195
     */
196
    public function testGetAndSetErrorMode()
197
    {
198
        $this->assertEquals(PDO::ERRMODE_SILENT, $this->pdo->getAttribute(PDO::ATTR_ERRMODE));
199
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
200
        $this->assertEquals(PDO::ERRMODE_EXCEPTION, $this->pdo->getAttribute(PDO::ATTR_ERRMODE));
201
    }
202
203
    /**
204
     * @covers ::getAttribute
205
     */
206
    public function testGetVersion()
207
    {
208
        $this->assertEquals(PDO::VERSION, $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION));
209
    }
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()
228
    {
229
        $this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PERSISTENT));
230
    }
231
232
    /**
233
     * @covers ::getAttribute
234
     */
235
    public function testPreFetch()
236
    {
237
        $this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PREFETCH));
238
    }
239
240
    /**
241
     * @covers ::getAttribute
242
     */
243
    public function testAutoCommit()
244
    {
245
        $this->assertTrue($this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT));
246
    }
247
248
    /**
249
     * @covers ::getAttribute
250
     * @covers ::setAttribute
251
     */
252
    public function testGetAndSetTimeout()
253
    {
254
        $timeout = 3;
255
256
        $this->client
257
            ->expects($this->once())
258
            ->method('setTimeout')
259
            ->with($timeout);
260
261
        $this->assertEquals(5, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT));
262
263
        $this->pdo->setAttribute(PDO::ATTR_TIMEOUT, $timeout);
264
265
        $this->assertEquals($timeout, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT));
266
    }
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()
284
    {
285
        return [
286
            [PDO::PARAM_LOB, PDOException::class, 'This is not supported by crate.io'],
287
            [PDO::PARAM_STR, PDOException::class, 'This is not supported, please use prepared statements.'],
288
            [120, InvalidArgumentException::class, 'Unknown param type'],
289
        ];
290
    }
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)
300
    {
301
        $this->setExpectedException($exception, $message);
302
        $this->pdo->quote('helloWorld', $paramType);
303
    }
304
305
    /**
306
     * @covers ::prepare
307
     */
308
    public function testPrepareReturnsAPDOStatement()
309
    {
310
        $statement = $this->pdo->prepare('SELECT * FROM tweets');
311
        $this->assertInstanceOf(PDOStatement::class, $statement);
312
    }
313
314
    /**
315
     * @covers ::getAvailableDrivers
316
     */
317
    public function testAvailableDriversContainsCrate()
318
    {
319
        $this->assertContains('crate', PDO::getAvailableDrivers());
320
    }
321
322
    /**
323
     * @covers ::beginTransaction
324
     */
325
    public function testBeginTransactionThrowsUnsupportedException()
326
    {
327
        $this->assertTrue($this->pdo->beginTransaction());
328
    }
329
330
    /**
331
     * @covers ::commit
332
     */
333
    public function testCommitThrowsUnsupportedException()
334
    {
335
        $this->assertTrue($this->pdo->commit());
336
    }
337
338
    /**
339
     * @covers ::rollback
340
     */
341
    public function testRollbackThrowsUnsupportedException()
342
    {
343
        $this->setExpectedException(UnsupportedException::class);
344
        $this->pdo->rollBack();
345
    }
346
347
    /**
348
     * @covers ::inTransaction
349
     */
350
    public function testInTransactionThrowsUnsupportedException()
351
    {
352
        $this->assertFalse($this->pdo->inTransaction());
353
    }
354
355
    /**
356
     * @covers ::lastInsertId
357
     */
358
    public function testLastInsertIdThrowsUnsupportedException()
359
    {
360
        $this->setExpectedException(UnsupportedException::class);
361
        $this->pdo->lastInsertId();
362
    }
363
364
    /**
365
     * @covers ::lastInsertId
366
     */
367
    public function testGetServerVersion()
368
    {
369
        $this->setExpectedException(UnsupportedException::class);
370
        $this->pdo->lastInsertId();
371
    }
372
}
373