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

DBALTest::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 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 testStmtSimpleBind()
229
	{
230
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
231
232
		$conn = $this->object->acquire();
233
234
		$stmt = $conn->create( $sqlinsert );
235
		$stmt->bind( 1, 'test' );
236
		$stmt->execute()->finish();
237
238
		$this->object->release( $conn );
239
240
		$this->assertEquals( 'INSERT INTO "mw_unit_test" ("name") VALUES (\'test\')', strval( $stmt ) );
241
	}
242
243
244
	public function testStmtSimpleInvalidBindParamType()
245
	{
246
		$sqlinsert2 =  'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
247
248
		$conn = $this->object->acquire();
249
250
		try
251
		{
252
			$stmt2 = $conn->create( $sqlinsert2 );
253
			$stmt2->bind( 1, 1, \Aimeos\MW\DB\Statement\Base::PARAM_INT);
254
			$stmt2->bind( 2, 0.15, 123);
255
			$stmt2->execute();
256
		}
257
		catch( \Aimeos\MW\DB\Exception $de )
258
		{
259
			$this->object->release( $conn );
260
			return;
261
		}
262
263
		$this->object->release( $conn );
264
		$this->fail('An expected exception has not been raised');
265
	}
266
267
268
	public function testStmtSimpleBindInvalid()
269
	{
270
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
271
272
		$conn = $this->object->acquire();
273
274
		try {
275
			$stmt = $conn->create( $sqlinsert );
276
			$stmt->execute();
277
		} catch ( \Aimeos\MW\DB\Exception $de ) {
278
			$this->object->release( $conn );
279
			return;
280
		}
281
282
		$this->object->release( $conn );
283
		$this->fail('An expected exception has not been raised');
284
	}
285
286
287
	public function testStmtPreparedBind()
288
	{
289
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("name") VALUES (?)';
290
291
		$conn = $this->object->acquire();
292
293
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
294
		$stmt->bind( 1, 'test' );
295
		$result = $stmt->execute();
296
		$rows = $result->affectedRows();
297
		$result->finish();
298
299
		$this->object->release( $conn );
300
301
		$this->assertEquals( 1, $rows );
302
	}
303
304
305
	public function testResultFetch()
306
	{
307
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
308
		$sqlselect = 'SELECT * FROM "mw_unit_test"';
309
310
		$conn = $this->object->acquire();
311
312
		$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
313
		$stmt->bind( 1, 1 );
314
		$stmt->bind( 2, 'test' );
315
		$stmt->execute()->finish();
316
317
		$stmt = $conn->create( $sqlselect );
318
		$result = $stmt->execute();
319
		$row = $result->fetch();
320
		$result->finish();
321
322
		$this->object->release( $conn );
323
324
		$this->assertEquals( array( 'id' => 1, 'name' => 'test' ), $row );
325
	}
326
327
328
	public function testWrongFieldType()
329
	{
330
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
331
		$sqlinsert = 'INSERT INTO "mw_unit_test" ("id", "name") VALUES (?, ?)';
332
333
		$conn = $this->object->acquire();
334
335
		try
336
		{
337
			$stmt = $conn->create( $sqlinsert, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
338
			$stmt->bind( 1, 1 );
339
			$stmt->bind( 2, 'test', 123 );
340
			$stmt->execute();
341
		}
342
		catch ( \Aimeos\MW\DB\Exception $e )
343
		{
344
			$this->object->release( $conn );
345
			throw $e;
346
		}
347
	}
348
349
350
	public function testGetRawObject()
351
	{
352
		$conn = $this->object->acquire();
353
		$raw = $conn->getRawObject();
354
		$this->object->release( $conn );
355
356
		$this->assertInstanceOf( '\Doctrine\DBAL\Connection', $raw );
357
	}
358
359
360
	public function testNonExisting()
361
	{
362
		$sql = 'SELECT * FROM "mw_non_existing"';
363
364
		$conn = $this->object->acquire();
365
366
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
367
368
		try
369
		{
370
			$conn->create( $sql )->execute()->finish();
371
		}
372
		catch ( \Aimeos\MW\DB\Exception $e )
373
		{
374
			$this->object->release( $conn );
375
			throw $e;
376
		}
377
	}
378
379
380
	public function testSqlError()
381
	{
382
		$conn = $this->object->acquire();
383
384
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
385
386
		try
387
		{
388
			$conn->create( 'SELECT *' )->execute()->finish();
389
		}
390
		catch ( \Aimeos\MW\DB\Exception $e )
391
		{
392
			$this->object->release( $conn );
393
			throw $e;
394
		}
395
	}
396
397
398
	public function testWrongStmtType()
399
	{
400
		$sql = 'SELECT * FROM "mw_unit_test"';
401
402
		$conn = $this->object->acquire();
403
404
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
405
406
		try
407
		{
408
			$conn->create( $sql, 123 );
409
		}
410
		catch (\Aimeos\MW\DB\Exception $e)
411
		{
412
			$this->object->release( $conn );
413
			throw $e;
414
		}
415
	}
416
417
418
	public function testDBALException()
419
	{
420
		$mock = $this->getMockBuilder( '\Aimeos\MW\DB\Connection\Iface' )->getMock();
421
422
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
423
		$this->object->release( $mock );
424
	}
425
426
427
	public function testDBFactory()
428
	{
429
		$this->assertInstanceOf('\\Aimeos\\MW\\DB\\Manager\\Iface', $this->object);
430
	}
431
432
433
	public function testFactoryFail()
434
	{
435
		$this->setExpectedException('\\Aimeos\\MW\\DB\\Exception');
436
		\Aimeos\MW\DB\Factory::createManager( \TestHelperMw::getConfig(), 'notDefined' );
437
	}
438
}
439