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

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