1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Prophecy. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* Marcello Duarte <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prophecy\Call; |
13
|
|
|
|
14
|
|
|
use Prophecy\Exception\Prophecy\MethodProphecyException; |
15
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
16
|
|
|
use Prophecy\Argument\ArgumentsWildcard; |
17
|
|
|
use Prophecy\Util\StringUtil; |
18
|
|
|
use Prophecy\Exception\Call\UnexpectedCallException; |
19
|
|
|
use SplObjectStorage; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Calls receiver & manager. |
23
|
|
|
* |
24
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CallCenter |
27
|
|
|
{ |
28
|
|
|
private $util; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Call[] |
32
|
|
|
*/ |
33
|
|
|
private $recordedCalls = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var SplObjectStorage |
37
|
|
|
*/ |
38
|
|
|
private $unexpectedCalls; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initializes call center. |
42
|
|
|
* |
43
|
|
|
* @param StringUtil $util |
44
|
|
|
*/ |
45
|
|
|
public function __construct(StringUtil $util = null) |
46
|
|
|
{ |
47
|
|
|
$this->util = $util ?: new StringUtil; |
48
|
|
|
$this->unexpectedCalls = new SplObjectStorage(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Makes and records specific method call for object prophecy. |
53
|
|
|
* |
54
|
|
|
* @param ObjectProphecy $prophecy |
55
|
|
|
* @param string $methodName |
56
|
|
|
* @param array $arguments |
57
|
|
|
* |
58
|
|
|
* @return mixed Returns null if no promise for prophecy found or promise return value. |
59
|
|
|
* |
60
|
|
|
* @throws \Prophecy\Exception\Call\UnexpectedCallException If no appropriate method prophecy found |
61
|
|
|
*/ |
62
|
|
|
public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments) |
63
|
|
|
{ |
64
|
|
|
// For efficiency exclude 'args' from the generated backtrace |
65
|
|
|
if (PHP_VERSION_ID >= 50400) { |
66
|
|
|
// Limit backtrace to last 3 calls as we don't use the rest |
67
|
|
|
// Limit argument was introduced in PHP 5.4.0 |
68
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
69
|
|
|
} elseif (defined('DEBUG_BACKTRACE_IGNORE_ARGS')) { |
70
|
|
|
// DEBUG_BACKTRACE_IGNORE_ARGS was introduced in PHP 5.3.6 |
71
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
72
|
|
|
} else { |
73
|
|
|
$backtrace = debug_backtrace(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$file = $line = null; |
77
|
|
|
if (isset($backtrace[2]) && isset($backtrace[2]['file'])) { |
78
|
|
|
$file = $backtrace[2]['file']; |
79
|
|
|
$line = $backtrace[2]['line']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// If no method prophecies defined, then it's a dummy, so we'll just return null |
83
|
|
|
if ('__destruct' === $methodName || 0 == count($prophecy->getMethodProphecies())) { |
84
|
|
|
$this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// There are method prophecies, so it's a fake/stub. Searching prophecy for this call |
90
|
|
|
$matches = $this->findMethodProphecies($prophecy, $methodName, $arguments); |
91
|
|
|
|
92
|
|
|
// If fake/stub doesn't have method prophecy for this call - throw exception |
93
|
|
|
if (!count($matches)) { |
94
|
|
|
$this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy); |
95
|
|
|
$this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Sort matches by their score value |
101
|
|
|
@usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; }); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$score = $matches[0][0]; |
104
|
|
|
// If Highest rated method prophecy has a promise - execute it or return null instead |
105
|
|
|
$methodProphecy = $matches[0][1]; |
106
|
|
|
$returnValue = null; |
107
|
|
|
$exception = null; |
108
|
|
|
if ($promise = $methodProphecy->getPromise()) { |
109
|
|
|
try { |
110
|
|
|
$returnValue = $promise->execute($arguments, $prophecy, $methodProphecy); |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
$exception = $e; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($methodProphecy->hasReturnVoid() && $returnValue !== null) { |
117
|
|
|
throw new MethodProphecyException( |
118
|
|
|
"The method \"$methodName\" has a void return type, but the promise returned a value", |
119
|
|
|
$methodProphecy |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->recordedCalls[] = $call = new Call( |
124
|
|
|
$methodName, $arguments, $returnValue, $exception, $file, $line |
125
|
|
|
); |
126
|
|
|
$call->addScore($methodProphecy->getArgumentsWildcard(), $score); |
127
|
|
|
|
128
|
|
|
if (null !== $exception) { |
129
|
|
|
throw $exception; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $returnValue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Searches for calls by method name & arguments wildcard. |
137
|
|
|
* |
138
|
|
|
* @param string $methodName |
139
|
|
|
* @param ArgumentsWildcard $wildcard |
140
|
|
|
* |
141
|
|
|
* @return Call[] |
142
|
|
|
*/ |
143
|
|
|
public function findCalls($methodName, ArgumentsWildcard $wildcard) |
144
|
|
|
{ |
145
|
|
|
return array_values( |
146
|
|
|
array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) { |
147
|
|
|
return $methodName === $call->getMethodName() |
148
|
|
|
&& 0 < $call->getScore($wildcard) |
149
|
|
|
; |
150
|
|
|
}) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws UnexpectedCallException |
156
|
|
|
*/ |
157
|
|
|
public function checkUnexpectedCalls() |
158
|
|
|
{ |
159
|
|
|
/** @var Call $call */ |
160
|
|
|
foreach ($this->unexpectedCalls as $call) { |
161
|
|
|
$prophecy = $this->unexpectedCalls[$call]; |
162
|
|
|
|
163
|
|
|
// If fake/stub doesn't have method prophecy for this call - throw exception |
164
|
|
|
if (!count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
165
|
|
|
throw $this->createUnexpectedCallException($prophecy, $call->getMethodName(), $call->getArguments()); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function createUnexpectedCallException(ObjectProphecy $prophecy, $methodName, |
171
|
|
|
array $arguments) |
172
|
|
|
{ |
173
|
|
|
$classname = get_class($prophecy->reveal()); |
174
|
|
|
$indentationLength = 8; // looks good |
175
|
|
|
$argstring = implode( |
176
|
|
|
",\n", |
177
|
|
|
$this->indentArguments( |
178
|
|
|
array_map(array($this->util, 'stringify'), $arguments), |
179
|
|
|
$indentationLength |
180
|
|
|
) |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$expected = array(); |
184
|
|
|
|
185
|
|
|
foreach (call_user_func_array('array_merge', $prophecy->getMethodProphecies()) as $methodProphecy) { |
186
|
|
|
$expected[] = sprintf( |
187
|
|
|
" - %s(\n" . |
188
|
|
|
"%s\n" . |
189
|
|
|
" )", |
190
|
|
|
$methodProphecy->getMethodName(), |
191
|
|
|
implode( |
192
|
|
|
",\n", |
193
|
|
|
$this->indentArguments( |
194
|
|
|
array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()), |
195
|
|
|
$indentationLength |
196
|
|
|
) |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return new UnexpectedCallException( |
202
|
|
|
sprintf( |
203
|
|
|
"Unexpected method call on %s:\n". |
204
|
|
|
" - %s(\n". |
205
|
|
|
"%s\n". |
206
|
|
|
" )\n". |
207
|
|
|
"expected calls were:\n". |
208
|
|
|
"%s", |
209
|
|
|
|
210
|
|
|
$classname, $methodName, $argstring, implode("\n", $expected) |
211
|
|
|
), |
212
|
|
|
$prophecy, $methodName, $arguments |
213
|
|
|
|
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function indentArguments(array $arguments, $indentationLength) |
218
|
|
|
{ |
219
|
|
|
return preg_replace_callback( |
220
|
|
|
'/^/m', |
221
|
|
|
function () use ($indentationLength) { |
222
|
|
|
return str_repeat(' ', $indentationLength); |
223
|
|
|
}, |
224
|
|
|
$arguments |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param ObjectProphecy $prophecy |
230
|
|
|
* @param string $methodName |
231
|
|
|
* @param array $arguments |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
|
|
private function findMethodProphecies(ObjectProphecy $prophecy, $methodName, array $arguments) |
236
|
|
|
{ |
237
|
|
|
$matches = array(); |
238
|
|
|
foreach ($prophecy->getMethodProphecies($methodName) as $methodProphecy) { |
239
|
|
|
if (0 < $score = $methodProphecy->getArgumentsWildcard()->scoreArguments($arguments)) { |
240
|
|
|
$matches[] = array($score, $methodProphecy); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $matches; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: