Completed
Push — master ( 58621f...aba2a4 )
by smiley
02:25
created

DriverTestAbstract::testMultiInvalidData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
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\DBResult;
16
use chillerlan\Database\Drivers\DBDriverInterface;
17
use chillerlan\DatabaseTest\TestAbstract;
18
19
abstract class DriverTestAbstract extends TestAbstract{
20
21
	/**
22
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
23
	 */
24
	protected $DBDriver;
25
26
27
	protected $driver;
28
29
	protected $SQL_RAW_TRUNCATE   = 'DELETE FROM test';
30
	protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test';
31
	protected $SQL_RAW_DROP       = 'DROP TABLE IF EXISTS test';
32
	protected $SQL_RAW_CREATE     = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)';
33
	protected $SQL_RAW_INSERT     = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')';
34
	protected $SQL_RAW_ERROR      = '-';
35
36
	protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)';
37
38
	protected $SQL_INDEX_COL = 'id';
39
	protected $SQL_DATA_COL = 'hash';
40
41
	protected function setUp(){
42
		parent::setUp();
43
44
		$this->DBDriver = (new $this->driver($this->dbOptions))->connect();
45
	}
46
47
	protected function tearDown(){
48
		$this->DBDriver->disconnect();
49
	}
50
51
	protected function resultTest(){
52
		$DBResult = $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL);
53
		$DBResultPrepared = $this->DBDriver->prepared($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); // coverage
54
55
		$this->assertEquals($DBResult, $DBResultPrepared);
56
57
		$this->assertCount(10, $DBResult);
58
59 View Code Duplication
		$DBResult->__each(function($row, $i){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
			/** @var \chillerlan\Database\DBResultRow $row */
61
			$this->assertEquals($row->{$this->SQL_INDEX_COL}, $i);
62
			$this->assertSame(md5($row->{$this->SQL_INDEX_COL}), $row->{$this->SQL_DATA_COL}());
63
64
			$row->__each(function($v, $j) use ($row){
65
				$this->assertEquals($row->{$j}, $v);
66
				$this->assertEquals($row[$j], $v);
67
			});
68
		});
69
70
		$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...
71
	}
72
73
	public function testInstance(){
74
		$this->assertInstanceOf(DBDriverInterface::class, $this->DBDriver);
75
		$this->assertNotNull($this->DBDriver->getDBResource());
76
77
		print_r([
78
			'driver' => get_class($this->DBDriver),
79
			'client' => $this->DBDriver->getClientInfo(),
80
			'server' => $this->DBDriver->getServerInfo(),
81
		]);
82
	}
83
84
	public function testCreateTable(){
85
		$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...
86
		$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...
87
	}
88
89
	public function testTruncate(){
90
		$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...
91
		$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...
92
	}
93
94
	/**
95
	 * @expectedException \chillerlan\Database\DBException
96
	 * @expectedExceptionMessage sql error:
97
	 */
98
	public function testRawSQLError(){
99
		$this->DBDriver->raw($this->SQL_RAW_ERROR);
100
	}
101
102
	public function testRaw(){
103
104
		// don't try this at home!
105
		foreach(range(0, 9) as $k){
106
			$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...
107
		}
108
109
		$this->resultTest();
110
	}
111
112
	/**
113
	 * @expectedException \chillerlan\Database\DBException
114
	 * @expectedExceptionMessage sql error:
115
	 */
116
	public function testPreparedSQLError(){
117
		$this->DBDriver->prepared($this->SQL_RAW_ERROR);
118
	}
119
120 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...
121
122
		foreach(range(0, 9) as $k){
123
			$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...
124
		}
125
126
		$this->resultTest();
127
	}
128
129
	/**
130
	 * @expectedException \chillerlan\Database\DBException
131
	 * @expectedExceptionMessage invalid data
132
	 */
133
	public function testMultiInvalidData(){
134
		$this->DBDriver->multi($this->SQL_RAW_ERROR, []);
135
	}
136
137
	/**
138
	 * @expectedException \chillerlan\Database\DBException
139
	 * @expectedExceptionMessage sql error:
140
	 */
141
	public function testMultiSQLError(){
142
		$this->DBDriver->multi($this->SQL_RAW_ERROR, [[0]]);
143
	}
144
145 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...
146
147
		$this->assertTrue($this->DBDriver->multi($this->SQL_PREPARED_INSERT, array_map(function($k){
148
			return [$k, md5($k)];
149
		}, range(0, 9))));
150
151
		$this->resultTest();
152
	}
153
154
	/**
155
	 * @expectedException \chillerlan\Database\DBException
156
	 * @expectedExceptionMessage invalid data
157
	 */
158
	public function testMultiCallbackInvalidData(){
159
		$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [], function(){});
160
	}
161
162
	/**
163
	 * @expectedException \chillerlan\Database\DBException
164
	 * @expectedExceptionMessage invalid callback
165
	 */
166
	public function testMultiCallbackInvalidCallback(){
167
		$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], '');
168
	}
169
170
	/**
171
	 * @expectedException \chillerlan\Database\DBException
172
	 * @expectedExceptionMessage sql error:
173
	 */
174
	public function testMultiCallbackSQLError(){
175
		$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], function(){return [];});
176
	}
177
178 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...
179
180
		$this->assertTrue($this->DBDriver->multi_callback($this->SQL_PREPARED_INSERT, range(0, 9), function($k){
181
			return [$k, md5($k)];
182
		}));
183
184
		$this->resultTest();
185
	}
186
}
187