|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\Extra; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
use PDOStatement; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use QB\Generic\Expr\Expr; |
|
12
|
|
|
|
|
13
|
|
|
class PDOWrapperTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var PDOWrapper System Under Test */ |
|
16
|
|
|
protected PDOWrapper $sut; |
|
17
|
|
|
|
|
18
|
|
|
/** @var PDO|MockObject */ |
|
19
|
|
|
protected PDO|MockObject $pdoMock; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @suppress PhanTypeMismatchArgument |
|
23
|
|
|
*/ |
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var PDO $pdoMock */ |
|
27
|
|
|
$pdoMock = $this->getMockBuilder(PDO::class) |
|
28
|
|
|
->disableOriginalConstructor() |
|
29
|
|
|
->getMock(); |
|
30
|
|
|
|
|
31
|
|
|
$this->pdoMock = $pdoMock; |
|
32
|
|
|
$this->sut = new PDOWrapper($pdoMock); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testPrepareNumeric() |
|
39
|
|
|
{ |
|
40
|
|
|
$sql = '? AND ?'; |
|
41
|
|
|
$values = [17, 23]; |
|
42
|
|
|
|
|
43
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
44
|
|
|
$statementMock |
|
45
|
|
|
->expects($this->exactly(count($values))) |
|
46
|
|
|
->method('bindParam') |
|
47
|
|
|
->withConsecutive([1, 17, PDO::PARAM_INT], [2, 23, PDO::PARAM_INT]); |
|
48
|
|
|
|
|
49
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$query = new Expr($sql, $values); |
|
52
|
|
|
|
|
53
|
|
|
$this->sut->prepare($query); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testPrepareAssoc() |
|
60
|
|
|
{ |
|
61
|
|
|
$sql = ':foo AND :bar'; |
|
62
|
|
|
$values = ['foo' => 'bar', 'bar' => 'baz']; |
|
63
|
|
|
|
|
64
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
65
|
|
|
$statementMock |
|
66
|
|
|
->expects($this->exactly(count($values))) |
|
67
|
|
|
->method('bindParam') |
|
68
|
|
|
->withConsecutive(['foo', 'bar', PDO::PARAM_STR], ['bar', 'baz', PDO::PARAM_STR]); |
|
69
|
|
|
|
|
70
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
71
|
|
|
|
|
72
|
|
|
$query = new Expr($sql, $values); |
|
73
|
|
|
|
|
74
|
|
|
$this->sut->prepare($query); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testExecute() |
|
81
|
|
|
{ |
|
82
|
|
|
$returnValue = true; |
|
83
|
|
|
|
|
84
|
|
|
$sql = ':foo AND :bar'; |
|
85
|
|
|
$values = ['foo' => 'bar', 'bar' => 'baz']; |
|
86
|
|
|
|
|
87
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
88
|
|
|
$statementMock |
|
89
|
|
|
->expects($this->exactly(count($values))) |
|
90
|
|
|
->method('bindParam') |
|
91
|
|
|
->withConsecutive(['foo', 'bar', PDO::PARAM_STR], ['bar', 'baz', PDO::PARAM_STR]); |
|
92
|
|
|
$statementMock |
|
93
|
|
|
->expects($this->once()) |
|
94
|
|
|
->method('execute') |
|
95
|
|
|
->willReturn($returnValue); |
|
96
|
|
|
|
|
97
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
98
|
|
|
|
|
99
|
|
|
$query = new Expr($sql, $values); |
|
100
|
|
|
|
|
101
|
|
|
$actualValue = $this->sut->execute($query); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame($returnValue, $actualValue); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testFetchAll() |
|
110
|
|
|
{ |
|
111
|
|
|
$returnValue = [['foo', 'bar'], ['bar', 'baz']]; |
|
112
|
|
|
|
|
113
|
|
|
$sql = ':foo AND :bar'; |
|
114
|
|
|
$values = ['foo' => 'bar', 'bar' => 'baz']; |
|
115
|
|
|
|
|
116
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
117
|
|
|
$statementMock |
|
118
|
|
|
->expects($this->exactly(count($values))) |
|
119
|
|
|
->method('bindParam') |
|
120
|
|
|
->withConsecutive(['foo', 'bar', PDO::PARAM_STR], ['bar', 'baz', PDO::PARAM_STR]); |
|
121
|
|
|
$statementMock |
|
122
|
|
|
->expects($this->once()) |
|
123
|
|
|
->method('execute') |
|
124
|
|
|
->willReturn(true); |
|
125
|
|
|
$statementMock |
|
126
|
|
|
->expects($this->once()) |
|
127
|
|
|
->method('fetchAll') |
|
128
|
|
|
->willReturn($returnValue); |
|
129
|
|
|
|
|
130
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
131
|
|
|
|
|
132
|
|
|
$query = new Expr($sql, $values); |
|
133
|
|
|
|
|
134
|
|
|
$actualValue = $this->sut->fetchAll($query); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertSame($returnValue, $actualValue); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testFetchColumn() |
|
143
|
|
|
{ |
|
144
|
|
|
$returnValue = 123; |
|
145
|
|
|
|
|
146
|
|
|
$sql = ':foo AND :bar'; |
|
147
|
|
|
$values = ['foo' => 'bar', 'bar' => 'baz']; |
|
148
|
|
|
|
|
149
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
150
|
|
|
$statementMock |
|
151
|
|
|
->expects($this->exactly(count($values))) |
|
152
|
|
|
->method('bindParam') |
|
153
|
|
|
->withConsecutive(['foo', 'bar', PDO::PARAM_STR], ['bar', 'baz', PDO::PARAM_STR]); |
|
154
|
|
|
$statementMock |
|
155
|
|
|
->expects($this->once()) |
|
156
|
|
|
->method('execute') |
|
157
|
|
|
->willReturn(true); |
|
158
|
|
|
$statementMock |
|
159
|
|
|
->expects($this->once()) |
|
160
|
|
|
->method('fetchColumn') |
|
161
|
|
|
->willReturn($returnValue); |
|
162
|
|
|
|
|
163
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
164
|
|
|
|
|
165
|
|
|
$query = new Expr($sql, $values); |
|
166
|
|
|
|
|
167
|
|
|
$actualValue = $this->sut->fetchColumn($query); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertSame($returnValue, $actualValue); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @suppress PhanTypeMismatchArgumentProbablyReal |
|
174
|
|
|
*/ |
|
175
|
|
|
public function testFetch() |
|
176
|
|
|
{ |
|
177
|
|
|
$returnValue = ['foo', 'bar']; |
|
178
|
|
|
|
|
179
|
|
|
$sql = ':foo AND :bar'; |
|
180
|
|
|
$values = ['foo' => 'bar', 'bar' => 'baz']; |
|
181
|
|
|
|
|
182
|
|
|
$statementMock = $this->getMockBuilder(PDOStatement::class)->getMock(); |
|
183
|
|
|
$statementMock |
|
184
|
|
|
->expects($this->exactly(count($values))) |
|
185
|
|
|
->method('bindParam') |
|
186
|
|
|
->withConsecutive(['foo', 'bar', PDO::PARAM_STR], ['bar', 'baz', PDO::PARAM_STR]); |
|
187
|
|
|
$statementMock |
|
188
|
|
|
->expects($this->once()) |
|
189
|
|
|
->method('execute') |
|
190
|
|
|
->willReturn(true); |
|
191
|
|
|
$statementMock |
|
192
|
|
|
->expects($this->once()) |
|
193
|
|
|
->method('fetch') |
|
194
|
|
|
->willReturn($returnValue); |
|
195
|
|
|
|
|
196
|
|
|
$this->pdoMock->expects($this->once())->method('prepare')->with($sql)->willReturn($statementMock); |
|
197
|
|
|
|
|
198
|
|
|
$query = new Expr($sql, $values); |
|
199
|
|
|
|
|
200
|
|
|
$actualValue = $this->sut->fetch($query); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertSame($returnValue, $actualValue); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.