Completed
Push — master ( 47c098...6c95d6 )
by smiley
02:23
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 Psr\SimpleCache\CacheInterface;
18
19
abstract class DriverTestAbstract extends TestAbstract{
20
21
	protected $SQL_RAW_TRUNCATE   = 'DELETE FROM test';
22
	protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test';
23
	protected $SQL_RAW_DROP       = 'DROP TABLE IF EXISTS test';
24
	protected $SQL_RAW_CREATE     = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)';
25
	protected $SQL_RAW_INSERT     = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')';
26
	protected $SQL_RAW_ERROR      = '-';
27
28
	protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)';
29
30
	protected $SQL_INDEX_COL = 'id';
31
	protected $SQL_DATA_COL  = 'hash';
32
33
	protected $SQL_ESCAPED;
34
	protected $SQL_QUOTES;
35
	protected $SQL_DIALECT;
36
37
	protected function tearDown(){
38
		$this->assertTrue($this->DBDriver->disconnect());
39
	}
40
41
	public function testInstance(){
42
		$this->assertInstanceOf(DBDriverInterface::class, $this->DBDriver);
43
		$this->assertInstanceOf(CacheInterface::class, $this->Cache);
44
		$this->assertSame($this->DBDriver, $this->DBDriver->connect());
45
		$this->assertNotNull($this->DBDriver->getDBResource());
46
		$this->assertSame($this->SQL_QUOTES, $this->DBDriver->quotes);
0 ignored issues
show
Bug introduced by
Accessing quotes on the interface chillerlan\Database\Drivers\DBDriverInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
47
		$this->assertSame($this->SQL_DIALECT, $this->DBDriver->dialect);
0 ignored issues
show
Bug introduced by
Accessing dialect on the interface chillerlan\Database\Drivers\DBDriverInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
48
#/*
49
		print_r([
50
			'driver' => get_class($this->DBDriver),
51
			'client' => $this->DBDriver->getClientInfo(),
52
			'server' => $this->DBDriver->getServerInfo(),
53
		]);
54
#*/
55
	}
56
57
	/**
58
	 * @expectedException \chillerlan\Database\DBException
59
	 * @expectedExceptionMessage db error:
60
	 */
61
	public function testConnectDBconnectError(){
62
		$this->DBOptions->host     = '...';
63
		$this->DBOptions->database = '...';
64
		(new $this->driver($this->DBOptions))->connect();
65
	}
66
67
	// coverage
68
	public function testEscape(){
69
		$this->assertSame($this->SQL_ESCAPED, $this->DBDriver->escape("'\\\''"));
70
	}
71
72
	public function testCreateTable(){
73
		$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...
74
		$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...
75
	}
76
77
	public function testTruncate(){
78
		$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...
79
		$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...
80
	}
81
82
	/**
83
	 * @expectedException \chillerlan\Database\DBException
84
	 * @expectedExceptionMessage sql error:
85
	 */
86
	public function testRawSQLError(){
87
		$this->DBDriver->raw($this->SQL_RAW_ERROR);
88
	}
89
90
	protected function resultTest(){
91
		$DBResult         = $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL);
92
		$DBResultPrepared = $this->DBDriver->prepared($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); // coverage
93
94
		$this->assertEquals($DBResult, $DBResultPrepared);
95
96
		$this->assertCount(10, $DBResult);
97
98
		$DBResult->__each(
99
			function($row, $i){
100
				/** @var \chillerlan\Database\DBResultRow $row */
101
				$this->assertEquals($row->{$this->SQL_INDEX_COL}, $i);
102
				$this->assertSame(md5($row->{$this->SQL_INDEX_COL}), $row->{$this->SQL_DATA_COL}());
103
104
				$row->__each(
105
					function($v, $j) use ($row){
106
						$this->assertEquals($row->{$j}, $v);
107
						$this->assertEquals($row[$j], $v);
108
					}
109
				);
110
			}
111
		);
112
113
		$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...
114
	}
115
116
	public function testRaw(){
117
118
		// don't try this at home!
119
		foreach(range(0, 9) as $k){
120
			$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...
121
		}
122
123
		$this->resultTest();
124
	}
125
126
	/**
127
	 * @expectedException \chillerlan\Database\DBException
128
	 * @expectedExceptionMessage sql error:
129
	 */
130
	public function testPreparedSQLError(){
131
		$this->DBDriver->prepared($this->SQL_RAW_ERROR);
132
	}
133
134 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...
135
136
		foreach(range(0, 9) as $k){
137
			$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...
138
		}
139
140
		$this->resultTest();
141
	}
142
143
	/**
144
	 * @expectedException \chillerlan\Database\DBException
145
	 * @expectedExceptionMessage invalid data
146
	 */
147
	public function testMultiInvalidData(){
148
		$this->DBDriver->multi($this->SQL_RAW_ERROR, []);
149
	}
150
151
	/**
152
	 * @expectedException \chillerlan\Database\DBException
153
	 * @expectedExceptionMessage sql error:
154
	 */
155
	public function testMultiSQLError(){
156
		$this->DBDriver->multi($this->SQL_RAW_ERROR, [[0]]);
157
	}
158
159 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...
160
161
		$this->assertTrue(
162
			$this->DBDriver->multi(
163
				$this->SQL_PREPARED_INSERT, array_map(
164
				function($k){
165
					return [$k, md5($k)];
166
				}, range(0, 9)
167
			)
168
			)
169
		);
170
171
		$this->resultTest();
172
	}
173
174
	/**
175
	 * @expectedException \chillerlan\Database\DBException
176
	 * @expectedExceptionMessage invalid data
177
	 */
178
	public function testMultiCallbackInvalidData(){
179
		$this->DBDriver->multi_callback(
180
			$this->SQL_RAW_ERROR, [], function(){
181
		});
182
	}
183
184
	/**
185
	 * @expectedException \chillerlan\Database\DBException
186
	 * @expectedExceptionMessage invalid callback
187
	 */
188
	public function testMultiCallbackInvalidCallback(){
189
		$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], '');
190
	}
191
192
	/**
193
	 * @expectedException \chillerlan\Database\DBException
194
	 * @expectedExceptionMessage sql error:
195
	 */
196
	public function testMultiCallbackSQLError(){
197
		$this->DBDriver->multi_callback(
198
			$this->SQL_RAW_ERROR, [[0]], function(){
199
			return [];
200
		});
201
	}
202
203 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...
204
205
		$this->assertTrue($this->DBDriver->multi_callback(
206
			$this->SQL_PREPARED_INSERT, range(0, 9), function($k){
207
			return [$k, md5($k)];
208
		}));
209
210
		$this->resultTest();
211
	}
212
213
	public function testCached(){
214
215
		$this->assertTrue($this->DBDriver->multi_callback(
216
			$this->SQL_PREPARED_INSERT, range(0, 9), function($k){
217
			return [$k, md5($k)];
218
		}));
219
220
		$cacheKey = $this->DBDriver->cacheKey($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL);
221
222
		// uncached
223
		$this->assertFalse($this->Cache->has($cacheKey));
224
		$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2);
225
226
		// cached
227
		$this->assertTrue($this->Cache->has($cacheKey));
228
		$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2);
229
230
		sleep(3);
231
232
		$this->Cache->clear();
233
234
		// prepared uncached
235
		$this->assertFalse($this->Cache->has($cacheKey));
236
		$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2);
237
238
		// prepared cached
239
		$this->assertTrue($this->Cache->has($cacheKey));
240
		$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2);
241
242
		sleep(3);
243
		$this->Cache->clear();
244
245
		$this->assertFalse($this->Cache->has($cacheKey));
246
247
		$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...
248
	}
249
250
}
251