1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\MicroDbal\Test\Raw; |
4
|
|
|
|
5
|
|
|
use Hgraca\MicroDbal\Raw\PdoClient; |
6
|
|
|
use Hgraca\MicroDbal\Test\Stub\Foo; |
7
|
|
|
use Mockery; |
8
|
|
|
use Mockery\MockInterface; |
9
|
|
|
use PDO; |
10
|
|
|
use PDOStatement; |
11
|
|
|
use PHPUnit_Framework_TestCase; |
12
|
|
|
|
13
|
|
|
final class PdoClientUnitTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var MockInterface|PDO */ |
16
|
|
|
private $pdo; |
17
|
|
|
|
18
|
|
|
/** @var PdoClient */ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @before |
23
|
|
|
*/ |
24
|
|
|
protected function setUpClient() |
25
|
|
|
{ |
26
|
|
|
$this->pdo = Mockery::mock(PDO::class); |
27
|
|
|
$this->pdo->shouldReceive('setAttribute'); |
28
|
|
|
$this->client = new PdoClient($this->pdo); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
* |
34
|
|
|
* @small |
35
|
|
|
*/ |
36
|
|
|
public function executeQuery() |
37
|
|
|
{ |
38
|
|
|
$sql = 'some dummy sql'; |
39
|
|
|
$parameterList = [ |
40
|
|
|
'trueVal' => true, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
44
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
45
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
46
|
|
|
->once() |
47
|
|
|
->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
48
|
|
|
->andReturn(true); |
49
|
|
|
|
50
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(true); |
51
|
|
|
$pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn( |
52
|
|
|
$expectedResult = [ |
53
|
|
|
['propA' => 1, 'propB' => 2], |
54
|
|
|
['propA' => 3, 'propB' => 4], |
55
|
|
|
] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
* |
64
|
|
|
* @small |
65
|
|
|
* |
66
|
|
|
* @expectedException \Hgraca\MicroDbal\Raw\Exception\BindingException |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function executeQuery_ShouldThrowExceptionIfCantBind() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$sql = 'some dummy sql'; |
71
|
|
|
$parameterList = [ |
72
|
|
|
'trueVal' => true, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
76
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
77
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
78
|
|
|
->once() |
79
|
|
|
->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
80
|
|
|
->andReturn(false); |
81
|
|
|
|
82
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(false); |
83
|
|
|
$pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123'); |
84
|
|
|
$pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']); |
85
|
|
|
|
86
|
|
|
$this->client->executeQuery($sql, $parameterList); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
* |
92
|
|
|
* @small |
93
|
|
|
* |
94
|
|
|
* @expectedException \Hgraca\MicroDbal\Raw\Exception\ExecutionException |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function executeQuery_ShouldThrowExceptionIfCantExecute() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$sql = 'some dummy sql'; |
99
|
|
|
$parameterList = [ |
100
|
|
|
'trueVal' => true, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
104
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
105
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
106
|
|
|
->once() |
107
|
|
|
->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
108
|
|
|
->andReturn(true); |
109
|
|
|
|
110
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(false); |
111
|
|
|
$pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123'); |
112
|
|
|
$pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']); |
113
|
|
|
|
114
|
|
|
$this->client->executeQuery($sql, $parameterList); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
* |
120
|
|
|
* @small |
121
|
|
|
*/ |
122
|
|
|
public function executeCommand() |
123
|
|
|
{ |
124
|
|
|
$sql = 'some dummy sql'; |
125
|
|
|
$parameterList = [ |
126
|
|
|
'trueVal' => true, |
127
|
|
|
'stringVal' => 'some string', |
128
|
|
|
'intVal' => 1, |
129
|
|
|
'floatVal' => 1.5, |
130
|
|
|
'nullVal' => null, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
134
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
135
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
136
|
|
|
$this->pdo->shouldReceive('commit')->once(); |
137
|
|
|
|
138
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
139
|
|
|
->once() |
140
|
|
|
->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
141
|
|
|
->andReturn(true); |
142
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
143
|
|
|
->once() |
144
|
|
|
->with('stringVal', $parameterList['stringVal'], PDO::PARAM_STR) |
145
|
|
|
->andReturn(true); |
146
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
147
|
|
|
->once() |
148
|
|
|
->with('intVal', $parameterList['intVal'], PDO::PARAM_INT) |
149
|
|
|
->andReturn(true); |
150
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
151
|
|
|
->once() |
152
|
|
|
->with('floatVal', strval($parameterList['floatVal']), PDO::PARAM_STR) |
153
|
|
|
->andReturn(true); |
154
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
155
|
|
|
->once() |
156
|
|
|
->with('nullVal', $parameterList['nullVal'], PDO::PARAM_NULL) |
157
|
|
|
->andReturn(true); |
158
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(true); |
159
|
|
|
|
160
|
|
|
$this->client->executeCommand($sql, $parameterList); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @test |
165
|
|
|
* |
166
|
|
|
* @small |
167
|
|
|
* |
168
|
|
|
* @expectedException \Hgraca\MicroDbal\Raw\Exception\BindingException |
169
|
|
|
*/ |
170
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantBind() |
171
|
|
|
{ |
172
|
|
|
$sql = 'some dummy sql'; |
173
|
|
|
$filterList = [ |
174
|
|
|
'trueVal' => true, |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
178
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
179
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
180
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
181
|
|
|
|
182
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
183
|
|
|
->once() |
184
|
|
|
->with('trueVal', $filterList['trueVal'], PDO::PARAM_BOOL) |
185
|
|
|
->andReturn(false); |
186
|
|
|
|
187
|
|
|
$this->client->executeCommand($sql, $filterList); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @test |
192
|
|
|
* |
193
|
|
|
* @small |
194
|
|
|
* |
195
|
|
|
* @expectedException \Hgraca\MicroDbal\Raw\Exception\ExecutionException |
196
|
|
|
* |
197
|
|
|
* @covers \Hgraca\MicroDbal\Raw\PdoClient::__construct |
198
|
|
|
* @covers \Hgraca\MicroDbal\Raw\PdoClient::executeCommand |
199
|
|
|
* @covers \Hgraca\MicroDbal\Raw\PdoClient::execute |
200
|
|
|
*/ |
201
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantExecute() |
202
|
|
|
{ |
203
|
|
|
$sql = 'some dummy sql'; |
204
|
|
|
$parameterList = [ |
205
|
|
|
'trueVal' => true, |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
209
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
210
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
211
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
212
|
|
|
|
213
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
214
|
|
|
->once() |
215
|
|
|
->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
216
|
|
|
->andReturn(true); |
217
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(false); |
218
|
|
|
$pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123'); |
219
|
|
|
$pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']); |
220
|
|
|
|
221
|
|
|
$this->client->executeCommand($sql, $parameterList); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @test |
226
|
|
|
* |
227
|
|
|
* @small |
228
|
|
|
* |
229
|
|
|
* @expectedException \Hgraca\MicroDbal\Raw\Exception\TypeResolutionException |
230
|
|
|
*/ |
231
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantResolvePdoType() |
232
|
|
|
{ |
233
|
|
|
$sql = 'some dummy sql'; |
234
|
|
|
$parameterList = [ |
235
|
|
|
'trueVal' => new Foo(), |
236
|
|
|
]; |
237
|
|
|
|
238
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
239
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
240
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
241
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
242
|
|
|
$pdoStatementMock->shouldNotReceive('bindValue'); |
|
|
|
|
243
|
|
|
|
244
|
|
|
$this->client->executeCommand($sql, $parameterList); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: