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