Passed
Push — master ( 78ff77...606203 )
by Aimeos
04:53
created

PDOTest::testMultipleResults()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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