1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\MicroOrm\Test\Repository\Client; |
4
|
|
|
|
5
|
|
|
use Hgraca\MicroOrm\DataSource\Pdo\PdoClient; |
6
|
|
|
use Hgraca\MicroOrm\Test\Stub\Foo; |
7
|
|
|
use Mockery; |
8
|
|
|
use Mockery\MockInterface; |
9
|
|
|
use PDO; |
10
|
|
|
use PDOStatement; |
11
|
|
|
use PHPUnit_Framework_TestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* TODO refactor PdoClientUnitTest |
15
|
|
|
*/ |
16
|
|
|
final class PdoClientUnitTest extends PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var MockInterface|PDO */ |
19
|
|
|
private $pdo; |
20
|
|
|
|
21
|
|
|
/** @var PdoClient */ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @before |
26
|
|
|
*/ |
27
|
|
|
protected function setUpClient() |
28
|
|
|
{ |
29
|
|
|
$this->pdo = Mockery::mock(PDO::class); |
30
|
|
|
$this->pdo->shouldReceive('setAttribute'); |
31
|
|
|
$this->client = new PdoClient($this->pdo); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* |
37
|
|
|
* @small |
38
|
|
|
*/ |
39
|
|
|
public function executeQuery() |
40
|
|
|
{ |
41
|
|
|
$sql = 'some dummy sql'; |
42
|
|
|
$parameterList = [ |
43
|
|
|
'trueVal' => true, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
47
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(true); |
50
|
|
|
$pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn( |
51
|
|
|
$expectedResult = [ |
52
|
|
|
['propA' => 1, 'propB' => 2], |
53
|
|
|
['propA' => 3, 'propB' => 4], |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList)); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
* |
63
|
|
|
* @small |
64
|
|
|
* |
65
|
|
|
* @expectedException \Hgraca\MicroOrm\DataSource\Exception\ExecutionException |
66
|
|
|
*/ |
67
|
|
|
public function executeQuery_ShouldThrowException() |
68
|
|
|
{ |
69
|
|
|
$sql = 'some dummy sql'; |
70
|
|
|
$parameterList = [ |
71
|
|
|
'trueVal' => true, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
75
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(false); |
78
|
|
|
$pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123'); |
79
|
|
|
$pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']); |
80
|
|
|
|
81
|
|
|
$this->client->executeQuery($sql, $parameterList); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
* |
87
|
|
|
* @small |
88
|
|
|
*/ |
89
|
|
|
public function executeCommand() |
90
|
|
|
{ |
91
|
|
|
$sql = 'some dummy sql'; |
92
|
|
|
$parameterList = [ |
93
|
|
|
'trueVal' => true, |
94
|
|
|
'stringVal' => 'some string', |
95
|
|
|
'intVal' => 1, |
96
|
|
|
'floatVal' => 1.5, |
97
|
|
|
'nullVal' => null, |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
101
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
102
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
103
|
|
|
$this->pdo->shouldReceive('commit')->once(); |
104
|
|
|
|
105
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
106
|
|
|
->once() |
107
|
|
|
->with(':trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
108
|
|
|
->andReturn(true); |
109
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
110
|
|
|
->once() |
111
|
|
|
->with(':stringVal', $parameterList['stringVal'], PDO::PARAM_STR) |
112
|
|
|
->andReturn(true); |
113
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
114
|
|
|
->once() |
115
|
|
|
->with(':intVal', $parameterList['intVal'], PDO::PARAM_INT) |
116
|
|
|
->andReturn(true); |
117
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
118
|
|
|
->once() |
119
|
|
|
->with(':floatVal', strval($parameterList['floatVal']), PDO::PARAM_STR) |
120
|
|
|
->andReturn(true); |
121
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
122
|
|
|
->once() |
123
|
|
|
->with(':nullVal', $parameterList['nullVal'], PDO::PARAM_NULL) |
124
|
|
|
->andReturn(true); |
125
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(true); |
126
|
|
|
|
127
|
|
|
$this->client->executeCommand($sql, $parameterList); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
* |
133
|
|
|
* @small |
134
|
|
|
* |
135
|
|
|
* @expectedException \Hgraca\MicroOrm\DataSource\Exception\BindingException |
136
|
|
|
*/ |
137
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantBind() |
138
|
|
|
{ |
139
|
|
|
$sql = 'some dummy sql'; |
140
|
|
|
$filterList = [ |
141
|
|
|
'trueVal' => true, |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
145
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
146
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
147
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
148
|
|
|
|
149
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
150
|
|
|
->once() |
151
|
|
|
->with(':trueVal', $filterList['trueVal'], PDO::PARAM_BOOL) |
152
|
|
|
->andReturn(false); |
153
|
|
|
|
154
|
|
|
$this->client->executeCommand($sql, $filterList); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @test |
159
|
|
|
* |
160
|
|
|
* @small |
161
|
|
|
* |
162
|
|
|
* @expectedException \Hgraca\MicroOrm\DataSource\Exception\ExecutionException |
163
|
|
|
* |
164
|
|
|
* @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct |
165
|
|
|
* @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand |
166
|
|
|
* @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute |
167
|
|
|
*/ |
168
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantExecute() |
169
|
|
|
{ |
170
|
|
|
$sql = 'some dummy sql'; |
171
|
|
|
$parameterList = [ |
172
|
|
|
'trueVal' => true, |
173
|
|
|
]; |
174
|
|
|
|
175
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
176
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
177
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
178
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
179
|
|
|
|
180
|
|
|
$pdoStatementMock->shouldReceive('bindValue') |
181
|
|
|
->once() |
182
|
|
|
->with(':trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL) |
183
|
|
|
->andReturn(true); |
184
|
|
|
$pdoStatementMock->shouldReceive('execute')->once()->andReturn(false); |
185
|
|
|
$pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123'); |
186
|
|
|
$pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']); |
187
|
|
|
|
188
|
|
|
$this->client->executeCommand($sql, $parameterList); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @test |
193
|
|
|
* |
194
|
|
|
* @small |
195
|
|
|
* |
196
|
|
|
* @expectedException \Hgraca\MicroOrm\DataSource\Exception\TypeResolutionException |
197
|
|
|
*/ |
198
|
|
|
public function executeCommand_ShouldThrowExceptionIfCantResolvePdoType() |
199
|
|
|
{ |
200
|
|
|
$sql = 'some dummy sql'; |
201
|
|
|
$parameterList = [ |
202
|
|
|
'trueVal' => new Foo(), |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
$pdoStatementMock = Mockery::mock(PDOStatement::class); |
206
|
|
|
$this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock); |
|
|
|
|
207
|
|
|
$this->pdo->shouldReceive('beginTransaction')->once(); |
208
|
|
|
$this->pdo->shouldReceive('rollBack')->once(); |
209
|
|
|
$pdoStatementMock->shouldNotReceive('bindValue'); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->client->executeCommand($sql, $parameterList); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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: