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

DBALTest::testSqlError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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