Completed
Pull Request — default-schema (#32)
by
unknown
20:57 queued 11:05
created

PDOTest::testSetAttributeWithDefaultSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4286
cc 1
eloc 13
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\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]));
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...
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');
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...
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 ::setAttribute
142
     */
143
    public function testSetAttributeWithDefaultSchema()
144
    {
145
        $client = $this->getMock(ClientInterface::class);
146
        $pdo = new PDO('crate:localhost:44200/my_schema', null, null, []);
147
        $reflection = new ReflectionClass(PDO::class);
148
        $property = $reflection->getProperty('client');
149
        $property->setAccessible(true);
150
        $property->setValue($pdo, $client);
151
        $client
152
            ->expects($this->once())
153
            ->method('setHttpHeader')
154
            ->with('default-schema', 'my_schema');
155
156
        $pdo->setAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA, 'my_schema');
157
        $this->assertEquals('my_schema', $pdo->getAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA));
158
    }
159
160
    /**
161
     * @covers ::getAttribute
162
     */
163
    public function testGetAttributeWithInvalidAttribute()
164
    {
165
        $this->setExpectedException(PDOException::class);
166
        $this->pdo->getAttribute('I DONT EXIST');
167
    }
168
169
    /**
170
     * @covers ::setAttribute
171
     */
172
    public function testSetAttributeWithInvalidAttribute()
173
    {
174
        $this->setExpectedException(PDOException::class);
175
        $this->pdo->setAttribute('I DONT EXIST', 'value');
176
    }
177
178
    /**
179
     * @covers ::getAttribute
180
     * @covers ::setAttribute
181
     */
182
    public function testGetAndSetDefaultFetchMode()
183
    {
184
        $this->assertEquals(PDO::FETCH_BOTH, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE));
185
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
186
        $this->assertEquals(PDO::FETCH_ASSOC, $this->pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE));
187
    }
188
189
    /**
190
     * @covers ::getAttribute
191
     * @covers ::setAttribute
192
     */
193
    public function testGetAndSetErrorMode()
194
    {
195
        $this->assertEquals(PDO::ERRMODE_SILENT, $this->pdo->getAttribute(PDO::ATTR_ERRMODE));
196
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
197
        $this->assertEquals(PDO::ERRMODE_EXCEPTION, $this->pdo->getAttribute(PDO::ATTR_ERRMODE));
198
    }
199
200
    /**
201
     * @covers ::getAttribute
202
     */
203
    public function testGetVersion()
204
    {
205
        $this->assertEquals(PDO::VERSION, $this->pdo->getAttribute(PDO::ATTR_CLIENT_VERSION));
206
    }
207
208
    /**
209
     * @covers ::getAttribute
210
     */
211
    public function testGetDriverName()
212
    {
213
        $this->assertEquals(PDO::DRIVER_NAME, $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
214
    }
215
216
    public function testGetStatementClass()
217
    {
218
        $this->assertEquals([PDOStatement::class], $this->pdo->getAttribute(PDO::ATTR_STATEMENT_CLASS));
219
    }
220
221
    /**
222
     * @covers ::getAttribute
223
     */
224
    public function testPersistent()
225
    {
226
        $this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PERSISTENT));
227
    }
228
229
    /**
230
     * @covers ::getAttribute
231
     */
232
    public function testPreFetch()
233
    {
234
        $this->assertFalse($this->pdo->getAttribute(PDO::ATTR_PREFETCH));
235
    }
236
237
    /**
238
     * @covers ::getAttribute
239
     */
240
    public function testAutoCommit()
241
    {
242
        $this->assertTrue($this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT));
243
    }
244
245
    /**
246
     * @covers ::getAttribute
247
     * @covers ::setAttribute
248
     */
249
    public function testGetAndSetTimeout()
250
    {
251
        $timeout = 3;
252
253
        $this->client
254
            ->expects($this->once())
255
            ->method('setTimeout')
256
            ->with($timeout);
257
258
        $this->assertEquals(5, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT));
259
260
        $this->pdo->setAttribute(PDO::ATTR_TIMEOUT, $timeout);
261
262
        $this->assertEquals($timeout, $this->pdo->getAttribute(PDO::ATTR_TIMEOUT));
263
    }
264
265
    /**
266
     * @covers ::quote
267
     */
268
    public function testQuote()
269
    {
270
        $this->assertTrue($this->pdo->quote('1', PDO::PARAM_BOOL));
271
        $this->assertFalse($this->pdo->quote('0', PDO::PARAM_BOOL));
272
273
        $this->assertEquals(100, $this->pdo->quote('100', PDO::PARAM_INT));
274
        $this->assertNull($this->pdo->quote('helloWorld', PDO::PARAM_NULL));
275
    }
276
277
    /**
278
     * @return array
279
     */
280
    public function quoteExceptionProvider()
281
    {
282
        return [
283
            [PDO::PARAM_LOB, PDOException::class, 'This is not supported by crate.io'],
284
            [PDO::PARAM_STR, PDOException::class, 'This is not supported, please use prepared statements.'],
285
            [120, InvalidArgumentException::class, 'Unknown param type'],
286
        ];
287
    }
288
289
    /**
290
     * @dataProvider quoteExceptionProvider
291
     * @covers ::quote
292
     *
293
     * @param int $paramType
294
     * @param string $message
295
     */
296
    public function testQuoteWithExpectedException($paramType, $exception, $message)
297
    {
298
        $this->setExpectedException($exception, $message);
299
        $this->pdo->quote('helloWorld', $paramType);
300
    }
301
302
    /**
303
     * @covers ::prepare
304
     */
305
    public function testPrepareReturnsAPDOStatement()
306
    {
307
        $statement = $this->pdo->prepare('SELECT * FROM tweets');
308
        $this->assertInstanceOf(PDOStatement::class, $statement);
309
    }
310
311
    /**
312
     * @covers ::getAvailableDrivers
313
     */
314
    public function testAvailableDriversContainsCrate()
315
    {
316
        $this->assertContains('crate', PDO::getAvailableDrivers());
317
    }
318
319
    /**
320
     * @covers ::beginTransaction
321
     */
322
    public function testBeginTransactionThrowsUnsupportedException()
323
    {
324
        $this->assertTrue($this->pdo->beginTransaction());
325
    }
326
327
    /**
328
     * @covers ::commit
329
     */
330
    public function testCommitThrowsUnsupportedException()
331
    {
332
        $this->assertTrue($this->pdo->commit());
333
    }
334
335
    /**
336
     * @covers ::rollback
337
     */
338
    public function testRollbackThrowsUnsupportedException()
339
    {
340
        $this->setExpectedException(UnsupportedException::class);
341
        $this->pdo->rollBack();
342
    }
343
344
    /**
345
     * @covers ::inTransaction
346
     */
347
    public function testInTransactionThrowsUnsupportedException()
348
    {
349
        $this->assertFalse($this->pdo->inTransaction());
350
    }
351
352
    /**
353
     * @covers ::lastInsertId
354
     */
355
    public function testLastInsertIdThrowsUnsupportedException()
356
    {
357
        $this->setExpectedException(UnsupportedException::class);
358
        $this->pdo->lastInsertId();
359
    }
360
}
361