1 | <?php |
||||
2 | |||||
3 | namespace Mautic\CoreBundle\Test\Doctrine; |
||||
4 | |||||
5 | use Doctrine\DBAL\Connection; |
||||
6 | use Doctrine\DBAL\Driver\ResultStatement; |
||||
7 | use Doctrine\DBAL\Query\Expression\ExpressionBuilder; |
||||
8 | use Doctrine\DBAL\Query\QueryBuilder; |
||||
9 | use Doctrine\ORM\EntityManager; |
||||
10 | use Mautic\LeadBundle\Entity\Lead; |
||||
11 | |||||
12 | class DBALMocker |
||||
13 | { |
||||
14 | protected $testCase; |
||||
15 | protected $mockEm; |
||||
16 | protected $mockConnection; |
||||
17 | protected $mockQueryBuilder; |
||||
18 | protected $queryResponse; |
||||
19 | protected $connectionUpdated; |
||||
20 | protected $connectionInserted; |
||||
21 | |||||
22 | protected $queryParts = [ |
||||
23 | 'select' => [], |
||||
24 | 'from' => [], |
||||
25 | 'where' => [], |
||||
26 | 'parameters' => [], |
||||
27 | ]; |
||||
28 | |||||
29 | public function __construct(\PHPUnit\Framework\TestCase $testCase) |
||||
30 | { |
||||
31 | $this->testCase = $testCase; |
||||
32 | } |
||||
33 | |||||
34 | public function setQueryResponse($queryResponse) |
||||
35 | { |
||||
36 | $this->queryResponse = $queryResponse; |
||||
37 | } |
||||
38 | |||||
39 | public function getQueryParts() |
||||
40 | { |
||||
41 | return $this->queryParts; |
||||
42 | } |
||||
43 | |||||
44 | public function getQueryPart($part) |
||||
45 | { |
||||
46 | if (array_key_exists($part, $this->queryParts)) { |
||||
47 | return $this->queryParts[$part]; |
||||
48 | } |
||||
49 | |||||
50 | throw new \UnexpectedValueException(sprintf('The requested query part (%s) does not exist. It must be one of %s.', $part, implode(', ', array_keys($this->queryParts)))); |
||||
51 | } |
||||
52 | |||||
53 | public function resetQueryParts() |
||||
54 | { |
||||
55 | $this->queryParts = [ |
||||
56 | 'select' => [], |
||||
57 | 'from' => [], |
||||
58 | 'where' => [], |
||||
59 | 'parameters' => [], |
||||
60 | ]; |
||||
61 | } |
||||
62 | |||||
63 | public function resetUpdated() |
||||
64 | { |
||||
65 | $this->connectionUpdated = []; |
||||
66 | } |
||||
67 | |||||
68 | public function resetInserted() |
||||
69 | { |
||||
70 | $this->connectionInserted = []; |
||||
71 | } |
||||
72 | |||||
73 | public function reset() |
||||
74 | { |
||||
75 | $this->resetQueryParts(); |
||||
76 | $this->resetUpdated(); |
||||
77 | $this->resetInserted(); |
||||
78 | } |
||||
79 | |||||
80 | public function getMockEm() |
||||
81 | { |
||||
82 | if (null === $this->mockEm) { |
||||
83 | $mock = $this->testCase->getMockBuilder(EntityManager::class) |
||||
0 ignored issues
–
show
|
|||||
84 | ->disableOriginalConstructor() |
||||
85 | ->setMethods( |
||||
86 | [ |
||||
87 | 'getConnection', |
||||
88 | 'getReference', |
||||
89 | ] |
||||
90 | ) |
||||
91 | ->getMock(); |
||||
92 | |||||
93 | $mock->expects($this->testCase->any()) |
||||
94 | ->method('getConnection') |
||||
95 | ->willReturn($this->getMockConnection()); |
||||
96 | |||||
97 | $mock->expects($this->testCase->any()) |
||||
98 | ->method('getReference') |
||||
99 | ->willReturnCallback(function () { |
||||
100 | switch (func_get_arg(0)) { |
||||
101 | case 'MauticLeadBundle:Lead': |
||||
102 | $entity = new Lead(); |
||||
103 | break; |
||||
104 | } |
||||
105 | |||||
106 | $entity->setId(func_get_arg(1)); |
||||
107 | |||||
108 | return $entity; |
||||
109 | }); |
||||
110 | |||||
111 | $this->mockEm = $mock; |
||||
112 | } |
||||
113 | |||||
114 | return $this->mockEm; |
||||
115 | } |
||||
116 | |||||
117 | public function getMockConnection() |
||||
118 | { |
||||
119 | if (null === $this->mockConnection) { |
||||
120 | $mock = $this->testCase->getMockBuilder(Connection::class) |
||||
0 ignored issues
–
show
The function
PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
121 | ->disableOriginalConstructor() |
||||
122 | ->setMethods([ |
||||
123 | 'createQueryBuilder', |
||||
124 | 'quote', |
||||
125 | 'update', |
||||
126 | 'insert', |
||||
127 | ]) |
||||
128 | ->getMock(); |
||||
129 | |||||
130 | $mock->expects($this->testCase->any()) |
||||
131 | ->method('createQueryBuilder') |
||||
132 | ->willReturn($this->getMockQueryBuilder()); |
||||
133 | |||||
134 | $mock->expects($this->testCase->any()) |
||||
135 | ->method('quote') |
||||
136 | ->willReturnArgument(0); |
||||
137 | |||||
138 | $mock->expects($this->testCase->any()) |
||||
139 | ->method('update') |
||||
140 | ->willReturnCallback(function () { |
||||
141 | $this->connectionUpdated[] = func_get_args(); |
||||
142 | }); |
||||
143 | |||||
144 | $mock->expects($this->testCase->any()) |
||||
145 | ->method('insert') |
||||
146 | ->willReturnCallback(function () { |
||||
147 | $this->connectionInserted[] = func_get_args(); |
||||
148 | }); |
||||
149 | |||||
150 | $this->mockConnection = $mock; |
||||
151 | } |
||||
152 | |||||
153 | return $this->mockConnection; |
||||
154 | } |
||||
155 | |||||
156 | public function getMockQueryBuilder() |
||||
157 | { |
||||
158 | if (null === $this->mockQueryBuilder) { |
||||
159 | $mock = $this->testCase->getMockBuilder(QueryBuilder::class) |
||||
0 ignored issues
–
show
The function
PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
160 | ->disableOriginalConstructor() |
||||
161 | ->setMethods( |
||||
162 | [ |
||||
163 | 'select', |
||||
164 | 'from', |
||||
165 | 'expr', |
||||
166 | 'where', |
||||
167 | 'andWhere', |
||||
168 | 'setParameter', |
||||
169 | 'execute', |
||||
170 | ] |
||||
171 | ) |
||||
172 | ->getMock(); |
||||
173 | |||||
174 | $mock->expects($this->testCase->any()) |
||||
175 | ->method('select') |
||||
176 | ->willReturnCallback( |
||||
177 | function () use ($mock) { |
||||
178 | $this->queryParts['select'][] = func_get_args(); |
||||
179 | |||||
180 | return $mock; |
||||
181 | } |
||||
182 | ); |
||||
183 | |||||
184 | $mock->expects($this->testCase->any()) |
||||
185 | ->method('from') |
||||
186 | ->willReturnCallback( |
||||
187 | function () use ($mock) { |
||||
188 | $this->queryParts['from'][] = func_get_args(); |
||||
189 | |||||
190 | return $mock; |
||||
191 | } |
||||
192 | ); |
||||
193 | |||||
194 | $mock->expects($this->testCase->any()) |
||||
195 | ->method('expr') |
||||
196 | ->willReturnCallback( |
||||
197 | function () { |
||||
198 | return new ExpressionBuilder($this->getMockConnection()); |
||||
199 | } |
||||
200 | ); |
||||
201 | |||||
202 | $mock->expects($this->testCase->any()) |
||||
203 | ->method('where') |
||||
204 | ->willReturnCallback( |
||||
205 | function () use ($mock) { |
||||
206 | $this->queryParts['where'][] = func_get_args(); |
||||
207 | |||||
208 | return $mock; |
||||
209 | } |
||||
210 | ); |
||||
211 | |||||
212 | $mock->expects($this->testCase->any()) |
||||
213 | ->method('andWhere') |
||||
214 | ->willReturnCallback( |
||||
215 | function () use ($mock) { |
||||
216 | $this->queryParts['where'][] = func_get_args(); |
||||
217 | |||||
218 | return $mock; |
||||
219 | } |
||||
220 | ); |
||||
221 | |||||
222 | $mock->expects($this->testCase->any()) |
||||
223 | ->method('setParameter') |
||||
224 | ->willReturnCallback( |
||||
225 | function () use ($mock) { |
||||
226 | $this->queryParts['parameters'][] = func_get_args(); |
||||
227 | |||||
228 | return $mock; |
||||
229 | } |
||||
230 | ); |
||||
231 | |||||
232 | $mock->expects($this->testCase->any()) |
||||
233 | ->method('execute') |
||||
234 | ->willReturnCallback([$this, 'getMockResultStatement']); |
||||
235 | |||||
236 | $this->mockQueryBuilder = $mock; |
||||
237 | } |
||||
238 | |||||
239 | return $this->mockQueryBuilder; |
||||
240 | } |
||||
241 | |||||
242 | public function getMockResultStatement() |
||||
243 | { |
||||
244 | $mock = $this->testCase->getMockBuilder(ResultStatement::class) |
||||
0 ignored issues
–
show
The function
PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
245 | ->disableOriginalConstructor() |
||||
246 | ->setMethods([ |
||||
247 | 'closeCursor', |
||||
248 | 'columnCount', |
||||
249 | 'setFetchMode', |
||||
250 | 'fetch', |
||||
251 | 'fetchAll', |
||||
252 | 'fetchColumn', |
||||
253 | ]) |
||||
254 | ->getMock(); |
||||
255 | |||||
256 | $mock->method('closeCursor') |
||||
257 | ->willReturn(true); |
||||
258 | |||||
259 | $mock->method('setFetchMode') |
||||
260 | ->willReturn(true); |
||||
261 | |||||
262 | $mock->method('columnCount') |
||||
263 | ->willReturnCallback(function () { |
||||
264 | if (isset($this->queryResponse[0]) && is_array($this->queryResponse[0])) { |
||||
265 | return count($this->queryResponse[0]); |
||||
266 | } else { |
||||
267 | return count($this->queryResponse); |
||||
268 | } |
||||
269 | }); |
||||
270 | |||||
271 | $mock->expects($this->testCase->any()) |
||||
272 | ->method('fetchAll') |
||||
273 | ->willReturn($this->queryResponse); |
||||
274 | |||||
275 | $mock->expects($this->testCase->any()) |
||||
276 | ->method('fetch') |
||||
277 | ->willReturn($this->queryResponse); |
||||
278 | |||||
279 | return $mock; |
||||
280 | } |
||||
281 | } |
||||
282 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.