Completed
Push — master ( 212c5f...8d6a89 )
by Aimeos
08:59
created

PDOTest::testStmtSimpleInvalidBindParamType()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 5
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Aimeos\MW\DB;
4
5
6
class PDOTest extends \PHPUnit\Framework\TestCase
7
{
8
	private $object;
9
	private $config;
10
11
12
	protected function setUp()
13
	{
14
		$this->config = \TestHelperMw::getConfig();
15
		$this->object = new \Aimeos\MW\DB\Manager\PDO( $this->config );
16
17
		$sql = 'CREATE TABLE "mw_unit_test" ( "id" INTEGER NOT NULL, "name" VARCHAR(20) NOT NULL )';
18
19
		$conn = $this->object->acquire();
20
		$conn->create( $sql )->execute()->finish();
21
		$this->object->release( $conn );
22
	}
23
24
25
	protected function tearDown()
26
	{
27
		$sql = 'DROP TABLE "mw_unit_test"';
28
29
		$conn = $this->object->acquire();
30
		$conn->create( $sql )->execute()->finish();
31
		$this->object->release( $conn );
32
33
		unset( $this->object );
34
	}
35
36
37
	public function testTransactionCommit()
38
	{
39
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
40
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
41
42
		$conn = $this->object->acquire();
43
44
		$conn->begin();
45
		$stmt = $conn->create( $sqlinsert );
46
		$stmt->execute()->finish();
47
		$stmt->execute()->finish();
48
		$conn->commit();
49
50
		$result = $conn->create( $sqlselect )->execute();
51
52
		$rows = [];
53
		while( ( $row = $result->fetch() ) !== false ) {
54
			$rows[] = $row;
55
		}
56
57
		$result->finish();
58
59
		$this->object->release( $conn );
60
61
		$this->assertEquals( 2, count( $rows ) );
62
	}
63
64
65
	public function testTransactionCommitMultiple()
66
	{
67
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
68
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
69
70
		$conn = $this->object->acquire();
71
72
		$stmt = $conn->create( $sqlinsert );
73
74
		$conn->begin();
75
		$stmt->execute()->finish();
76
		$conn->commit();
77
78
		$conn->begin();
79
		$stmt->execute()->finish();
80
		$conn->commit();
81
82
		$result = $conn->create( $sqlselect )->execute();
83
84
		$rows = [];
85
		while( ( $row = $result->fetch() ) !== false ) {
86
			$rows[] = $row;
87
		}
88
89
		$result->finish();
90
91
		$this->object->release( $conn );
92
93
		$this->assertEquals( 2, count( $rows ) );
94
	}
95
96
97
	public function testTransactionRollback()
98
	{
99
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
100
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
101
102
		$conn = $this->object->acquire();
103
104
		$conn->begin();
105
		$stmt = $conn->create( $sqlinsert );
106
		$stmt->execute()->finish();
107
		$stmt->execute()->finish();
108
		$conn->rollback();
109
110
		$result = $conn->create( $sqlselect )->execute();
111
112
		$rows = [];
113
		while( ( $row = $result->fetch() ) !== false ) {
114
			$rows[] = $row;
115
		}
116
117
		$result->finish();
118
119
		$this->object->release( $conn );
120
121
		$this->assertEquals( 0, count( $rows ) );
122
	}
123
124
125
	public function testTransactionStackCommit()
126
	{
127
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
128
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
129
130
		$conn = $this->object->acquire();
131
132
		$conn->begin();
133
		$conn->begin();
134
135
		$stmt = $conn->create( $sqlinsert );
136
		$stmt->execute()->finish();
137
138
		$conn->rollback();
139
		$conn->commit();
140
141
		$result = $conn->create( $sqlselect )->execute();
142
143
		$rows = [];
144
		while( ( $row = $result->fetch() ) !== false ) {
145
			$rows[] = $row;
146
		}
147
148
		$result->finish();
149
150
		$this->object->release( $conn );
151
152
		$this->assertEquals( 1, count( $rows ) );
153
	}
154
155
156
	public function testTransactionStackRollback()
157
	{
158
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
159
		$sqlselect = 'SELECT "name" FROM "mw_unit_test" WHERE "name" = \'a\'';
160
161
		$conn = $this->object->acquire();
162
163
		$conn->begin();
164
		$conn->begin();
165
166
		$stmt = $conn->create( $sqlinsert );
167
		$stmt->execute()->finish();
168
169
		$conn->commit();
170
		$conn->rollback();
171
172
		$result = $conn->create( $sqlselect )->execute();
173
174
		$rows = [];
175
		while( ( $row = $result->fetch() ) !== false ) {
176
			$rows[] = $row;
177
		}
178
179
		$result->finish();
180
181
		$this->object->release( $conn );
182
183
		$this->assertEquals( 0, count( $rows ) );
184
	}
185
186
187
	public function testAffectedRows()
188
	{
189
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'a\')';
190
191
		$conn = $this->object->acquire();
192
193
		$result = $conn->create( $sqlinsert )->execute();
194
		$rows = $result->affectedRows();
195
		$result->finish();
196
197
		$this->object->release( $conn );
198
199
		$this->assertEquals( 1, $rows );
200
	}
201
202
203
	public function testStmtEscape()
204
	{
205
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, :value)';
206
207
		$conn = $this->object->acquire();
208
209
		$value = "(\\')";
210
		$sqlinsert = str_replace( ':value', '\'' . $conn->escape( $value ) . '\'', $sqlinsert );
211
		$stmt = $conn->create( $sqlinsert );
212
		$stmt->execute()->finish();
213
214
		$this->object->release( $conn );
215
216
		$this->assertRegexp( '/^INSERT INTO "mw_unit_test" \("id", "name"\) VALUES \(1, \'\(.*\\\'\)\'\)$/', strval( $stmt ) );
217
	}
218
219
220
	public function testStmtSimpleBindOne()
221
	{
222
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, ?)';
223
224
		$conn = $this->object->acquire();
225
226
		$stmt = $conn->create( $sqlinsert );
227
		$stmt->bind( 1, 'test' );
228
		$stmt->execute()->finish();
229
230
		$this->object->release( $conn );
231
232
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'test\')', strval( $stmt ) );
233
	}
234
235
236
	public function testStmtSimpleBindTwo()
237
	{
238
		$sqlinsert2 =  'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
239
240
		$conn = $this->object->acquire();
241
242
		$stmt2 = $conn->create( $sqlinsert2 );
243
		$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
244
		$stmt2->bind( 2, 0.12 );
245
		$stmt2->execute()->finish();
246
247
		$this->object->release( $conn );
248
249
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'0.12\')', strval( $stmt2 ) );
250
	}
251
252
253
	public function testStmtSimpleBindThree()
254
	{
255
		$sqlinsert3 =  'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', ?)';
256
257
		$conn = $this->object->acquire();
258
259
		$stmt2 = $conn->create( $sqlinsert3 );
260
		$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
261
		$stmt2->execute()->finish();
262
263
		$this->object->release( $conn );
264
265
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', 1)', strval( $stmt2 ) );
266
	}
267
268
269
	public function testStmtSimpleInvalidBindParamType()
270
	{
271
		$sqlinsert2 =  'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
272
273
		$conn = $this->object->acquire();
274
275
		try
276
		{
277
			$stmt2 = $conn->create( $sqlinsert2 );
278
			$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
279
			$stmt2->bind( 2, 0.15, 123);
280
			$stmt2->execute();
281
		}
282
		catch( \Aimeos\MW\DB\Exception $de )
283
		{
284
			$this->object->release( $conn );
285
			return;
286
		}
287
288
		$this->object->release( $conn );
289
		$this->fail('An expected exception has not been raised');
290
	}
291
292
293
	public function testStmtSimpleBindInvalid()
294
	{
295
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
296
297
		$conn = $this->object->acquire();
298
299
		try {
300
			$stmt = $conn->create( $sqlinsert );
301
			$stmt->execute();
302
		} catch ( \Aimeos\MW\DB\Exception $de ) {
303
			$this->object->release( $conn );
304
			return;
305
		}
306
307
		$this->object->release( $conn );
308
		$this->fail('An expected exception has not been raised');
309
	}
310
311
312
	public function testStmtPreparedBindOne()
313
	{
314
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, ?)';
315
316
		$conn = $this->object->acquire();
317
318
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
319
		$stmt->bind( 1, 'test' );
320
		$result = $stmt->execute();
321
		$rows = $result->affectedRows();
322
		$result->finish();
323
324
		$this->object->release( $conn );
325
326
		$this->assertEquals( 1, $rows );
327
	}
328
329
330
	public function testStmtPreparedBindTwo()
331
	{
332
		$sqlinsert2 = 'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', ?)';
333
334
		$conn = $this->object->acquire();
335
336
		$stmt = $conn->create( $sqlinsert2, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
337
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
338
		$result = $stmt->execute();
339
		$rows = $result->affectedRows();
340
		$result->finish();
341
342
		$this->object->release( $conn );
343
344
		$this->assertEquals( 1, $rows );
345
	}
346
347
348
	public function testResultFetch()
349
	{
350
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
351
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
352
353
		$conn = $this->object->acquire();
354
355
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
356
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
357
		$stmt->bind( 2, 'test' );
358
		$stmt->execute()->finish();
359
360
		$stmt = $conn->create( $sqlselect );
361
		$result = $stmt->execute();
362
		$row = $result->fetch();
363
		$result->finish();
364
365
		$this->object->release( $conn );
366
367
		$this->assertEquals( array( 'id' => 1, 'name' => 'test' ), $row );
368
	}
369
370
371
	public function testMultipleResults()
372
	{
373
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
374
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
375
376
		$conn = $this->object->acquire();
377
378
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
379
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
380
		$stmt->bind( 2, 'test' );
381
		$stmt->execute()->finish();
382
383
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
384
		$stmt->bind( 2, 1, \Aimeos\MW\DB\Statement\Base::PARAM_BOOL );
385
		$stmt->execute()->finish();
386
387
		$stmt->bind( 1, 123, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
388
		$stmt->bind( 2, 0.1, \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
389
		$stmt->execute()->finish();
390
391
392
		$stmt = $conn->create( $sqlselect );
393
		$result = $stmt->execute();
394
		$resultSets = [];
395
396
		do {
397
			$resultSets[] = $result->fetch();
398
		}
399
		while( $result->nextResult() !== false );
400
401
		$result->finish();
402
403
		$this->object->release( $conn );
404
405
		$this->assertEquals( array( array( 'id' => 1, 'name' => 'test' ) ), $resultSets );
406
	}
407
408
409
	public function testWrongFieldType()
410
	{
411
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
412
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
413
414
		$conn = $this->object->acquire();
415
416
		try
417
		{
418
			$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
419
			$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
420
			$stmt->bind( 2, 'test', 123 );
421
			$stmt->execute();
422
		}
423
		catch ( \Aimeos\MW\DB\Exception $e )
424
		{
425
			$this->object->release( $conn );
426
			throw $e;
427
		}
428
	}
429
430
431
	public function testGetRawObject()
432
	{
433
		$conn = $this->object->acquire();
434
		$raw = $conn->getRawObject();
435
		$this->object->release( $conn );
436
437
		$this->assertInstanceOf( '\PDO', $raw );
438
	}
439
440
441
	public function testNonExisting()
442
	{
443
		$sql = 'SELECT * FROM "mw_non_existing"';
444
445
		$conn = $this->object->acquire();
446
447
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
448
449
		try
450
		{
451
			$conn->create( $sql )->execute()->finish();
452
		}
453
		catch ( \Aimeos\MW\DB\Exception $e )
454
		{
455
			$this->object->release( $conn );
456
			throw $e;
457
		}
458
	}
459
460
461
	public function testSqlError()
462
	{
463
		$conn = $this->object->acquire();
464
465
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
466
467
		try
468
		{
469
			$conn->create( 'SELECT *' )->execute()->finish();
470
		}
471
		catch ( \Aimeos\MW\DB\Exception $e )
472
		{
473
			$this->object->release( $conn );
474
			throw $e;
475
		}
476
	}
477
478
479
	public function testWrongStmtType()
480
	{
481
		$sql = 'SELECT * FROM "mw_unit_test"';
482
483
		$conn = $this->object->acquire();
484
485
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
486
487
		try
488
		{
489
			$conn->create( $sql, 123 );
490
		}
491
		catch (\Aimeos\MW\DB\Exception $e)
492
		{
493
			$this->object->release( $conn );
494
			throw $e;
495
		}
496
	}
497
498
499
	public function testPDOException()
500
	{
501
		$mock = $this->getMockBuilder( '\Aimeos\MW\DB\Connection\Iface' )->getMock();
502
503
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
504
		$this->object->release( $mock );
505
	}
506
507
508
	public function testDBFactory()
509
	{
510
		$this->assertInstanceOf('\\Aimeos\\MW\\DB\\Manager\\Iface', $this->object);
511
	}
512
513
514
	public function testFactoryFail()
515
	{
516
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
517
		\Aimeos\MW\DB\Factory::createManager( \TestHelperMw::getConfig(), 'notDefined' );
518
	}
519
}
520