Completed
Push — master ( 612476...c205d6 )
by smiley
04:22
created

DriverTestAbstract::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Class DriverTestAbstract
4
 *
5
 * @filesource   DriverTestAbstract.php
6
 * @created      29.05.2017
7
 * @package      chillerlan\DatabaseTest\Drivers
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\DatabaseTest\Drivers;
14
15
use chillerlan\Database\Drivers\DBDriverInterface;
16
use chillerlan\DatabaseTest\TestAbstract;
17
use chillerlan\SimpleCache\Cache;
18
use chillerlan\SimpleCache\Drivers\MemoryCacheDriver;
19
use Psr\SimpleCache\CacheInterface;
20
21
abstract class DriverTestAbstract extends TestAbstract{
22
23
	/**
24
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
25
	 */
26
	protected $DBDriver;
27
28
	/**
29
	 * @var \Psr\SimpleCache\CacheInterface
30
	 */
31
	protected $Cache;
32
33
	protected $driver;
34
35
	protected $SQL_RAW_TRUNCATE   = 'DELETE FROM test';
36
	protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test';
37
	protected $SQL_RAW_DROP       = 'DROP TABLE IF EXISTS test';
38
	protected $SQL_RAW_CREATE     = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)';
39
	protected $SQL_RAW_INSERT     = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')';
40
	protected $SQL_RAW_ERROR      = '-';
41
42
	protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)';
43
44
	protected $SQL_INDEX_COL = 'id';
45
	protected $SQL_DATA_COL  = 'hash';
46
47
	protected $SQL_ESCAPED;
48
49
	protected function setUp(){
50
		parent::setUp();
51
52
		$this->Cache = new Cache(new MemoryCacheDriver);
53
		$this->DBDriver = (new $this->driver($this->DBOptions, $this->Cache))->connect();
54
	}
55
56
	protected function tearDown(){
57
		$this->assertTrue($this->DBDriver->disconnect());
58
	}
59
60
	public function testInstance(){
61
		$this->assertInstanceOf(DBDriverInterface::class, $this->DBDriver);
62
		$this->assertInstanceOf(CacheInterface::class, $this->Cache);
63
		$this->assertSame($this->DBDriver, $this->DBDriver->connect());
64
		$this->assertNotNull($this->DBDriver->getDBResource());
65
#/*
66
		print_r([
67
			'driver' => get_class($this->DBDriver),
68
			'client' => $this->DBDriver->getClientInfo(),
69
			'server' => $this->DBDriver->getServerInfo(),
70
		]);
71
#*/
72
	}
73
74
	/**
75
	 * @expectedException \chillerlan\Database\DBException
76
	 * @expectedExceptionMessage db error:
77
	 */
78
	public function testConnectDBconnectError(){
79
		$this->DBOptions->host     = '...';
80
		$this->DBOptions->database = '...';
81
		(new $this->driver($this->DBOptions))->connect();
82
	}
83
84
	// coverage
85
	public function testEscape(){
86
		$this->assertSame($this->SQL_ESCAPED, $this->DBDriver->escape("'\\\''"));
87
	}
88
89
	public function testCreateTable(){
90
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_DROP));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_DROP) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_CREATE));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_CREATE) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
	}
93
94
	public function testTruncate(){
95
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_TRUNCATE) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
96
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_SELECT_ALL));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
	}
98
99
	/**
100
	 * @expectedException \chillerlan\Database\DBException
101
	 * @expectedExceptionMessage sql error:
102
	 */
103
	public function testRawSQLError(){
104
		$this->DBDriver->raw($this->SQL_RAW_ERROR);
105
	}
106
107
	protected function resultTest(){
108
		$DBResult         = $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL);
109
		$DBResultPrepared = $this->DBDriver->prepared($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); // coverage
110
111
		$this->assertEquals($DBResult, $DBResultPrepared);
112
113
		$this->assertCount(10, $DBResult);
114
115
		$DBResult->__each(
116
			function($row, $i){
117
				/** @var \chillerlan\Database\DBResultRow $row */
118
				$this->assertEquals($row->{$this->SQL_INDEX_COL}, $i);
119
				$this->assertSame(md5($row->{$this->SQL_INDEX_COL}), $row->{$this->SQL_DATA_COL}());
120
121
				$row->__each(
122
					function($v, $j) use ($row){
123
						$this->assertEquals($row->{$j}, $v);
124
						$this->assertEquals($row[$j], $v);
125
					}
126
				);
127
			}
128
		);
129
130
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_TRUNCATE) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
131
	}
132
133
	public function testRaw(){
134
135
		// don't try this at home!
136
		foreach(range(0, 9) as $k){
137
			$this->assertTrue($this->DBDriver->raw(sprintf($this->SQL_RAW_INSERT, $k, md5($k))));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw(spr...W_INSERT, $k, md5($k))) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
138
		}
139
140
		$this->resultTest();
141
	}
142
143
	/**
144
	 * @expectedException \chillerlan\Database\DBException
145
	 * @expectedExceptionMessage sql error:
146
	 */
147
	public function testPreparedSQLError(){
148
		$this->DBDriver->prepared($this->SQL_RAW_ERROR);
149
	}
150
151 View Code Duplication
	public function testPrepared(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
153
		foreach(range(0, 9) as $k){
154
			$this->assertTrue($this->DBDriver->prepared($this->SQL_PREPARED_INSERT, [$k, md5($k)]));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->prepare...RT, array($k, md5($k))) targeting chillerlan\Database\Driv...erInterface::prepared() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
155
		}
156
157
		$this->resultTest();
158
	}
159
160
	/**
161
	 * @expectedException \chillerlan\Database\DBException
162
	 * @expectedExceptionMessage invalid data
163
	 */
164
	public function testMultiInvalidData(){
165
		$this->DBDriver->multi($this->SQL_RAW_ERROR, []);
166
	}
167
168
	/**
169
	 * @expectedException \chillerlan\Database\DBException
170
	 * @expectedExceptionMessage sql error:
171
	 */
172
	public function testMultiSQLError(){
173
		$this->DBDriver->multi($this->SQL_RAW_ERROR, [[0]]);
174
	}
175
176 View Code Duplication
	public function testMulti(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
178
		$this->assertTrue(
179
			$this->DBDriver->multi(
180
				$this->SQL_PREPARED_INSERT, array_map(
181
				function($k){
182
					return [$k, md5($k)];
183
				}, range(0, 9)
184
			)
185
			)
186
		);
187
188
		$this->resultTest();
189
	}
190
191
	/**
192
	 * @expectedException \chillerlan\Database\DBException
193
	 * @expectedExceptionMessage invalid data
194
	 */
195
	public function testMultiCallbackInvalidData(){
196
		$this->DBDriver->multi_callback(
197
			$this->SQL_RAW_ERROR, [], function(){
198
		});
199
	}
200
201
	/**
202
	 * @expectedException \chillerlan\Database\DBException
203
	 * @expectedExceptionMessage invalid callback
204
	 */
205
	public function testMultiCallbackInvalidCallback(){
206
		$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], '');
207
	}
208
209
	/**
210
	 * @expectedException \chillerlan\Database\DBException
211
	 * @expectedExceptionMessage sql error:
212
	 */
213
	public function testMultiCallbackSQLError(){
214
		$this->DBDriver->multi_callback(
215
			$this->SQL_RAW_ERROR, [[0]], function(){
216
			return [];
217
		});
218
	}
219
220 View Code Duplication
	public function testMultiCallback(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
222
		$this->assertTrue($this->DBDriver->multi_callback(
223
			$this->SQL_PREPARED_INSERT, range(0, 9), function($k){
224
			return [$k, md5($k)];
225
		}));
226
227
		$this->resultTest();
228
	}
229
230
	public function testCached(){
231
232
		$this->assertTrue($this->DBDriver->multi_callback(
233
			$this->SQL_PREPARED_INSERT, range(0, 9), function($k){
234
			return [$k, md5($k)];
235
		}));
236
237
		$cacheKey = $this->DBDriver->cacheKey($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL);
238
239
		// uncached
240
		$this->assertFalse($this->Cache->has($cacheKey));
241
		$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2);
242
243
		// cached
244
		$this->assertTrue($this->Cache->has($cacheKey));
245
		$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2);
246
247
		sleep(3);
248
249
		$this->Cache->clear();
250
251
		// prepared uncached
252
		$this->assertFalse($this->Cache->has($cacheKey));
253
		$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2);
254
255
		// prepared cached
256
		$this->assertTrue($this->Cache->has($cacheKey));
257
		$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2);
258
259
		sleep(3);
260
		$this->Cache->clear();
261
262
		$this->assertFalse($this->Cache->has($cacheKey));
263
264
		$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE));
0 ignored issues
show
Bug introduced by
It seems like $this->DBDriver->raw($this->SQL_RAW_TRUNCATE) targeting chillerlan\Database\Driv...BDriverInterface::raw() can also be of type object<chillerlan\Database\DBResult>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
265
	}
266
267
}
268