Passed
Branch main (45b422)
by Andreas
01:40
created

PDOTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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