Completed
Push — master ( 687f7d...ab57ba )
by Aimeos
10:53
created

PDOTest::testStmtSimpleBindApostrophes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 9.4285
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 testStmtSimpleBindApostrophes()
221
	{
222
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (\'\\\'\\\'\')';
223
224
		$conn = $this->object->acquire();
225
226
		$stmt = $conn->create( $sqlinsert );
227
		$stmt->execute()->finish();
228
229
		$this->object->release( $conn );
230
	}
231
232
233
	public function testStmtSimpleBindOne()
234
	{
235
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, ?)';
236
237
		$conn = $this->object->acquire();
238
239
		$stmt = $conn->create( $sqlinsert );
240
		$stmt->bind( 1, 'test' );
241
		$stmt->execute()->finish();
242
243
		$this->object->release( $conn );
244
245
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'test\')', strval( $stmt ) );
246
	}
247
248
249
	public function testStmtSimpleBindTwo()
250
	{
251
		$sqlinsert2 =  'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
252
253
		$conn = $this->object->acquire();
254
255
		$stmt2 = $conn->create( $sqlinsert2 );
256
		$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
257
		$stmt2->bind( 2, 0.12 );
258
		$stmt2->execute()->finish();
259
260
		$this->object->release( $conn );
261
262
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, \'0.12\')', strval( $stmt2 ) );
263
	}
264
265
266
	public function testStmtSimpleBindThree()
267
	{
268
		$sqlinsert3 =  'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', ?)';
269
270
		$conn = $this->object->acquire();
271
272
		$stmt2 = $conn->create( $sqlinsert3 );
273
		$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
274
		$stmt2->execute()->finish();
275
276
		$this->object->release( $conn );
277
278
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', 1)', strval( $stmt2 ) );
279
	}
280
281
282
	public function testStmtSimpleInvalidBindParamType()
283
	{
284
		$sqlinsert2 =  'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
285
286
		$conn = $this->object->acquire();
287
288
		try
289
		{
290
			$stmt2 = $conn->create( $sqlinsert2 );
291
			$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
292
			$stmt2->bind( 2, 0.15, 123);
293
			$stmt2->execute();
294
		}
295
		catch( \Aimeos\MW\DB\Exception $de )
296
		{
297
			$this->object->release( $conn );
298
			return;
299
		}
300
301
		$this->object->release( $conn );
302
		$this->fail('An expected exception has not been raised');
303
	}
304
305
306
	public function testStmtSimpleBindInvalid()
307
	{
308
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
309
310
		$conn = $this->object->acquire();
311
312
		try {
313
			$stmt = $conn->create( $sqlinsert );
314
			$stmt->execute();
315
		} catch ( \Aimeos\MW\DB\Exception $de ) {
316
			$this->object->release( $conn );
317
			return;
318
		}
319
320
		$this->object->release( $conn );
321
		$this->fail('An expected exception has not been raised');
322
	}
323
324
325
	public function testStmtPreparedBindOne()
326
	{
327
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (1, ?)';
328
329
		$conn = $this->object->acquire();
330
331
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
332
		$stmt->bind( 1, 'test' );
333
		$result = $stmt->execute();
334
		$rows = $result->affectedRows();
335
		$result->finish();
336
337
		$this->object->release( $conn );
338
339
		$this->assertEquals( 1, $rows );
340
	}
341
342
343
	public function testStmtPreparedBindTwo()
344
	{
345
		$sqlinsert2 = 'INSERT INTO "mw_unit_test" ("name", "id") VALUES (\'?te?st?\', ?)';
346
347
		$conn = $this->object->acquire();
348
349
		$stmt = $conn->create( $sqlinsert2, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
350
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
351
		$result = $stmt->execute();
352
		$rows = $result->affectedRows();
353
		$result->finish();
354
355
		$this->object->release( $conn );
356
357
		$this->assertEquals( 1, $rows );
358
	}
359
360
361
	public function testResultFetch()
362
	{
363
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
364
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
365
366
		$conn = $this->object->acquire();
367
368
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
369
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
370
		$stmt->bind( 2, 'test' );
371
		$stmt->execute()->finish();
372
373
		$stmt = $conn->create( $sqlselect );
374
		$result = $stmt->execute();
375
		$row = $result->fetch();
376
		$result->finish();
377
378
		$this->object->release( $conn );
379
380
		$this->assertEquals( array( 'id' => 1, 'name' => 'test' ), $row );
381
	}
382
383
384
	public function testMultipleResults()
385
	{
386
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
387
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
388
389
		$conn = $this->object->acquire();
390
391
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
392
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
393
		$stmt->bind( 2, 'test' );
394
		$stmt->execute()->finish();
395
396
		$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
397
		$stmt->bind( 2, 1, \Aimeos\MW\DB\Statement\Base::PARAM_BOOL );
398
		$stmt->execute()->finish();
399
400
		$stmt->bind( 1, 123, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
401
		$stmt->bind( 2, 0.1, \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
402
		$stmt->execute()->finish();
403
404
405
		$stmt = $conn->create( $sqlselect );
406
		$result = $stmt->execute();
407
		$resultSets = [];
408
409
		do {
410
			$resultSets[] = $result->fetch();
411
		}
412
		while( $result->nextResult() !== false );
413
414
		$result->finish();
415
416
		$this->object->release( $conn );
417
418
		$this->assertEquals( array( array( 'id' => 1, 'name' => 'test' ) ), $resultSets );
419
	}
420
421
422
	public function testWrongFieldType()
423
	{
424
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
425
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
426
427
		$conn = $this->object->acquire();
428
429
		try
430
		{
431
			$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
432
			$stmt->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
433
			$stmt->bind( 2, 'test', 123 );
434
			$stmt->execute();
435
		}
436
		catch ( \Aimeos\MW\DB\Exception $e )
437
		{
438
			$this->object->release( $conn );
439
			throw $e;
440
		}
441
	}
442
443
444
	public function testGetRawObject()
445
	{
446
		$conn = $this->object->acquire();
447
		$raw = $conn->getRawObject();
448
		$this->object->release( $conn );
449
450
		$this->assertInstanceOf( '\PDO', $raw );
451
	}
452
453
454
	public function testNonExisting()
455
	{
456
		$sql = 'SELECT * FROM "mw_non_existing"';
457
458
		$conn = $this->object->acquire();
459
460
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
461
462
		try
463
		{
464
			$conn->create( $sql )->execute()->finish();
465
		}
466
		catch ( \Aimeos\MW\DB\Exception $e )
467
		{
468
			$this->object->release( $conn );
469
			throw $e;
470
		}
471
	}
472
473
474
	public function testSqlError()
475
	{
476
		$conn = $this->object->acquire();
477
478
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
479
480
		try
481
		{
482
			$conn->create( 'SELECT *' )->execute()->finish();
483
		}
484
		catch ( \Aimeos\MW\DB\Exception $e )
485
		{
486
			$this->object->release( $conn );
487
			throw $e;
488
		}
489
	}
490
491
492
	public function testWrongStmtType()
493
	{
494
		$sql = 'SELECT * FROM "mw_unit_test"';
495
496
		$conn = $this->object->acquire();
497
498
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
499
500
		try
501
		{
502
			$conn->create( $sql, 123 );
503
		}
504
		catch (\Aimeos\MW\DB\Exception $e)
505
		{
506
			$this->object->release( $conn );
507
			throw $e;
508
		}
509
	}
510
511
512
	public function testPDOException()
513
	{
514
		$mock = $this->getMockBuilder( '\Aimeos\MW\DB\Connection\Iface' )->getMock();
515
516
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
517
		$this->object->release( $mock );
518
	}
519
520
521
	public function testDBFactory()
522
	{
523
		$this->assertInstanceOf('\\Aimeos\\MW\\DB\\Manager\\Iface', $this->object);
524
	}
525
526
527
	public function testFactoryFail()
528
	{
529
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
530
		\Aimeos\MW\DB\Factory::createManager( \TestHelperMw::getConfig(), 'notDefined' );
531
	}
532
}
533