Passed
Push — master ( 5e32c8...fdcb24 )
by Aimeos
07:53
created

PDOTest   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 394
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 32
eloc 186
c 4
b 0
f 0
dl 0
loc 394
rs 9.84

26 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 8 1
A setUp() 0 8 1
A testStmtEscape() 0 10 1
A testTransactionStackRollback() 0 24 2
A testStmtSimpleBindTwo() 0 10 1
A testStmtSimpleBindInvalid() 0 8 1
A testTransactionCommitMultiple() 0 25 2
A testStmtSimpleBindApostrophes() 0 7 1
A testMultipleResults() 0 31 2
A testResultFetchAll() 0 16 1
A testCrud() 0 10 1
A testWrongFieldType() 0 11 1
A testResultFetch() 0 16 1
A testSqlError() 0 4 1
A testStmtSimpleBindThree() 0 9 1
A testFactoryFail() 0 4 1
A testTransactionCommit() 0 21 2
A testNonExisting() 0 6 1
A testPDOException() 0 6 1
A testStmtSimpleInvalidBindParamType() 0 10 1
A testAffectedRows() 0 9 1
A testDBFactory() 0 3 1
A testTransactionRollback() 0 21 2
A testGetRawObject() 0 3 1
A testTransactionStackCommit() 0 24 2
A testStmtSimpleBindOne() 0 9 1
1
<?php
2
3
namespace Aimeos\Base\DB;
4
5
6
class PDOTest extends \PHPUnit\Framework\TestCase
7
{
8
	private $object;
9
	private $conn;
10
11
12
	protected function setUp() : void
13
	{
14
		$this->object = new \Aimeos\Base\DB\Manager\PDO( \TestHelper::getConfig()->get( 'resource' ) );
15
16
		$sql = 'CREATE TABLE "mw_unit_test" ( "id" INTEGER NOT NULL, "name" VARCHAR(20) NOT NULL )';
17
18
		$this->conn = $this->object->acquire();
19
		$this->conn->create( $sql )->execute()->finish();
20
	}
21
22
23
	protected function tearDown() : void
24
	{
25
		$sql = 'DROP TABLE "mw_unit_test"';
26
27
		$this->conn->create( $sql )->execute()->finish();
28
		$this->object->release( $this->conn );
29
30
		unset( $this->object );
31
	}
32
33
34
	public function testCrud()
35
	{
36
		$this->conn->insert( 'mw_unit_test', ['id' => 1, 'name' => 'test'] );
37
		$this->conn->update( 'mw_unit_test', ['name' => 'test 2'], ['id' => 1] );
38
39
		$result = $this->conn->query( 'SELECT * FROM "mw_unit_test" WHERE "id"=?', [1] )->fetch();
40
41
		$this->conn->delete( 'mw_unit_test', ['id' => 1] );
42
43
		$this->assertEquals( ['id' => 1, 'name' => 'test 2'], $result );
44
	}
45
46
47
	public function testTransactionCommit()
48
	{
49
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
50
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
51
52
		$this->conn->begin();
53
		$stmt = $this->conn->create( $sqlinsert );
54
		$stmt->execute()->finish();
55
		$stmt->execute()->finish();
56
		$this->conn->commit();
57
58
		$result = $this->conn->create( $sqlselect )->execute();
59
60
		$rows = [];
61
		while( ( $row = $result->fetch() ) !== null ) {
62
			$rows[] = $row;
63
		}
64
65
		$result->finish();
66
67
		$this->assertEquals( 2, count( $rows ) );
68
	}
69
70
71
	public function testTransactionCommitMultiple()
72
	{
73
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
74
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
75
76
		$stmt = $this->conn->create( $sqlinsert );
77
78
		$this->conn->begin();
79
		$stmt->execute()->finish();
80
		$this->conn->commit();
81
82
		$this->conn->begin();
83
		$stmt->execute()->finish();
84
		$this->conn->commit();
85
86
		$result = $this->conn->create( $sqlselect )->execute();
87
88
		$rows = [];
89
		while( ( $row = $result->fetch() ) !== null ) {
90
			$rows[] = $row;
91
		}
92
93
		$result->finish();
94
95
		$this->assertEquals( 2, count( $rows ) );
96
	}
97
98
99
	public function testTransactionRollback()
100
	{
101
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
102
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
103
104
		$this->conn->begin();
105
		$stmt = $this->conn->create( $sqlinsert );
106
		$stmt->execute()->finish();
107
		$stmt->execute()->finish();
108
		$this->conn->rollback();
109
110
		$result = $this->conn->create( $sqlselect )->execute();
111
112
		$rows = [];
113
		while( ( $row = $result->fetch() ) !== null ) {
114
			$rows[] = $row;
115
		}
116
117
		$result->finish();
118
119
		$this->assertEquals( 0, count( $rows ) );
120
	}
121
122
123
	public function testTransactionStackCommit()
124
	{
125
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
126
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
127
128
		$this->conn->begin();
129
		$this->conn->begin();
130
131
		$stmt = $this->conn->create( $sqlinsert );
132
		$stmt->execute()->finish();
133
134
		$this->conn->commit();
135
		$this->conn->commit();
136
137
		$result = $this->conn->create( $sqlselect )->execute();
138
139
		$rows = [];
140
		while( ( $row = $result->fetch() ) !== null ) {
141
			$rows[] = $row;
142
		}
143
144
		$result->finish();
145
146
		$this->assertEquals( 1, count( $rows ) );
147
	}
148
149
150
	public function testTransactionStackRollback()
151
	{
152
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
153
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
154
155
		$this->conn->begin();
156
		$this->conn->begin();
157
158
		$stmt = $this->conn->create( $sqlinsert );
159
		$stmt->execute()->finish();
160
161
		$this->conn->rollback();
162
		$this->conn->rollback();
163
164
		$result = $this->conn->create( $sqlselect )->execute();
165
166
		$rows = [];
167
		while( ( $row = $result->fetch() ) !== null ) {
168
			$rows[] = $row;
169
		}
170
171
		$result->finish();
172
173
		$this->assertEquals( 0, count( $rows ) );
174
	}
175
176
177
	public function testAffectedRows()
178
	{
179
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
180
181
		$result = $this->conn->create( $sqlinsert )->execute();
182
		$rows = $result->affectedRows();
183
		$result->finish();
184
185
		$this->assertEquals( 1, $rows );
186
	}
187
188
189
	public function testStmtEscape()
190
	{
191
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, :value)';
192
193
		$value = "(\\')";
194
		$sqlinsert = str_replace( ':value', '\'' . $this->conn->escape( $value ) . '\'', $sqlinsert );
195
		$stmt = $this->conn->create( $sqlinsert );
196
		$stmt->execute()->finish();
197
198
		$this->assertRegexp( '/^INSERT INTO "mw_unit_test" \("id", "name"\) VALUES \(1, \'\(.*\\\'\)\'\)$/', strval( $stmt ) );
199
	}
200
201
202
	public function testStmtSimpleBindApostrophes()
203
	{
204
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'' . $this->conn->escape( '\'\'' ) . '\')';
205
206
		$result = $this->conn->create( $sqlinsert )->execute()->finish();
207
208
		$this->assertInstanceOf( \Aimeos\Base\DB\Result\Iface::class, $result );
209
	}
210
211
212
	public function testStmtSimpleBindOne()
213
	{
214
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, ?)';
215
216
		$stmt = $this->conn->create( $sqlinsert );
217
		$stmt->bind( 1, 'test' );
218
		$result = $stmt->execute()->finish();
219
220
		$this->assertInstanceOf( \Aimeos\Base\DB\Result\Iface::class, $result );
221
	}
222
223
224
	public function testStmtSimpleBindTwo()
225
	{
226
		$sqlinsert2 = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
227
228
		$stmt2 = $this->conn->create( $sqlinsert2 );
229
		$stmt2->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
230
		$stmt2->bind( 2, 0.12 );
231
		$result = $stmt2->execute()->finish();
232
233
		$this->assertInstanceOf( \Aimeos\Base\DB\Result\Iface::class, $result );
234
	}
235
236
237
	public function testStmtSimpleBindThree()
238
	{
239
		$sqlinsert3 = 'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', ?)';
240
241
		$stmt2 = $this->conn->create( $sqlinsert3 );
242
		$stmt2->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
243
		$result = $stmt2->execute()->finish();
244
245
		$this->assertInstanceOf( \Aimeos\Base\DB\Result\Iface::class, $result );
246
	}
247
248
249
	public function testStmtSimpleInvalidBindParamType()
250
	{
251
		$sqlinsert2 = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
252
253
		$stmt2 = $this->conn->create( $sqlinsert2 );
254
		$stmt2->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
255
		$stmt2->bind( 2, 0.15, 123 );
256
257
		$this->expectException( \Aimeos\Base\DB\Exception::class );
258
		$stmt2->execute();
259
	}
260
261
262
	public function testStmtSimpleBindInvalid()
263
	{
264
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
265
266
		$stmt = $this->conn->create( $sqlinsert );
267
268
		$this->expectException( \Aimeos\Base\DB\Exception::class );
269
		$stmt->execute();
270
	}
271
272
273
	public function testResultFetch()
274
	{
275
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
276
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
277
278
		$stmt = $this->conn->create( $sqlinsert );
279
		$stmt->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
280
		$stmt->bind( 2, 'test' );
281
		$stmt->execute()->finish();
282
283
		$stmt = $this->conn->create( $sqlselect );
284
		$result = $stmt->execute();
285
		$row = $result->fetch();
286
		$result->finish();
287
288
		$this->assertEquals( array( 'id' => 1, 'name' => 'test' ), $row );
289
	}
290
291
292
	public function testResultFetchAll()
293
	{
294
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
295
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
296
297
		$stmt = $this->conn->create( $sqlinsert );
298
		$stmt->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
299
		$stmt->bind( 2, 'test' );
300
		$stmt->execute()->finish();
301
302
		$stmt = $this->conn->create( $sqlselect );
303
		$result = $stmt->execute();
304
		$rows = $result->all();
305
		$result->finish();
306
307
		$this->assertEquals( [['id' => 1, 'name' => 'test']], $rows );
308
	}
309
310
311
	public function testMultipleResults()
312
	{
313
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
314
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
315
316
		$stmt = $this->conn->create( $sqlinsert );
317
		$stmt->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
318
		$stmt->bind( 2, 'test' );
319
		$stmt->execute()->finish();
320
321
		$stmt->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
322
		$stmt->bind( 2, 1, \Aimeos\Base\DB\Statement\Base::PARAM_BOOL );
323
		$stmt->execute()->finish();
324
325
		$stmt->bind( 1, 123, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
326
		$stmt->bind( 2, 0.1, \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT );
327
		$stmt->execute()->finish();
328
329
330
		$stmt = $this->conn->create( $sqlselect );
331
		$result = $stmt->execute();
332
		$resultSets = [];
333
334
		do {
335
			$resultSets[] = $result->fetch();
336
		}
337
		while( $result->nextResult() !== false );
338
339
		$result->finish();
340
341
		$this->assertEquals( array( array( 'id' => 1, 'name' => 'test' ) ), $resultSets );
342
	}
343
344
345
	public function testWrongFieldType()
346
	{
347
		$this->expectException( \Aimeos\Base\DB\Exception::class );
348
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
349
350
		$stmt = $this->conn->create( $sqlinsert );
351
		$stmt->bind( 1, 1, \Aimeos\Base\DB\Statement\Base::PARAM_INT );
352
		$stmt->bind( 2, 'test', 123 );
353
354
		$this->expectException( \Aimeos\Base\DB\Exception::class );
355
		$stmt->execute();
356
	}
357
358
359
	public function testGetRawObject()
360
	{
361
		$this->assertInstanceOf( \PDO::class, $this->conn->getRawObject() );
362
	}
363
364
365
	public function testNonExisting()
366
	{
367
		$sql = 'SELECT * FROM "mw_non_existing"';
368
369
		$this->expectException( \Aimeos\Base\DB\Exception::class );
370
		$this->conn->create( $sql )->execute()->finish();
371
	}
372
373
374
	public function testSqlError()
375
	{
376
		$this->expectException( \Aimeos\Base\DB\Exception::class );
377
		$this->conn->create( 'SELECT *' )->execute()->finish();
378
	}
379
380
381
	public function testPDOException()
382
	{
383
		$mock = $this->getMockBuilder( \Aimeos\Base\DB\Connection\Iface::class )->getMock();
384
385
		$this->expectException( \Aimeos\Base\DB\Exception::class );
386
		$this->object->release( $mock );
387
	}
388
389
390
	public function testDBFactory()
391
	{
392
		$this->assertInstanceOf( \Aimeos\Base\DB\Manager\Iface::class, $this->object );
393
	}
394
395
396
	public function testFactoryFail()
397
	{
398
		$this->expectException( \Aimeos\Base\DB\Exception::class );
399
		\Aimeos\Base\DB\Factory::create( \TestHelper::getConfig(), 'notDefined' );
400
	}
401
}
402