@@ -25,42 +25,42 @@ |
||
25 | 25 | */ |
26 | 26 | class CallbackPrediction implements PredictionInterface |
27 | 27 | { |
28 | - private $callback; |
|
28 | + private $callback; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Initializes callback prediction. |
|
32 | - * |
|
33 | - * @param callable $callback Custom callback |
|
34 | - * |
|
35 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
36 | - */ |
|
37 | - public function __construct($callback) |
|
38 | - { |
|
39 | - if (!is_callable($callback)) { |
|
40 | - throw new InvalidArgumentException(sprintf( |
|
41 | - 'Callable expected as an argument to CallbackPrediction, but got %s.', |
|
42 | - gettype($callback) |
|
43 | - )); |
|
44 | - } |
|
30 | + /** |
|
31 | + * Initializes callback prediction. |
|
32 | + * |
|
33 | + * @param callable $callback Custom callback |
|
34 | + * |
|
35 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
36 | + */ |
|
37 | + public function __construct($callback) |
|
38 | + { |
|
39 | + if (!is_callable($callback)) { |
|
40 | + throw new InvalidArgumentException(sprintf( |
|
41 | + 'Callable expected as an argument to CallbackPrediction, but got %s.', |
|
42 | + gettype($callback) |
|
43 | + )); |
|
44 | + } |
|
45 | 45 | |
46 | - $this->callback = $callback; |
|
47 | - } |
|
46 | + $this->callback = $callback; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Executes preset callback. |
|
51 | - * |
|
52 | - * @param Call[] $calls |
|
53 | - * @param ObjectProphecy $object |
|
54 | - * @param MethodProphecy $method |
|
55 | - */ |
|
56 | - public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
57 | - { |
|
58 | - $callback = $this->callback; |
|
49 | + /** |
|
50 | + * Executes preset callback. |
|
51 | + * |
|
52 | + * @param Call[] $calls |
|
53 | + * @param ObjectProphecy $object |
|
54 | + * @param MethodProphecy $method |
|
55 | + */ |
|
56 | + public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
57 | + { |
|
58 | + $callback = $this->callback; |
|
59 | 59 | |
60 | - if ($callback instanceof Closure && method_exists('Closure', 'bind') && (new ReflectionFunction($callback))->getClosureThis() !== null) { |
|
61 | - $callback = Closure::bind($callback, $object); |
|
62 | - } |
|
60 | + if ($callback instanceof Closure && method_exists('Closure', 'bind') && (new ReflectionFunction($callback))->getClosureThis() !== null) { |
|
61 | + $callback = Closure::bind($callback, $object); |
|
62 | + } |
|
63 | 63 | |
64 | - call_user_func($callback, $calls, $object, $method); |
|
65 | - } |
|
64 | + call_user_func($callback, $calls, $object, $method); |
|
65 | + } |
|
66 | 66 | } |
@@ -36,7 +36,7 @@ |
||
36 | 36 | */ |
37 | 37 | public function __construct($callback) |
38 | 38 | { |
39 | - if (!is_callable($callback)) { |
|
39 | + if ( ! is_callable($callback)) { |
|
40 | 40 | throw new InvalidArgumentException(sprintf( |
41 | 41 | 'Callable expected as an argument to CallbackPrediction, but got %s.', |
42 | 42 | gettype($callback) |
@@ -27,81 +27,81 @@ |
||
27 | 27 | */ |
28 | 28 | class CallTimesPrediction implements PredictionInterface |
29 | 29 | { |
30 | - private $times; |
|
31 | - private $util; |
|
30 | + private $times; |
|
31 | + private $util; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Initializes prediction. |
|
35 | - * |
|
36 | - * @param int $times |
|
37 | - * @param StringUtil $util |
|
38 | - */ |
|
39 | - public function __construct($times, StringUtil $util = null) |
|
40 | - { |
|
41 | - $this->times = intval($times); |
|
42 | - $this->util = $util ?: new StringUtil; |
|
43 | - } |
|
33 | + /** |
|
34 | + * Initializes prediction. |
|
35 | + * |
|
36 | + * @param int $times |
|
37 | + * @param StringUtil $util |
|
38 | + */ |
|
39 | + public function __construct($times, StringUtil $util = null) |
|
40 | + { |
|
41 | + $this->times = intval($times); |
|
42 | + $this->util = $util ?: new StringUtil; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Tests that there was exact amount of calls made. |
|
47 | - * |
|
48 | - * @param Call[] $calls |
|
49 | - * @param ObjectProphecy $object |
|
50 | - * @param MethodProphecy $method |
|
51 | - * |
|
52 | - * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException |
|
53 | - */ |
|
54 | - public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
55 | - { |
|
56 | - if ($this->times == count($calls)) { |
|
57 | - return; |
|
58 | - } |
|
45 | + /** |
|
46 | + * Tests that there was exact amount of calls made. |
|
47 | + * |
|
48 | + * @param Call[] $calls |
|
49 | + * @param ObjectProphecy $object |
|
50 | + * @param MethodProphecy $method |
|
51 | + * |
|
52 | + * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException |
|
53 | + */ |
|
54 | + public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
55 | + { |
|
56 | + if ($this->times == count($calls)) { |
|
57 | + return; |
|
58 | + } |
|
59 | 59 | |
60 | - $methodCalls = $object->findProphecyMethodCalls( |
|
61 | - $method->getMethodName(), |
|
62 | - new ArgumentsWildcard(array(new AnyValuesToken)) |
|
63 | - ); |
|
60 | + $methodCalls = $object->findProphecyMethodCalls( |
|
61 | + $method->getMethodName(), |
|
62 | + new ArgumentsWildcard(array(new AnyValuesToken)) |
|
63 | + ); |
|
64 | 64 | |
65 | - if (count($calls)) { |
|
66 | - $message = sprintf( |
|
67 | - "Expected exactly %d calls that match:\n". |
|
68 | - " %s->%s(%s)\n". |
|
69 | - "but %d were made:\n%s", |
|
65 | + if (count($calls)) { |
|
66 | + $message = sprintf( |
|
67 | + "Expected exactly %d calls that match:\n". |
|
68 | + " %s->%s(%s)\n". |
|
69 | + "but %d were made:\n%s", |
|
70 | 70 | |
71 | - $this->times, |
|
72 | - get_class($object->reveal()), |
|
73 | - $method->getMethodName(), |
|
74 | - $method->getArgumentsWildcard(), |
|
75 | - count($calls), |
|
76 | - $this->util->stringifyCalls($calls) |
|
77 | - ); |
|
78 | - } elseif (count($methodCalls)) { |
|
79 | - $message = sprintf( |
|
80 | - "Expected exactly %d calls that match:\n". |
|
81 | - " %s->%s(%s)\n". |
|
82 | - "but none were made.\n". |
|
83 | - "Recorded `%s(...)` calls:\n%s", |
|
71 | + $this->times, |
|
72 | + get_class($object->reveal()), |
|
73 | + $method->getMethodName(), |
|
74 | + $method->getArgumentsWildcard(), |
|
75 | + count($calls), |
|
76 | + $this->util->stringifyCalls($calls) |
|
77 | + ); |
|
78 | + } elseif (count($methodCalls)) { |
|
79 | + $message = sprintf( |
|
80 | + "Expected exactly %d calls that match:\n". |
|
81 | + " %s->%s(%s)\n". |
|
82 | + "but none were made.\n". |
|
83 | + "Recorded `%s(...)` calls:\n%s", |
|
84 | 84 | |
85 | - $this->times, |
|
86 | - get_class($object->reveal()), |
|
87 | - $method->getMethodName(), |
|
88 | - $method->getArgumentsWildcard(), |
|
89 | - $method->getMethodName(), |
|
90 | - $this->util->stringifyCalls($methodCalls) |
|
91 | - ); |
|
92 | - } else { |
|
93 | - $message = sprintf( |
|
94 | - "Expected exactly %d calls that match:\n". |
|
95 | - " %s->%s(%s)\n". |
|
96 | - "but none were made.", |
|
85 | + $this->times, |
|
86 | + get_class($object->reveal()), |
|
87 | + $method->getMethodName(), |
|
88 | + $method->getArgumentsWildcard(), |
|
89 | + $method->getMethodName(), |
|
90 | + $this->util->stringifyCalls($methodCalls) |
|
91 | + ); |
|
92 | + } else { |
|
93 | + $message = sprintf( |
|
94 | + "Expected exactly %d calls that match:\n". |
|
95 | + " %s->%s(%s)\n". |
|
96 | + "but none were made.", |
|
97 | 97 | |
98 | - $this->times, |
|
99 | - get_class($object->reveal()), |
|
100 | - $method->getMethodName(), |
|
101 | - $method->getArgumentsWildcard() |
|
102 | - ); |
|
103 | - } |
|
98 | + $this->times, |
|
99 | + get_class($object->reveal()), |
|
100 | + $method->getMethodName(), |
|
101 | + $method->getArgumentsWildcard() |
|
102 | + ); |
|
103 | + } |
|
104 | 104 | |
105 | - throw new UnexpectedCallsCountException($message, $method, $this->times, $calls); |
|
106 | - } |
|
105 | + throw new UnexpectedCallsCountException($message, $method, $this->times, $calls); |
|
106 | + } |
|
107 | 107 | } |
@@ -26,61 +26,61 @@ |
||
26 | 26 | */ |
27 | 27 | class CallPrediction implements PredictionInterface |
28 | 28 | { |
29 | - private $util; |
|
29 | + private $util; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Initializes prediction. |
|
33 | - * |
|
34 | - * @param StringUtil $util |
|
35 | - */ |
|
36 | - public function __construct(StringUtil $util = null) |
|
37 | - { |
|
38 | - $this->util = $util ?: new StringUtil; |
|
39 | - } |
|
31 | + /** |
|
32 | + * Initializes prediction. |
|
33 | + * |
|
34 | + * @param StringUtil $util |
|
35 | + */ |
|
36 | + public function __construct(StringUtil $util = null) |
|
37 | + { |
|
38 | + $this->util = $util ?: new StringUtil; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Tests that there was at least one call. |
|
43 | - * |
|
44 | - * @param Call[] $calls |
|
45 | - * @param ObjectProphecy $object |
|
46 | - * @param MethodProphecy $method |
|
47 | - * |
|
48 | - * @throws \Prophecy\Exception\Prediction\NoCallsException |
|
49 | - */ |
|
50 | - public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
51 | - { |
|
52 | - if (count($calls)) { |
|
53 | - return; |
|
54 | - } |
|
41 | + /** |
|
42 | + * Tests that there was at least one call. |
|
43 | + * |
|
44 | + * @param Call[] $calls |
|
45 | + * @param ObjectProphecy $object |
|
46 | + * @param MethodProphecy $method |
|
47 | + * |
|
48 | + * @throws \Prophecy\Exception\Prediction\NoCallsException |
|
49 | + */ |
|
50 | + public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) |
|
51 | + { |
|
52 | + if (count($calls)) { |
|
53 | + return; |
|
54 | + } |
|
55 | 55 | |
56 | - $methodCalls = $object->findProphecyMethodCalls( |
|
57 | - $method->getMethodName(), |
|
58 | - new ArgumentsWildcard(array(new AnyValuesToken)) |
|
59 | - ); |
|
56 | + $methodCalls = $object->findProphecyMethodCalls( |
|
57 | + $method->getMethodName(), |
|
58 | + new ArgumentsWildcard(array(new AnyValuesToken)) |
|
59 | + ); |
|
60 | 60 | |
61 | - if (count($methodCalls)) { |
|
62 | - throw new NoCallsException(sprintf( |
|
63 | - "No calls have been made that match:\n". |
|
64 | - " %s->%s(%s)\n". |
|
65 | - "but expected at least one.\n". |
|
66 | - "Recorded `%s(...)` calls:\n%s", |
|
61 | + if (count($methodCalls)) { |
|
62 | + throw new NoCallsException(sprintf( |
|
63 | + "No calls have been made that match:\n". |
|
64 | + " %s->%s(%s)\n". |
|
65 | + "but expected at least one.\n". |
|
66 | + "Recorded `%s(...)` calls:\n%s", |
|
67 | 67 | |
68 | - get_class($object->reveal()), |
|
69 | - $method->getMethodName(), |
|
70 | - $method->getArgumentsWildcard(), |
|
71 | - $method->getMethodName(), |
|
72 | - $this->util->stringifyCalls($methodCalls) |
|
73 | - ), $method); |
|
74 | - } |
|
68 | + get_class($object->reveal()), |
|
69 | + $method->getMethodName(), |
|
70 | + $method->getArgumentsWildcard(), |
|
71 | + $method->getMethodName(), |
|
72 | + $this->util->stringifyCalls($methodCalls) |
|
73 | + ), $method); |
|
74 | + } |
|
75 | 75 | |
76 | - throw new NoCallsException(sprintf( |
|
77 | - "No calls have been made that match:\n". |
|
78 | - " %s->%s(%s)\n". |
|
79 | - "but expected at least one.", |
|
76 | + throw new NoCallsException(sprintf( |
|
77 | + "No calls have been made that match:\n". |
|
78 | + " %s->%s(%s)\n". |
|
79 | + "but expected at least one.", |
|
80 | 80 | |
81 | - get_class($object->reveal()), |
|
82 | - $method->getMethodName(), |
|
83 | - $method->getArgumentsWildcard() |
|
84 | - ), $method); |
|
85 | - } |
|
81 | + get_class($object->reveal()), |
|
82 | + $method->getMethodName(), |
|
83 | + $method->getArgumentsWildcard() |
|
84 | + ), $method); |
|
85 | + } |
|
86 | 86 | } |
@@ -23,15 +23,15 @@ |
||
23 | 23 | */ |
24 | 24 | interface PredictionInterface |
25 | 25 | { |
26 | - /** |
|
27 | - * Tests that double fulfilled prediction. |
|
28 | - * |
|
29 | - * @param Call[] $calls |
|
30 | - * @param ObjectProphecy $object |
|
31 | - * @param MethodProphecy $method |
|
32 | - * |
|
33 | - * @throws object |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function check(array $calls, ObjectProphecy $object, MethodProphecy $method); |
|
26 | + /** |
|
27 | + * Tests that double fulfilled prediction. |
|
28 | + * |
|
29 | + * @param Call[] $calls |
|
30 | + * @param ObjectProphecy $object |
|
31 | + * @param MethodProphecy $method |
|
32 | + * |
|
33 | + * @throws object |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function check(array $calls, ObjectProphecy $object, MethodProphecy $method); |
|
37 | 37 | } |
@@ -25,216 +25,216 @@ |
||
25 | 25 | */ |
26 | 26 | class CallCenter |
27 | 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 | - // Limit backtrace to last 3 calls as we don't use the rest |
|
66 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
|
67 | - |
|
68 | - $file = $line = null; |
|
69 | - if (isset($backtrace[2]) && isset($backtrace[2]['file'])) { |
|
70 | - $file = $backtrace[2]['file']; |
|
71 | - $line = $backtrace[2]['line']; |
|
72 | - } |
|
73 | - |
|
74 | - // If no method prophecies defined, then it's a dummy, so we'll just return null |
|
75 | - if ('__destruct' === strtolower($methodName) || 0 == count($prophecy->getMethodProphecies())) { |
|
76 | - $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
|
77 | - |
|
78 | - return null; |
|
79 | - } |
|
80 | - |
|
81 | - // There are method prophecies, so it's a fake/stub. Searching prophecy for this call |
|
82 | - $matches = $this->findMethodProphecies($prophecy, $methodName, $arguments); |
|
83 | - |
|
84 | - // If fake/stub doesn't have method prophecy for this call - throw exception |
|
85 | - if (!count($matches)) { |
|
86 | - $this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy); |
|
87 | - $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
|
88 | - |
|
89 | - return null; |
|
90 | - } |
|
91 | - |
|
92 | - // Sort matches by their score value |
|
93 | - @usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; }); |
|
94 | - |
|
95 | - $score = $matches[0][0]; |
|
96 | - // If Highest rated method prophecy has a promise - execute it or return null instead |
|
97 | - $methodProphecy = $matches[0][1]; |
|
98 | - $returnValue = null; |
|
99 | - $exception = null; |
|
100 | - if ($promise = $methodProphecy->getPromise()) { |
|
101 | - try { |
|
102 | - $returnValue = $promise->execute($arguments, $prophecy, $methodProphecy); |
|
103 | - } catch (\Exception $e) { |
|
104 | - $exception = $e; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - if ($methodProphecy->hasReturnVoid() && $returnValue !== null) { |
|
109 | - throw new MethodProphecyException( |
|
110 | - "The method \"$methodName\" has a void return type, but the promise returned a value", |
|
111 | - $methodProphecy |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - $this->recordedCalls[] = $call = new Call( |
|
116 | - $methodName, $arguments, $returnValue, $exception, $file, $line |
|
117 | - ); |
|
118 | - $call->addScore($methodProphecy->getArgumentsWildcard(), $score); |
|
119 | - |
|
120 | - if (null !== $exception) { |
|
121 | - throw $exception; |
|
122 | - } |
|
123 | - |
|
124 | - return $returnValue; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Searches for calls by method name & arguments wildcard. |
|
129 | - * |
|
130 | - * @param string $methodName |
|
131 | - * @param ArgumentsWildcard $wildcard |
|
132 | - * |
|
133 | - * @return Call[] |
|
134 | - */ |
|
135 | - public function findCalls($methodName, ArgumentsWildcard $wildcard) |
|
136 | - { |
|
137 | - $methodName = strtolower($methodName); |
|
138 | - |
|
139 | - return array_values( |
|
140 | - array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) { |
|
141 | - return $methodName === strtolower($call->getMethodName()) |
|
142 | - && 0 < $call->getScore($wildcard) |
|
143 | - ; |
|
144 | - }) |
|
145 | - ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @throws UnexpectedCallException |
|
150 | - */ |
|
151 | - public function checkUnexpectedCalls() |
|
152 | - { |
|
153 | - /** @var Call $call */ |
|
154 | - foreach ($this->unexpectedCalls as $call) { |
|
155 | - $prophecy = $this->unexpectedCalls[$call]; |
|
156 | - |
|
157 | - // If fake/stub doesn't have method prophecy for this call - throw exception |
|
158 | - if (!count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
|
159 | - throw $this->createUnexpectedCallException($prophecy, $call->getMethodName(), $call->getArguments()); |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - private function createUnexpectedCallException(ObjectProphecy $prophecy, $methodName, |
|
165 | - array $arguments) |
|
166 | - { |
|
167 | - $classname = get_class($prophecy->reveal()); |
|
168 | - $indentationLength = 8; // looks good |
|
169 | - $argstring = implode( |
|
170 | - ",\n", |
|
171 | - $this->indentArguments( |
|
172 | - array_map(array($this->util, 'stringify'), $arguments), |
|
173 | - $indentationLength |
|
174 | - ) |
|
175 | - ); |
|
176 | - |
|
177 | - $expected = array(); |
|
178 | - |
|
179 | - foreach (array_merge(...array_values($prophecy->getMethodProphecies())) as $methodProphecy) { |
|
180 | - $expected[] = sprintf( |
|
181 | - " - %s(\n" . |
|
182 | - "%s\n" . |
|
183 | - " )", |
|
184 | - $methodProphecy->getMethodName(), |
|
185 | - implode( |
|
186 | - ",\n", |
|
187 | - $this->indentArguments( |
|
188 | - array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()), |
|
189 | - $indentationLength |
|
190 | - ) |
|
191 | - ) |
|
192 | - ); |
|
193 | - } |
|
194 | - |
|
195 | - return new UnexpectedCallException( |
|
196 | - sprintf( |
|
197 | - "Unexpected method call on %s:\n". |
|
198 | - " - %s(\n". |
|
199 | - "%s\n". |
|
200 | - " )\n". |
|
201 | - "expected calls were:\n". |
|
202 | - "%s", |
|
203 | - |
|
204 | - $classname, $methodName, $argstring, implode("\n", $expected) |
|
205 | - ), |
|
206 | - $prophecy, $methodName, $arguments |
|
207 | - |
|
208 | - ); |
|
209 | - } |
|
210 | - |
|
211 | - private function indentArguments(array $arguments, $indentationLength) |
|
212 | - { |
|
213 | - return preg_replace_callback( |
|
214 | - '/^/m', |
|
215 | - function () use ($indentationLength) { |
|
216 | - return str_repeat(' ', $indentationLength); |
|
217 | - }, |
|
218 | - $arguments |
|
219 | - ); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param ObjectProphecy $prophecy |
|
224 | - * @param string $methodName |
|
225 | - * @param array $arguments |
|
226 | - * |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - private function findMethodProphecies(ObjectProphecy $prophecy, $methodName, array $arguments) |
|
230 | - { |
|
231 | - $matches = array(); |
|
232 | - foreach ($prophecy->getMethodProphecies($methodName) as $methodProphecy) { |
|
233 | - if (0 < $score = $methodProphecy->getArgumentsWildcard()->scoreArguments($arguments)) { |
|
234 | - $matches[] = array($score, $methodProphecy); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - return $matches; |
|
239 | - } |
|
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 | + // Limit backtrace to last 3 calls as we don't use the rest |
|
66 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
|
67 | + |
|
68 | + $file = $line = null; |
|
69 | + if (isset($backtrace[2]) && isset($backtrace[2]['file'])) { |
|
70 | + $file = $backtrace[2]['file']; |
|
71 | + $line = $backtrace[2]['line']; |
|
72 | + } |
|
73 | + |
|
74 | + // If no method prophecies defined, then it's a dummy, so we'll just return null |
|
75 | + if ('__destruct' === strtolower($methodName) || 0 == count($prophecy->getMethodProphecies())) { |
|
76 | + $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
|
77 | + |
|
78 | + return null; |
|
79 | + } |
|
80 | + |
|
81 | + // There are method prophecies, so it's a fake/stub. Searching prophecy for this call |
|
82 | + $matches = $this->findMethodProphecies($prophecy, $methodName, $arguments); |
|
83 | + |
|
84 | + // If fake/stub doesn't have method prophecy for this call - throw exception |
|
85 | + if (!count($matches)) { |
|
86 | + $this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy); |
|
87 | + $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
|
88 | + |
|
89 | + return null; |
|
90 | + } |
|
91 | + |
|
92 | + // Sort matches by their score value |
|
93 | + @usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; }); |
|
94 | + |
|
95 | + $score = $matches[0][0]; |
|
96 | + // If Highest rated method prophecy has a promise - execute it or return null instead |
|
97 | + $methodProphecy = $matches[0][1]; |
|
98 | + $returnValue = null; |
|
99 | + $exception = null; |
|
100 | + if ($promise = $methodProphecy->getPromise()) { |
|
101 | + try { |
|
102 | + $returnValue = $promise->execute($arguments, $prophecy, $methodProphecy); |
|
103 | + } catch (\Exception $e) { |
|
104 | + $exception = $e; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + if ($methodProphecy->hasReturnVoid() && $returnValue !== null) { |
|
109 | + throw new MethodProphecyException( |
|
110 | + "The method \"$methodName\" has a void return type, but the promise returned a value", |
|
111 | + $methodProphecy |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + $this->recordedCalls[] = $call = new Call( |
|
116 | + $methodName, $arguments, $returnValue, $exception, $file, $line |
|
117 | + ); |
|
118 | + $call->addScore($methodProphecy->getArgumentsWildcard(), $score); |
|
119 | + |
|
120 | + if (null !== $exception) { |
|
121 | + throw $exception; |
|
122 | + } |
|
123 | + |
|
124 | + return $returnValue; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Searches for calls by method name & arguments wildcard. |
|
129 | + * |
|
130 | + * @param string $methodName |
|
131 | + * @param ArgumentsWildcard $wildcard |
|
132 | + * |
|
133 | + * @return Call[] |
|
134 | + */ |
|
135 | + public function findCalls($methodName, ArgumentsWildcard $wildcard) |
|
136 | + { |
|
137 | + $methodName = strtolower($methodName); |
|
138 | + |
|
139 | + return array_values( |
|
140 | + array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) { |
|
141 | + return $methodName === strtolower($call->getMethodName()) |
|
142 | + && 0 < $call->getScore($wildcard) |
|
143 | + ; |
|
144 | + }) |
|
145 | + ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @throws UnexpectedCallException |
|
150 | + */ |
|
151 | + public function checkUnexpectedCalls() |
|
152 | + { |
|
153 | + /** @var Call $call */ |
|
154 | + foreach ($this->unexpectedCalls as $call) { |
|
155 | + $prophecy = $this->unexpectedCalls[$call]; |
|
156 | + |
|
157 | + // If fake/stub doesn't have method prophecy for this call - throw exception |
|
158 | + if (!count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
|
159 | + throw $this->createUnexpectedCallException($prophecy, $call->getMethodName(), $call->getArguments()); |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + private function createUnexpectedCallException(ObjectProphecy $prophecy, $methodName, |
|
165 | + array $arguments) |
|
166 | + { |
|
167 | + $classname = get_class($prophecy->reveal()); |
|
168 | + $indentationLength = 8; // looks good |
|
169 | + $argstring = implode( |
|
170 | + ",\n", |
|
171 | + $this->indentArguments( |
|
172 | + array_map(array($this->util, 'stringify'), $arguments), |
|
173 | + $indentationLength |
|
174 | + ) |
|
175 | + ); |
|
176 | + |
|
177 | + $expected = array(); |
|
178 | + |
|
179 | + foreach (array_merge(...array_values($prophecy->getMethodProphecies())) as $methodProphecy) { |
|
180 | + $expected[] = sprintf( |
|
181 | + " - %s(\n" . |
|
182 | + "%s\n" . |
|
183 | + " )", |
|
184 | + $methodProphecy->getMethodName(), |
|
185 | + implode( |
|
186 | + ",\n", |
|
187 | + $this->indentArguments( |
|
188 | + array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()), |
|
189 | + $indentationLength |
|
190 | + ) |
|
191 | + ) |
|
192 | + ); |
|
193 | + } |
|
194 | + |
|
195 | + return new UnexpectedCallException( |
|
196 | + sprintf( |
|
197 | + "Unexpected method call on %s:\n". |
|
198 | + " - %s(\n". |
|
199 | + "%s\n". |
|
200 | + " )\n". |
|
201 | + "expected calls were:\n". |
|
202 | + "%s", |
|
203 | + |
|
204 | + $classname, $methodName, $argstring, implode("\n", $expected) |
|
205 | + ), |
|
206 | + $prophecy, $methodName, $arguments |
|
207 | + |
|
208 | + ); |
|
209 | + } |
|
210 | + |
|
211 | + private function indentArguments(array $arguments, $indentationLength) |
|
212 | + { |
|
213 | + return preg_replace_callback( |
|
214 | + '/^/m', |
|
215 | + function () use ($indentationLength) { |
|
216 | + return str_repeat(' ', $indentationLength); |
|
217 | + }, |
|
218 | + $arguments |
|
219 | + ); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param ObjectProphecy $prophecy |
|
224 | + * @param string $methodName |
|
225 | + * @param array $arguments |
|
226 | + * |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + private function findMethodProphecies(ObjectProphecy $prophecy, $methodName, array $arguments) |
|
230 | + { |
|
231 | + $matches = array(); |
|
232 | + foreach ($prophecy->getMethodProphecies($methodName) as $methodProphecy) { |
|
233 | + if (0 < $score = $methodProphecy->getArgumentsWildcard()->scoreArguments($arguments)) { |
|
234 | + $matches[] = array($score, $methodProphecy); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + return $matches; |
|
239 | + } |
|
240 | 240 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $matches = $this->findMethodProphecies($prophecy, $methodName, $arguments); |
83 | 83 | |
84 | 84 | // If fake/stub doesn't have method prophecy for this call - throw exception |
85 | - if (!count($matches)) { |
|
85 | + if ( ! count($matches)) { |
|
86 | 86 | $this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy); |
87 | 87 | $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
88 | 88 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | } |
91 | 91 | |
92 | 92 | // Sort matches by their score value |
93 | - @usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; }); |
|
93 | + @usort($matches, function($match1, $match2) { return $match2[0] - $match1[0]; }); |
|
94 | 94 | |
95 | 95 | $score = $matches[0][0]; |
96 | 96 | // If Highest rated method prophecy has a promise - execute it or return null instead |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | $methodName = strtolower($methodName); |
138 | 138 | |
139 | 139 | return array_values( |
140 | - array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) { |
|
140 | + array_filter($this->recordedCalls, function(Call $call) use ($methodName, $wildcard) { |
|
141 | 141 | return $methodName === strtolower($call->getMethodName()) |
142 | 142 | && 0 < $call->getScore($wildcard) |
143 | 143 | ; |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | $prophecy = $this->unexpectedCalls[$call]; |
156 | 156 | |
157 | 157 | // If fake/stub doesn't have method prophecy for this call - throw exception |
158 | - if (!count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
|
158 | + if ( ! count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
|
159 | 159 | throw $this->createUnexpectedCallException($prophecy, $call->getMethodName(), $call->getArguments()); |
160 | 160 | } |
161 | 161 | } |
@@ -178,8 +178,8 @@ discard block |
||
178 | 178 | |
179 | 179 | foreach (array_merge(...array_values($prophecy->getMethodProphecies())) as $methodProphecy) { |
180 | 180 | $expected[] = sprintf( |
181 | - " - %s(\n" . |
|
182 | - "%s\n" . |
|
181 | + " - %s(\n". |
|
182 | + "%s\n". |
|
183 | 183 | " )", |
184 | 184 | $methodProphecy->getMethodName(), |
185 | 185 | implode( |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | { |
213 | 213 | return preg_replace_callback( |
214 | 214 | '/^/m', |
215 | - function () use ($indentationLength) { |
|
215 | + function() use ($indentationLength) { |
|
216 | 216 | return str_repeat(' ', $indentationLength); |
217 | 217 | }, |
218 | 218 | $arguments |
@@ -21,142 +21,142 @@ |
||
21 | 21 | */ |
22 | 22 | class Call |
23 | 23 | { |
24 | - private $methodName; |
|
25 | - private $arguments; |
|
26 | - private $returnValue; |
|
27 | - private $exception; |
|
28 | - private $file; |
|
29 | - private $line; |
|
30 | - private $scores; |
|
31 | - |
|
32 | - /** |
|
33 | - * Initializes call. |
|
34 | - * |
|
35 | - * @param string $methodName |
|
36 | - * @param array $arguments |
|
37 | - * @param mixed $returnValue |
|
38 | - * @param Exception $exception |
|
39 | - * @param null|string $file |
|
40 | - * @param null|int $line |
|
41 | - */ |
|
42 | - public function __construct($methodName, array $arguments, $returnValue, |
|
43 | - Exception $exception = null, $file, $line) |
|
44 | - { |
|
45 | - $this->methodName = $methodName; |
|
46 | - $this->arguments = $arguments; |
|
47 | - $this->returnValue = $returnValue; |
|
48 | - $this->exception = $exception; |
|
49 | - $this->scores = new \SplObjectStorage(); |
|
50 | - |
|
51 | - if ($file) { |
|
52 | - $this->file = $file; |
|
53 | - $this->line = intval($line); |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Returns called method name. |
|
59 | - * |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function getMethodName() |
|
63 | - { |
|
64 | - return $this->methodName; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Returns called method arguments. |
|
69 | - * |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function getArguments() |
|
73 | - { |
|
74 | - return $this->arguments; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Returns called method return value. |
|
79 | - * |
|
80 | - * @return null|mixed |
|
81 | - */ |
|
82 | - public function getReturnValue() |
|
83 | - { |
|
84 | - return $this->returnValue; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns exception that call thrown. |
|
89 | - * |
|
90 | - * @return null|Exception |
|
91 | - */ |
|
92 | - public function getException() |
|
93 | - { |
|
94 | - return $this->exception; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns callee filename. |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function getFile() |
|
103 | - { |
|
104 | - return $this->file; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns callee line number. |
|
109 | - * |
|
110 | - * @return int |
|
111 | - */ |
|
112 | - public function getLine() |
|
113 | - { |
|
114 | - return $this->line; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Returns short notation for callee place. |
|
119 | - * |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getCallPlace() |
|
123 | - { |
|
124 | - if (null === $this->file) { |
|
125 | - return 'unknown'; |
|
126 | - } |
|
127 | - |
|
128 | - return sprintf('%s:%d', $this->file, $this->line); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Adds the wildcard match score for the provided wildcard. |
|
133 | - * |
|
134 | - * @param ArgumentsWildcard $wildcard |
|
135 | - * @param false|int $score |
|
136 | - * |
|
137 | - * @return $this |
|
138 | - */ |
|
139 | - public function addScore(ArgumentsWildcard $wildcard, $score) |
|
140 | - { |
|
141 | - $this->scores[$wildcard] = $score; |
|
142 | - |
|
143 | - return $this; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Returns wildcard match score for the provided wildcard. The score is |
|
148 | - * calculated if not already done. |
|
149 | - * |
|
150 | - * @param ArgumentsWildcard $wildcard |
|
151 | - * |
|
152 | - * @return false|int False OR integer score (higher - better) |
|
153 | - */ |
|
154 | - public function getScore(ArgumentsWildcard $wildcard) |
|
155 | - { |
|
156 | - if (isset($this->scores[$wildcard])) { |
|
157 | - return $this->scores[$wildcard]; |
|
158 | - } |
|
159 | - |
|
160 | - return $this->scores[$wildcard] = $wildcard->scoreArguments($this->getArguments()); |
|
161 | - } |
|
24 | + private $methodName; |
|
25 | + private $arguments; |
|
26 | + private $returnValue; |
|
27 | + private $exception; |
|
28 | + private $file; |
|
29 | + private $line; |
|
30 | + private $scores; |
|
31 | + |
|
32 | + /** |
|
33 | + * Initializes call. |
|
34 | + * |
|
35 | + * @param string $methodName |
|
36 | + * @param array $arguments |
|
37 | + * @param mixed $returnValue |
|
38 | + * @param Exception $exception |
|
39 | + * @param null|string $file |
|
40 | + * @param null|int $line |
|
41 | + */ |
|
42 | + public function __construct($methodName, array $arguments, $returnValue, |
|
43 | + Exception $exception = null, $file, $line) |
|
44 | + { |
|
45 | + $this->methodName = $methodName; |
|
46 | + $this->arguments = $arguments; |
|
47 | + $this->returnValue = $returnValue; |
|
48 | + $this->exception = $exception; |
|
49 | + $this->scores = new \SplObjectStorage(); |
|
50 | + |
|
51 | + if ($file) { |
|
52 | + $this->file = $file; |
|
53 | + $this->line = intval($line); |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Returns called method name. |
|
59 | + * |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function getMethodName() |
|
63 | + { |
|
64 | + return $this->methodName; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Returns called method arguments. |
|
69 | + * |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function getArguments() |
|
73 | + { |
|
74 | + return $this->arguments; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Returns called method return value. |
|
79 | + * |
|
80 | + * @return null|mixed |
|
81 | + */ |
|
82 | + public function getReturnValue() |
|
83 | + { |
|
84 | + return $this->returnValue; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns exception that call thrown. |
|
89 | + * |
|
90 | + * @return null|Exception |
|
91 | + */ |
|
92 | + public function getException() |
|
93 | + { |
|
94 | + return $this->exception; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns callee filename. |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function getFile() |
|
103 | + { |
|
104 | + return $this->file; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns callee line number. |
|
109 | + * |
|
110 | + * @return int |
|
111 | + */ |
|
112 | + public function getLine() |
|
113 | + { |
|
114 | + return $this->line; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Returns short notation for callee place. |
|
119 | + * |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getCallPlace() |
|
123 | + { |
|
124 | + if (null === $this->file) { |
|
125 | + return 'unknown'; |
|
126 | + } |
|
127 | + |
|
128 | + return sprintf('%s:%d', $this->file, $this->line); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Adds the wildcard match score for the provided wildcard. |
|
133 | + * |
|
134 | + * @param ArgumentsWildcard $wildcard |
|
135 | + * @param false|int $score |
|
136 | + * |
|
137 | + * @return $this |
|
138 | + */ |
|
139 | + public function addScore(ArgumentsWildcard $wildcard, $score) |
|
140 | + { |
|
141 | + $this->scores[$wildcard] = $score; |
|
142 | + |
|
143 | + return $this; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Returns wildcard match score for the provided wildcard. The score is |
|
148 | + * calculated if not already done. |
|
149 | + * |
|
150 | + * @param ArgumentsWildcard $wildcard |
|
151 | + * |
|
152 | + * @return false|int False OR integer score (higher - better) |
|
153 | + */ |
|
154 | + public function getScore(ArgumentsWildcard $wildcard) |
|
155 | + { |
|
156 | + if (isset($this->scores[$wildcard])) { |
|
157 | + return $this->scores[$wildcard]; |
|
158 | + } |
|
159 | + |
|
160 | + return $this->scores[$wildcard] = $wildcard->scoreArguments($this->getArguments()); |
|
161 | + } |
|
162 | 162 | } |
@@ -18,12 +18,12 @@ |
||
18 | 18 | */ |
19 | 19 | interface RevealerInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Unwraps value(s). |
|
23 | - * |
|
24 | - * @param mixed $value |
|
25 | - * |
|
26 | - * @return mixed |
|
27 | - */ |
|
28 | - public function reveal($value); |
|
21 | + /** |
|
22 | + * Unwraps value(s). |
|
23 | + * |
|
24 | + * @param mixed $value |
|
25 | + * |
|
26 | + * @return mixed |
|
27 | + */ |
|
28 | + public function reveal($value); |
|
29 | 29 | } |
@@ -29,545 +29,545 @@ |
||
29 | 29 | */ |
30 | 30 | class MethodProphecy |
31 | 31 | { |
32 | - private $objectProphecy; |
|
33 | - private $methodName; |
|
34 | - private $argumentsWildcard; |
|
35 | - private $promise; |
|
36 | - private $prediction; |
|
37 | - private $checkedPredictions = array(); |
|
38 | - private $bound = false; |
|
39 | - private $voidReturnType = false; |
|
40 | - |
|
41 | - /** |
|
42 | - * Initializes method prophecy. |
|
43 | - * |
|
44 | - * @param ObjectProphecy $objectProphecy |
|
45 | - * @param string $methodName |
|
46 | - * @param null|Argument\ArgumentsWildcard|array $arguments |
|
47 | - * |
|
48 | - * @throws \Prophecy\Exception\Doubler\MethodNotFoundException If method not found |
|
49 | - */ |
|
50 | - public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null) |
|
51 | - { |
|
52 | - $double = $objectProphecy->reveal(); |
|
53 | - if (!method_exists($double, $methodName)) { |
|
54 | - throw new MethodNotFoundException(sprintf( |
|
55 | - 'Method `%s::%s()` is not defined.', get_class($double), $methodName |
|
56 | - ), get_class($double), $methodName, $arguments); |
|
57 | - } |
|
58 | - |
|
59 | - $this->objectProphecy = $objectProphecy; |
|
60 | - $this->methodName = $methodName; |
|
61 | - |
|
62 | - $reflectedMethod = new \ReflectionMethod($double, $methodName); |
|
63 | - if ($reflectedMethod->isFinal()) { |
|
64 | - throw new MethodProphecyException(sprintf( |
|
65 | - "Can not add prophecy for a method `%s::%s()`\n". |
|
66 | - "as it is a final method.", |
|
67 | - get_class($double), |
|
68 | - $methodName |
|
69 | - ), $this); |
|
70 | - } |
|
71 | - |
|
72 | - if (null !== $arguments) { |
|
73 | - $this->withArguments($arguments); |
|
74 | - } |
|
75 | - |
|
76 | - $hasTentativeReturnType = method_exists($reflectedMethod, 'hasTentativeReturnType') |
|
77 | - && $reflectedMethod->hasTentativeReturnType(); |
|
78 | - |
|
79 | - if (true === $reflectedMethod->hasReturnType() || $hasTentativeReturnType) { |
|
80 | - if ($hasTentativeReturnType) { |
|
81 | - $reflectionType = $reflectedMethod->getTentativeReturnType(); |
|
82 | - } |
|
83 | - else { |
|
84 | - $reflectionType = $reflectedMethod->getReturnType(); |
|
85 | - } |
|
86 | - |
|
87 | - if ($reflectionType instanceof ReflectionNamedType) { |
|
88 | - $types = [$reflectionType]; |
|
89 | - } |
|
90 | - elseif ($reflectionType instanceof ReflectionUnionType) { |
|
91 | - $types = $reflectionType->getTypes(); |
|
92 | - } |
|
93 | - |
|
94 | - $types = array_map( |
|
95 | - function(ReflectionType $type) { return $type->getName(); }, |
|
96 | - $types |
|
97 | - ); |
|
98 | - |
|
99 | - usort( |
|
100 | - $types, |
|
101 | - static function(string $type1, string $type2) { |
|
102 | - |
|
103 | - // null is lowest priority |
|
104 | - if ($type2 == 'null') { |
|
105 | - return -1; |
|
106 | - } |
|
107 | - elseif ($type1 == 'null') { |
|
108 | - return 1; |
|
109 | - } |
|
110 | - |
|
111 | - // objects are higher priority than scalars |
|
112 | - $isObject = static function($type) { |
|
113 | - return class_exists($type) || interface_exists($type); |
|
114 | - }; |
|
115 | - |
|
116 | - if($isObject($type1) && !$isObject($type2)) { |
|
117 | - return -1; |
|
118 | - } |
|
119 | - elseif(!$isObject($type1) && $isObject($type2)) |
|
120 | - { |
|
121 | - return 1; |
|
122 | - } |
|
123 | - |
|
124 | - // don't sort both-scalars or both-objects |
|
125 | - return 0; |
|
126 | - } |
|
127 | - ); |
|
128 | - |
|
129 | - $defaultType = $types[0]; |
|
130 | - |
|
131 | - if ('void' === $defaultType) { |
|
132 | - $this->voidReturnType = true; |
|
133 | - } |
|
134 | - |
|
135 | - $this->will(function () use ($defaultType) { |
|
136 | - switch ($defaultType) { |
|
137 | - case 'void': return; |
|
138 | - case 'string': return ''; |
|
139 | - case 'float': return 0.0; |
|
140 | - case 'int': return 0; |
|
141 | - case 'bool': return false; |
|
142 | - case 'array': return array(); |
|
143 | - |
|
144 | - case 'callable': |
|
145 | - case 'Closure': |
|
146 | - return function () {}; |
|
147 | - |
|
148 | - case 'Traversable': |
|
149 | - case 'Generator': |
|
150 | - return (function () { yield; })(); |
|
151 | - |
|
152 | - default: |
|
153 | - $prophet = new Prophet; |
|
154 | - return $prophet->prophesize($defaultType)->reveal(); |
|
155 | - } |
|
156 | - }); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Sets argument wildcard. |
|
162 | - * |
|
163 | - * @param array|Argument\ArgumentsWildcard $arguments |
|
164 | - * |
|
165 | - * @return $this |
|
166 | - * |
|
167 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
168 | - */ |
|
169 | - public function withArguments($arguments) |
|
170 | - { |
|
171 | - if (is_array($arguments)) { |
|
172 | - $arguments = new Argument\ArgumentsWildcard($arguments); |
|
173 | - } |
|
174 | - |
|
175 | - if (!$arguments instanceof Argument\ArgumentsWildcard) { |
|
176 | - throw new InvalidArgumentException(sprintf( |
|
177 | - "Either an array or an instance of ArgumentsWildcard expected as\n". |
|
178 | - 'a `MethodProphecy::withArguments()` argument, but got %s.', |
|
179 | - gettype($arguments) |
|
180 | - )); |
|
181 | - } |
|
182 | - |
|
183 | - $this->argumentsWildcard = $arguments; |
|
184 | - |
|
185 | - return $this; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Sets custom promise to the prophecy. |
|
190 | - * |
|
191 | - * @param callable|Promise\PromiseInterface $promise |
|
192 | - * |
|
193 | - * @return $this |
|
194 | - * |
|
195 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
196 | - */ |
|
197 | - public function will($promise) |
|
198 | - { |
|
199 | - if (is_callable($promise)) { |
|
200 | - $promise = new Promise\CallbackPromise($promise); |
|
201 | - } |
|
202 | - |
|
203 | - if (!$promise instanceof Promise\PromiseInterface) { |
|
204 | - throw new InvalidArgumentException(sprintf( |
|
205 | - 'Expected callable or instance of PromiseInterface, but got %s.', |
|
206 | - gettype($promise) |
|
207 | - )); |
|
208 | - } |
|
209 | - |
|
210 | - $this->bindToObjectProphecy(); |
|
211 | - $this->promise = $promise; |
|
212 | - |
|
213 | - return $this; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Sets return promise to the prophecy. |
|
218 | - * |
|
219 | - * @see \Prophecy\Promise\ReturnPromise |
|
220 | - * |
|
221 | - * @return $this |
|
222 | - */ |
|
223 | - public function willReturn() |
|
224 | - { |
|
225 | - if ($this->voidReturnType) { |
|
226 | - throw new MethodProphecyException( |
|
227 | - "The method \"$this->methodName\" has a void return type, and so cannot return anything", |
|
228 | - $this |
|
229 | - ); |
|
230 | - } |
|
231 | - |
|
232 | - return $this->will(new Promise\ReturnPromise(func_get_args())); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param array $items |
|
237 | - * @param mixed $return |
|
238 | - * |
|
239 | - * @return $this |
|
240 | - * |
|
241 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
242 | - */ |
|
243 | - public function willYield($items, $return = null) |
|
244 | - { |
|
245 | - if ($this->voidReturnType) { |
|
246 | - throw new MethodProphecyException( |
|
247 | - "The method \"$this->methodName\" has a void return type, and so cannot yield anything", |
|
248 | - $this |
|
249 | - ); |
|
250 | - } |
|
251 | - |
|
252 | - if (!is_array($items)) { |
|
253 | - throw new InvalidArgumentException(sprintf( |
|
254 | - 'Expected array, but got %s.', |
|
255 | - gettype($items) |
|
256 | - )); |
|
257 | - } |
|
258 | - |
|
259 | - $generator = function() use ($items, $return) { |
|
260 | - yield from $items; |
|
261 | - |
|
262 | - return $return; |
|
263 | - }; |
|
264 | - |
|
265 | - return $this->will($generator); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Sets return argument promise to the prophecy. |
|
270 | - * |
|
271 | - * @param int $index The zero-indexed number of the argument to return |
|
272 | - * |
|
273 | - * @see \Prophecy\Promise\ReturnArgumentPromise |
|
274 | - * |
|
275 | - * @return $this |
|
276 | - */ |
|
277 | - public function willReturnArgument($index = 0) |
|
278 | - { |
|
279 | - if ($this->voidReturnType) { |
|
280 | - throw new MethodProphecyException("The method \"$this->methodName\" has a void return type", $this); |
|
281 | - } |
|
282 | - |
|
283 | - return $this->will(new Promise\ReturnArgumentPromise($index)); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Sets throw promise to the prophecy. |
|
288 | - * |
|
289 | - * @see \Prophecy\Promise\ThrowPromise |
|
290 | - * |
|
291 | - * @param string|\Exception $exception Exception class or instance |
|
292 | - * |
|
293 | - * @return $this |
|
294 | - */ |
|
295 | - public function willThrow($exception) |
|
296 | - { |
|
297 | - return $this->will(new Promise\ThrowPromise($exception)); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Sets custom prediction to the prophecy. |
|
302 | - * |
|
303 | - * @param callable|Prediction\PredictionInterface $prediction |
|
304 | - * |
|
305 | - * @return $this |
|
306 | - * |
|
307 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
308 | - */ |
|
309 | - public function should($prediction) |
|
310 | - { |
|
311 | - if (is_callable($prediction)) { |
|
312 | - $prediction = new Prediction\CallbackPrediction($prediction); |
|
313 | - } |
|
314 | - |
|
315 | - if (!$prediction instanceof Prediction\PredictionInterface) { |
|
316 | - throw new InvalidArgumentException(sprintf( |
|
317 | - 'Expected callable or instance of PredictionInterface, but got %s.', |
|
318 | - gettype($prediction) |
|
319 | - )); |
|
320 | - } |
|
321 | - |
|
322 | - $this->bindToObjectProphecy(); |
|
323 | - $this->prediction = $prediction; |
|
324 | - |
|
325 | - return $this; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Sets call prediction to the prophecy. |
|
330 | - * |
|
331 | - * @see \Prophecy\Prediction\CallPrediction |
|
332 | - * |
|
333 | - * @return $this |
|
334 | - */ |
|
335 | - public function shouldBeCalled() |
|
336 | - { |
|
337 | - return $this->should(new Prediction\CallPrediction); |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Sets no calls prediction to the prophecy. |
|
342 | - * |
|
343 | - * @see \Prophecy\Prediction\NoCallsPrediction |
|
344 | - * |
|
345 | - * @return $this |
|
346 | - */ |
|
347 | - public function shouldNotBeCalled() |
|
348 | - { |
|
349 | - return $this->should(new Prediction\NoCallsPrediction); |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Sets call times prediction to the prophecy. |
|
354 | - * |
|
355 | - * @see \Prophecy\Prediction\CallTimesPrediction |
|
356 | - * |
|
357 | - * @param $count |
|
358 | - * |
|
359 | - * @return $this |
|
360 | - */ |
|
361 | - public function shouldBeCalledTimes($count) |
|
362 | - { |
|
363 | - return $this->should(new Prediction\CallTimesPrediction($count)); |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Sets call times prediction to the prophecy. |
|
368 | - * |
|
369 | - * @see \Prophecy\Prediction\CallTimesPrediction |
|
370 | - * |
|
371 | - * @return $this |
|
372 | - */ |
|
373 | - public function shouldBeCalledOnce() |
|
374 | - { |
|
375 | - return $this->shouldBeCalledTimes(1); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * Checks provided prediction immediately. |
|
380 | - * |
|
381 | - * @param callable|Prediction\PredictionInterface $prediction |
|
382 | - * |
|
383 | - * @return $this |
|
384 | - * |
|
385 | - * @throws \Prophecy\Exception\InvalidArgumentException |
|
386 | - */ |
|
387 | - public function shouldHave($prediction) |
|
388 | - { |
|
389 | - if (is_callable($prediction)) { |
|
390 | - $prediction = new Prediction\CallbackPrediction($prediction); |
|
391 | - } |
|
392 | - |
|
393 | - if (!$prediction instanceof Prediction\PredictionInterface) { |
|
394 | - throw new InvalidArgumentException(sprintf( |
|
395 | - 'Expected callable or instance of PredictionInterface, but got %s.', |
|
396 | - gettype($prediction) |
|
397 | - )); |
|
398 | - } |
|
399 | - |
|
400 | - if (null === $this->promise && !$this->voidReturnType) { |
|
401 | - $this->willReturn(); |
|
402 | - } |
|
403 | - |
|
404 | - $calls = $this->getObjectProphecy()->findProphecyMethodCalls( |
|
405 | - $this->getMethodName(), |
|
406 | - $this->getArgumentsWildcard() |
|
407 | - ); |
|
408 | - |
|
409 | - try { |
|
410 | - $prediction->check($calls, $this->getObjectProphecy(), $this); |
|
411 | - $this->checkedPredictions[] = $prediction; |
|
412 | - } catch (\Exception $e) { |
|
413 | - $this->checkedPredictions[] = $prediction; |
|
414 | - |
|
415 | - throw $e; |
|
416 | - } |
|
417 | - |
|
418 | - return $this; |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * Checks call prediction. |
|
423 | - * |
|
424 | - * @see \Prophecy\Prediction\CallPrediction |
|
425 | - * |
|
426 | - * @return $this |
|
427 | - */ |
|
428 | - public function shouldHaveBeenCalled() |
|
429 | - { |
|
430 | - return $this->shouldHave(new Prediction\CallPrediction); |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Checks no calls prediction. |
|
435 | - * |
|
436 | - * @see \Prophecy\Prediction\NoCallsPrediction |
|
437 | - * |
|
438 | - * @return $this |
|
439 | - */ |
|
440 | - public function shouldNotHaveBeenCalled() |
|
441 | - { |
|
442 | - return $this->shouldHave(new Prediction\NoCallsPrediction); |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * Checks no calls prediction. |
|
447 | - * |
|
448 | - * @see \Prophecy\Prediction\NoCallsPrediction |
|
449 | - * @deprecated |
|
450 | - * |
|
451 | - * @return $this |
|
452 | - */ |
|
453 | - public function shouldNotBeenCalled() |
|
454 | - { |
|
455 | - return $this->shouldNotHaveBeenCalled(); |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Checks call times prediction. |
|
460 | - * |
|
461 | - * @see \Prophecy\Prediction\CallTimesPrediction |
|
462 | - * |
|
463 | - * @param int $count |
|
464 | - * |
|
465 | - * @return $this |
|
466 | - */ |
|
467 | - public function shouldHaveBeenCalledTimes($count) |
|
468 | - { |
|
469 | - return $this->shouldHave(new Prediction\CallTimesPrediction($count)); |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Checks call times prediction. |
|
474 | - * |
|
475 | - * @see \Prophecy\Prediction\CallTimesPrediction |
|
476 | - * |
|
477 | - * @return $this |
|
478 | - */ |
|
479 | - public function shouldHaveBeenCalledOnce() |
|
480 | - { |
|
481 | - return $this->shouldHaveBeenCalledTimes(1); |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Checks currently registered [with should(...)] prediction. |
|
486 | - */ |
|
487 | - public function checkPrediction() |
|
488 | - { |
|
489 | - if (null === $this->prediction) { |
|
490 | - return; |
|
491 | - } |
|
492 | - |
|
493 | - $this->shouldHave($this->prediction); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Returns currently registered promise. |
|
498 | - * |
|
499 | - * @return null|Promise\PromiseInterface |
|
500 | - */ |
|
501 | - public function getPromise() |
|
502 | - { |
|
503 | - return $this->promise; |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Returns currently registered prediction. |
|
508 | - * |
|
509 | - * @return null|Prediction\PredictionInterface |
|
510 | - */ |
|
511 | - public function getPrediction() |
|
512 | - { |
|
513 | - return $this->prediction; |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * Returns predictions that were checked on this object. |
|
518 | - * |
|
519 | - * @return Prediction\PredictionInterface[] |
|
520 | - */ |
|
521 | - public function getCheckedPredictions() |
|
522 | - { |
|
523 | - return $this->checkedPredictions; |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Returns object prophecy this method prophecy is tied to. |
|
528 | - * |
|
529 | - * @return ObjectProphecy |
|
530 | - */ |
|
531 | - public function getObjectProphecy() |
|
532 | - { |
|
533 | - return $this->objectProphecy; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Returns method name. |
|
538 | - * |
|
539 | - * @return string |
|
540 | - */ |
|
541 | - public function getMethodName() |
|
542 | - { |
|
543 | - return $this->methodName; |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Returns arguments wildcard. |
|
548 | - * |
|
549 | - * @return Argument\ArgumentsWildcard |
|
550 | - */ |
|
551 | - public function getArgumentsWildcard() |
|
552 | - { |
|
553 | - return $this->argumentsWildcard; |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * @return bool |
|
558 | - */ |
|
559 | - public function hasReturnVoid() |
|
560 | - { |
|
561 | - return $this->voidReturnType; |
|
562 | - } |
|
563 | - |
|
564 | - private function bindToObjectProphecy() |
|
565 | - { |
|
566 | - if ($this->bound) { |
|
567 | - return; |
|
568 | - } |
|
569 | - |
|
570 | - $this->getObjectProphecy()->addMethodProphecy($this); |
|
571 | - $this->bound = true; |
|
572 | - } |
|
32 | + private $objectProphecy; |
|
33 | + private $methodName; |
|
34 | + private $argumentsWildcard; |
|
35 | + private $promise; |
|
36 | + private $prediction; |
|
37 | + private $checkedPredictions = array(); |
|
38 | + private $bound = false; |
|
39 | + private $voidReturnType = false; |
|
40 | + |
|
41 | + /** |
|
42 | + * Initializes method prophecy. |
|
43 | + * |
|
44 | + * @param ObjectProphecy $objectProphecy |
|
45 | + * @param string $methodName |
|
46 | + * @param null|Argument\ArgumentsWildcard|array $arguments |
|
47 | + * |
|
48 | + * @throws \Prophecy\Exception\Doubler\MethodNotFoundException If method not found |
|
49 | + */ |
|
50 | + public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null) |
|
51 | + { |
|
52 | + $double = $objectProphecy->reveal(); |
|
53 | + if (!method_exists($double, $methodName)) { |
|
54 | + throw new MethodNotFoundException(sprintf( |
|
55 | + 'Method `%s::%s()` is not defined.', get_class($double), $methodName |
|
56 | + ), get_class($double), $methodName, $arguments); |
|
57 | + } |
|
58 | + |
|
59 | + $this->objectProphecy = $objectProphecy; |
|
60 | + $this->methodName = $methodName; |
|
61 | + |
|
62 | + $reflectedMethod = new \ReflectionMethod($double, $methodName); |
|
63 | + if ($reflectedMethod->isFinal()) { |
|
64 | + throw new MethodProphecyException(sprintf( |
|
65 | + "Can not add prophecy for a method `%s::%s()`\n". |
|
66 | + "as it is a final method.", |
|
67 | + get_class($double), |
|
68 | + $methodName |
|
69 | + ), $this); |
|
70 | + } |
|
71 | + |
|
72 | + if (null !== $arguments) { |
|
73 | + $this->withArguments($arguments); |
|
74 | + } |
|
75 | + |
|
76 | + $hasTentativeReturnType = method_exists($reflectedMethod, 'hasTentativeReturnType') |
|
77 | + && $reflectedMethod->hasTentativeReturnType(); |
|
78 | + |
|
79 | + if (true === $reflectedMethod->hasReturnType() || $hasTentativeReturnType) { |
|
80 | + if ($hasTentativeReturnType) { |
|
81 | + $reflectionType = $reflectedMethod->getTentativeReturnType(); |
|
82 | + } |
|
83 | + else { |
|
84 | + $reflectionType = $reflectedMethod->getReturnType(); |
|
85 | + } |
|
86 | + |
|
87 | + if ($reflectionType instanceof ReflectionNamedType) { |
|
88 | + $types = [$reflectionType]; |
|
89 | + } |
|
90 | + elseif ($reflectionType instanceof ReflectionUnionType) { |
|
91 | + $types = $reflectionType->getTypes(); |
|
92 | + } |
|
93 | + |
|
94 | + $types = array_map( |
|
95 | + function(ReflectionType $type) { return $type->getName(); }, |
|
96 | + $types |
|
97 | + ); |
|
98 | + |
|
99 | + usort( |
|
100 | + $types, |
|
101 | + static function(string $type1, string $type2) { |
|
102 | + |
|
103 | + // null is lowest priority |
|
104 | + if ($type2 == 'null') { |
|
105 | + return -1; |
|
106 | + } |
|
107 | + elseif ($type1 == 'null') { |
|
108 | + return 1; |
|
109 | + } |
|
110 | + |
|
111 | + // objects are higher priority than scalars |
|
112 | + $isObject = static function($type) { |
|
113 | + return class_exists($type) || interface_exists($type); |
|
114 | + }; |
|
115 | + |
|
116 | + if($isObject($type1) && !$isObject($type2)) { |
|
117 | + return -1; |
|
118 | + } |
|
119 | + elseif(!$isObject($type1) && $isObject($type2)) |
|
120 | + { |
|
121 | + return 1; |
|
122 | + } |
|
123 | + |
|
124 | + // don't sort both-scalars or both-objects |
|
125 | + return 0; |
|
126 | + } |
|
127 | + ); |
|
128 | + |
|
129 | + $defaultType = $types[0]; |
|
130 | + |
|
131 | + if ('void' === $defaultType) { |
|
132 | + $this->voidReturnType = true; |
|
133 | + } |
|
134 | + |
|
135 | + $this->will(function () use ($defaultType) { |
|
136 | + switch ($defaultType) { |
|
137 | + case 'void': return; |
|
138 | + case 'string': return ''; |
|
139 | + case 'float': return 0.0; |
|
140 | + case 'int': return 0; |
|
141 | + case 'bool': return false; |
|
142 | + case 'array': return array(); |
|
143 | + |
|
144 | + case 'callable': |
|
145 | + case 'Closure': |
|
146 | + return function () {}; |
|
147 | + |
|
148 | + case 'Traversable': |
|
149 | + case 'Generator': |
|
150 | + return (function () { yield; })(); |
|
151 | + |
|
152 | + default: |
|
153 | + $prophet = new Prophet; |
|
154 | + return $prophet->prophesize($defaultType)->reveal(); |
|
155 | + } |
|
156 | + }); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Sets argument wildcard. |
|
162 | + * |
|
163 | + * @param array|Argument\ArgumentsWildcard $arguments |
|
164 | + * |
|
165 | + * @return $this |
|
166 | + * |
|
167 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
168 | + */ |
|
169 | + public function withArguments($arguments) |
|
170 | + { |
|
171 | + if (is_array($arguments)) { |
|
172 | + $arguments = new Argument\ArgumentsWildcard($arguments); |
|
173 | + } |
|
174 | + |
|
175 | + if (!$arguments instanceof Argument\ArgumentsWildcard) { |
|
176 | + throw new InvalidArgumentException(sprintf( |
|
177 | + "Either an array or an instance of ArgumentsWildcard expected as\n". |
|
178 | + 'a `MethodProphecy::withArguments()` argument, but got %s.', |
|
179 | + gettype($arguments) |
|
180 | + )); |
|
181 | + } |
|
182 | + |
|
183 | + $this->argumentsWildcard = $arguments; |
|
184 | + |
|
185 | + return $this; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Sets custom promise to the prophecy. |
|
190 | + * |
|
191 | + * @param callable|Promise\PromiseInterface $promise |
|
192 | + * |
|
193 | + * @return $this |
|
194 | + * |
|
195 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
196 | + */ |
|
197 | + public function will($promise) |
|
198 | + { |
|
199 | + if (is_callable($promise)) { |
|
200 | + $promise = new Promise\CallbackPromise($promise); |
|
201 | + } |
|
202 | + |
|
203 | + if (!$promise instanceof Promise\PromiseInterface) { |
|
204 | + throw new InvalidArgumentException(sprintf( |
|
205 | + 'Expected callable or instance of PromiseInterface, but got %s.', |
|
206 | + gettype($promise) |
|
207 | + )); |
|
208 | + } |
|
209 | + |
|
210 | + $this->bindToObjectProphecy(); |
|
211 | + $this->promise = $promise; |
|
212 | + |
|
213 | + return $this; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Sets return promise to the prophecy. |
|
218 | + * |
|
219 | + * @see \Prophecy\Promise\ReturnPromise |
|
220 | + * |
|
221 | + * @return $this |
|
222 | + */ |
|
223 | + public function willReturn() |
|
224 | + { |
|
225 | + if ($this->voidReturnType) { |
|
226 | + throw new MethodProphecyException( |
|
227 | + "The method \"$this->methodName\" has a void return type, and so cannot return anything", |
|
228 | + $this |
|
229 | + ); |
|
230 | + } |
|
231 | + |
|
232 | + return $this->will(new Promise\ReturnPromise(func_get_args())); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param array $items |
|
237 | + * @param mixed $return |
|
238 | + * |
|
239 | + * @return $this |
|
240 | + * |
|
241 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
242 | + */ |
|
243 | + public function willYield($items, $return = null) |
|
244 | + { |
|
245 | + if ($this->voidReturnType) { |
|
246 | + throw new MethodProphecyException( |
|
247 | + "The method \"$this->methodName\" has a void return type, and so cannot yield anything", |
|
248 | + $this |
|
249 | + ); |
|
250 | + } |
|
251 | + |
|
252 | + if (!is_array($items)) { |
|
253 | + throw new InvalidArgumentException(sprintf( |
|
254 | + 'Expected array, but got %s.', |
|
255 | + gettype($items) |
|
256 | + )); |
|
257 | + } |
|
258 | + |
|
259 | + $generator = function() use ($items, $return) { |
|
260 | + yield from $items; |
|
261 | + |
|
262 | + return $return; |
|
263 | + }; |
|
264 | + |
|
265 | + return $this->will($generator); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Sets return argument promise to the prophecy. |
|
270 | + * |
|
271 | + * @param int $index The zero-indexed number of the argument to return |
|
272 | + * |
|
273 | + * @see \Prophecy\Promise\ReturnArgumentPromise |
|
274 | + * |
|
275 | + * @return $this |
|
276 | + */ |
|
277 | + public function willReturnArgument($index = 0) |
|
278 | + { |
|
279 | + if ($this->voidReturnType) { |
|
280 | + throw new MethodProphecyException("The method \"$this->methodName\" has a void return type", $this); |
|
281 | + } |
|
282 | + |
|
283 | + return $this->will(new Promise\ReturnArgumentPromise($index)); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Sets throw promise to the prophecy. |
|
288 | + * |
|
289 | + * @see \Prophecy\Promise\ThrowPromise |
|
290 | + * |
|
291 | + * @param string|\Exception $exception Exception class or instance |
|
292 | + * |
|
293 | + * @return $this |
|
294 | + */ |
|
295 | + public function willThrow($exception) |
|
296 | + { |
|
297 | + return $this->will(new Promise\ThrowPromise($exception)); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Sets custom prediction to the prophecy. |
|
302 | + * |
|
303 | + * @param callable|Prediction\PredictionInterface $prediction |
|
304 | + * |
|
305 | + * @return $this |
|
306 | + * |
|
307 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
308 | + */ |
|
309 | + public function should($prediction) |
|
310 | + { |
|
311 | + if (is_callable($prediction)) { |
|
312 | + $prediction = new Prediction\CallbackPrediction($prediction); |
|
313 | + } |
|
314 | + |
|
315 | + if (!$prediction instanceof Prediction\PredictionInterface) { |
|
316 | + throw new InvalidArgumentException(sprintf( |
|
317 | + 'Expected callable or instance of PredictionInterface, but got %s.', |
|
318 | + gettype($prediction) |
|
319 | + )); |
|
320 | + } |
|
321 | + |
|
322 | + $this->bindToObjectProphecy(); |
|
323 | + $this->prediction = $prediction; |
|
324 | + |
|
325 | + return $this; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Sets call prediction to the prophecy. |
|
330 | + * |
|
331 | + * @see \Prophecy\Prediction\CallPrediction |
|
332 | + * |
|
333 | + * @return $this |
|
334 | + */ |
|
335 | + public function shouldBeCalled() |
|
336 | + { |
|
337 | + return $this->should(new Prediction\CallPrediction); |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Sets no calls prediction to the prophecy. |
|
342 | + * |
|
343 | + * @see \Prophecy\Prediction\NoCallsPrediction |
|
344 | + * |
|
345 | + * @return $this |
|
346 | + */ |
|
347 | + public function shouldNotBeCalled() |
|
348 | + { |
|
349 | + return $this->should(new Prediction\NoCallsPrediction); |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Sets call times prediction to the prophecy. |
|
354 | + * |
|
355 | + * @see \Prophecy\Prediction\CallTimesPrediction |
|
356 | + * |
|
357 | + * @param $count |
|
358 | + * |
|
359 | + * @return $this |
|
360 | + */ |
|
361 | + public function shouldBeCalledTimes($count) |
|
362 | + { |
|
363 | + return $this->should(new Prediction\CallTimesPrediction($count)); |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Sets call times prediction to the prophecy. |
|
368 | + * |
|
369 | + * @see \Prophecy\Prediction\CallTimesPrediction |
|
370 | + * |
|
371 | + * @return $this |
|
372 | + */ |
|
373 | + public function shouldBeCalledOnce() |
|
374 | + { |
|
375 | + return $this->shouldBeCalledTimes(1); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * Checks provided prediction immediately. |
|
380 | + * |
|
381 | + * @param callable|Prediction\PredictionInterface $prediction |
|
382 | + * |
|
383 | + * @return $this |
|
384 | + * |
|
385 | + * @throws \Prophecy\Exception\InvalidArgumentException |
|
386 | + */ |
|
387 | + public function shouldHave($prediction) |
|
388 | + { |
|
389 | + if (is_callable($prediction)) { |
|
390 | + $prediction = new Prediction\CallbackPrediction($prediction); |
|
391 | + } |
|
392 | + |
|
393 | + if (!$prediction instanceof Prediction\PredictionInterface) { |
|
394 | + throw new InvalidArgumentException(sprintf( |
|
395 | + 'Expected callable or instance of PredictionInterface, but got %s.', |
|
396 | + gettype($prediction) |
|
397 | + )); |
|
398 | + } |
|
399 | + |
|
400 | + if (null === $this->promise && !$this->voidReturnType) { |
|
401 | + $this->willReturn(); |
|
402 | + } |
|
403 | + |
|
404 | + $calls = $this->getObjectProphecy()->findProphecyMethodCalls( |
|
405 | + $this->getMethodName(), |
|
406 | + $this->getArgumentsWildcard() |
|
407 | + ); |
|
408 | + |
|
409 | + try { |
|
410 | + $prediction->check($calls, $this->getObjectProphecy(), $this); |
|
411 | + $this->checkedPredictions[] = $prediction; |
|
412 | + } catch (\Exception $e) { |
|
413 | + $this->checkedPredictions[] = $prediction; |
|
414 | + |
|
415 | + throw $e; |
|
416 | + } |
|
417 | + |
|
418 | + return $this; |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * Checks call prediction. |
|
423 | + * |
|
424 | + * @see \Prophecy\Prediction\CallPrediction |
|
425 | + * |
|
426 | + * @return $this |
|
427 | + */ |
|
428 | + public function shouldHaveBeenCalled() |
|
429 | + { |
|
430 | + return $this->shouldHave(new Prediction\CallPrediction); |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Checks no calls prediction. |
|
435 | + * |
|
436 | + * @see \Prophecy\Prediction\NoCallsPrediction |
|
437 | + * |
|
438 | + * @return $this |
|
439 | + */ |
|
440 | + public function shouldNotHaveBeenCalled() |
|
441 | + { |
|
442 | + return $this->shouldHave(new Prediction\NoCallsPrediction); |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * Checks no calls prediction. |
|
447 | + * |
|
448 | + * @see \Prophecy\Prediction\NoCallsPrediction |
|
449 | + * @deprecated |
|
450 | + * |
|
451 | + * @return $this |
|
452 | + */ |
|
453 | + public function shouldNotBeenCalled() |
|
454 | + { |
|
455 | + return $this->shouldNotHaveBeenCalled(); |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Checks call times prediction. |
|
460 | + * |
|
461 | + * @see \Prophecy\Prediction\CallTimesPrediction |
|
462 | + * |
|
463 | + * @param int $count |
|
464 | + * |
|
465 | + * @return $this |
|
466 | + */ |
|
467 | + public function shouldHaveBeenCalledTimes($count) |
|
468 | + { |
|
469 | + return $this->shouldHave(new Prediction\CallTimesPrediction($count)); |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Checks call times prediction. |
|
474 | + * |
|
475 | + * @see \Prophecy\Prediction\CallTimesPrediction |
|
476 | + * |
|
477 | + * @return $this |
|
478 | + */ |
|
479 | + public function shouldHaveBeenCalledOnce() |
|
480 | + { |
|
481 | + return $this->shouldHaveBeenCalledTimes(1); |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Checks currently registered [with should(...)] prediction. |
|
486 | + */ |
|
487 | + public function checkPrediction() |
|
488 | + { |
|
489 | + if (null === $this->prediction) { |
|
490 | + return; |
|
491 | + } |
|
492 | + |
|
493 | + $this->shouldHave($this->prediction); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Returns currently registered promise. |
|
498 | + * |
|
499 | + * @return null|Promise\PromiseInterface |
|
500 | + */ |
|
501 | + public function getPromise() |
|
502 | + { |
|
503 | + return $this->promise; |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Returns currently registered prediction. |
|
508 | + * |
|
509 | + * @return null|Prediction\PredictionInterface |
|
510 | + */ |
|
511 | + public function getPrediction() |
|
512 | + { |
|
513 | + return $this->prediction; |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * Returns predictions that were checked on this object. |
|
518 | + * |
|
519 | + * @return Prediction\PredictionInterface[] |
|
520 | + */ |
|
521 | + public function getCheckedPredictions() |
|
522 | + { |
|
523 | + return $this->checkedPredictions; |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Returns object prophecy this method prophecy is tied to. |
|
528 | + * |
|
529 | + * @return ObjectProphecy |
|
530 | + */ |
|
531 | + public function getObjectProphecy() |
|
532 | + { |
|
533 | + return $this->objectProphecy; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Returns method name. |
|
538 | + * |
|
539 | + * @return string |
|
540 | + */ |
|
541 | + public function getMethodName() |
|
542 | + { |
|
543 | + return $this->methodName; |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Returns arguments wildcard. |
|
548 | + * |
|
549 | + * @return Argument\ArgumentsWildcard |
|
550 | + */ |
|
551 | + public function getArgumentsWildcard() |
|
552 | + { |
|
553 | + return $this->argumentsWildcard; |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * @return bool |
|
558 | + */ |
|
559 | + public function hasReturnVoid() |
|
560 | + { |
|
561 | + return $this->voidReturnType; |
|
562 | + } |
|
563 | + |
|
564 | + private function bindToObjectProphecy() |
|
565 | + { |
|
566 | + if ($this->bound) { |
|
567 | + return; |
|
568 | + } |
|
569 | + |
|
570 | + $this->getObjectProphecy()->addMethodProphecy($this); |
|
571 | + $this->bound = true; |
|
572 | + } |
|
573 | 573 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null) |
51 | 51 | { |
52 | 52 | $double = $objectProphecy->reveal(); |
53 | - if (!method_exists($double, $methodName)) { |
|
53 | + if ( ! method_exists($double, $methodName)) { |
|
54 | 54 | throw new MethodNotFoundException(sprintf( |
55 | 55 | 'Method `%s::%s()` is not defined.', get_class($double), $methodName |
56 | 56 | ), get_class($double), $methodName, $arguments); |
@@ -113,10 +113,10 @@ discard block |
||
113 | 113 | return class_exists($type) || interface_exists($type); |
114 | 114 | }; |
115 | 115 | |
116 | - if($isObject($type1) && !$isObject($type2)) { |
|
116 | + if ($isObject($type1) && ! $isObject($type2)) { |
|
117 | 117 | return -1; |
118 | 118 | } |
119 | - elseif(!$isObject($type1) && $isObject($type2)) |
|
119 | + elseif ( ! $isObject($type1) && $isObject($type2)) |
|
120 | 120 | { |
121 | 121 | return 1; |
122 | 122 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | $this->voidReturnType = true; |
133 | 133 | } |
134 | 134 | |
135 | - $this->will(function () use ($defaultType) { |
|
135 | + $this->will(function() use ($defaultType) { |
|
136 | 136 | switch ($defaultType) { |
137 | 137 | case 'void': return; |
138 | 138 | case 'string': return ''; |
@@ -143,11 +143,11 @@ discard block |
||
143 | 143 | |
144 | 144 | case 'callable': |
145 | 145 | case 'Closure': |
146 | - return function () {}; |
|
146 | + return function() {}; |
|
147 | 147 | |
148 | 148 | case 'Traversable': |
149 | 149 | case 'Generator': |
150 | - return (function () { yield; })(); |
|
150 | + return (function() { yield; })(); |
|
151 | 151 | |
152 | 152 | default: |
153 | 153 | $prophet = new Prophet; |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | $arguments = new Argument\ArgumentsWildcard($arguments); |
173 | 173 | } |
174 | 174 | |
175 | - if (!$arguments instanceof Argument\ArgumentsWildcard) { |
|
175 | + if ( ! $arguments instanceof Argument\ArgumentsWildcard) { |
|
176 | 176 | throw new InvalidArgumentException(sprintf( |
177 | 177 | "Either an array or an instance of ArgumentsWildcard expected as\n". |
178 | 178 | 'a `MethodProphecy::withArguments()` argument, but got %s.', |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | $promise = new Promise\CallbackPromise($promise); |
201 | 201 | } |
202 | 202 | |
203 | - if (!$promise instanceof Promise\PromiseInterface) { |
|
203 | + if ( ! $promise instanceof Promise\PromiseInterface) { |
|
204 | 204 | throw new InvalidArgumentException(sprintf( |
205 | 205 | 'Expected callable or instance of PromiseInterface, but got %s.', |
206 | 206 | gettype($promise) |
@@ -249,14 +249,14 @@ discard block |
||
249 | 249 | ); |
250 | 250 | } |
251 | 251 | |
252 | - if (!is_array($items)) { |
|
252 | + if ( ! is_array($items)) { |
|
253 | 253 | throw new InvalidArgumentException(sprintf( |
254 | 254 | 'Expected array, but got %s.', |
255 | 255 | gettype($items) |
256 | 256 | )); |
257 | 257 | } |
258 | 258 | |
259 | - $generator = function() use ($items, $return) { |
|
259 | + $generator = function() use ($items, $return) { |
|
260 | 260 | yield from $items; |
261 | 261 | |
262 | 262 | return $return; |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | $prediction = new Prediction\CallbackPrediction($prediction); |
313 | 313 | } |
314 | 314 | |
315 | - if (!$prediction instanceof Prediction\PredictionInterface) { |
|
315 | + if ( ! $prediction instanceof Prediction\PredictionInterface) { |
|
316 | 316 | throw new InvalidArgumentException(sprintf( |
317 | 317 | 'Expected callable or instance of PredictionInterface, but got %s.', |
318 | 318 | gettype($prediction) |
@@ -390,14 +390,14 @@ discard block |
||
390 | 390 | $prediction = new Prediction\CallbackPrediction($prediction); |
391 | 391 | } |
392 | 392 | |
393 | - if (!$prediction instanceof Prediction\PredictionInterface) { |
|
393 | + if ( ! $prediction instanceof Prediction\PredictionInterface) { |
|
394 | 394 | throw new InvalidArgumentException(sprintf( |
395 | 395 | 'Expected callable or instance of PredictionInterface, but got %s.', |
396 | 396 | gettype($prediction) |
397 | 397 | )); |
398 | 398 | } |
399 | 399 | |
400 | - if (null === $this->promise && !$this->voidReturnType) { |
|
400 | + if (null === $this->promise && ! $this->voidReturnType) { |
|
401 | 401 | $this->willReturn(); |
402 | 402 | } |
403 | 403 |
@@ -79,15 +79,13 @@ discard block |
||
79 | 79 | if (true === $reflectedMethod->hasReturnType() || $hasTentativeReturnType) { |
80 | 80 | if ($hasTentativeReturnType) { |
81 | 81 | $reflectionType = $reflectedMethod->getTentativeReturnType(); |
82 | - } |
|
83 | - else { |
|
82 | + } else { |
|
84 | 83 | $reflectionType = $reflectedMethod->getReturnType(); |
85 | 84 | } |
86 | 85 | |
87 | 86 | if ($reflectionType instanceof ReflectionNamedType) { |
88 | 87 | $types = [$reflectionType]; |
89 | - } |
|
90 | - elseif ($reflectionType instanceof ReflectionUnionType) { |
|
88 | + } elseif ($reflectionType instanceof ReflectionUnionType) { |
|
91 | 89 | $types = $reflectionType->getTypes(); |
92 | 90 | } |
93 | 91 | |
@@ -103,8 +101,7 @@ discard block |
||
103 | 101 | // null is lowest priority |
104 | 102 | if ($type2 == 'null') { |
105 | 103 | return -1; |
106 | - } |
|
107 | - elseif ($type1 == 'null') { |
|
104 | + } elseif ($type1 == 'null') { |
|
108 | 105 | return 1; |
109 | 106 | } |
110 | 107 | |
@@ -115,8 +112,7 @@ discard block |
||
115 | 112 | |
116 | 113 | if($isObject($type1) && !$isObject($type2)) { |
117 | 114 | return -1; |
118 | - } |
|
119 | - elseif(!$isObject($type1) && $isObject($type2)) |
|
115 | + } elseif(!$isObject($type1) && $isObject($type2)) |
|
120 | 116 | { |
121 | 117 | return 1; |
122 | 118 | } |
@@ -29,258 +29,258 @@ |
||
29 | 29 | */ |
30 | 30 | class ObjectProphecy implements ProphecyInterface |
31 | 31 | { |
32 | - private $lazyDouble; |
|
33 | - private $callCenter; |
|
34 | - private $revealer; |
|
35 | - private $comparatorFactory; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var MethodProphecy[][] |
|
39 | - */ |
|
40 | - private $methodProphecies = array(); |
|
41 | - |
|
42 | - /** |
|
43 | - * Initializes object prophecy. |
|
44 | - * |
|
45 | - * @param LazyDouble $lazyDouble |
|
46 | - * @param CallCenter $callCenter |
|
47 | - * @param RevealerInterface $revealer |
|
48 | - * @param ComparatorFactory $comparatorFactory |
|
49 | - */ |
|
50 | - public function __construct( |
|
51 | - LazyDouble $lazyDouble, |
|
52 | - CallCenter $callCenter = null, |
|
53 | - RevealerInterface $revealer = null, |
|
54 | - ComparatorFactory $comparatorFactory = null |
|
55 | - ) { |
|
56 | - $this->lazyDouble = $lazyDouble; |
|
57 | - $this->callCenter = $callCenter ?: new CallCenter; |
|
58 | - $this->revealer = $revealer ?: new Revealer; |
|
59 | - |
|
60 | - $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance(); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Forces double to extend specific class. |
|
65 | - * |
|
66 | - * @param string $class |
|
67 | - * |
|
68 | - * @return $this |
|
69 | - */ |
|
70 | - public function willExtend($class) |
|
71 | - { |
|
72 | - $this->lazyDouble->setParentClass($class); |
|
73 | - |
|
74 | - return $this; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Forces double to implement specific interface. |
|
79 | - * |
|
80 | - * @param string $interface |
|
81 | - * |
|
82 | - * @return $this |
|
83 | - */ |
|
84 | - public function willImplement($interface) |
|
85 | - { |
|
86 | - $this->lazyDouble->addInterface($interface); |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Sets constructor arguments. |
|
93 | - * |
|
94 | - * @param array $arguments |
|
95 | - * |
|
96 | - * @return $this |
|
97 | - */ |
|
98 | - public function willBeConstructedWith(array $arguments = null) |
|
99 | - { |
|
100 | - $this->lazyDouble->setArguments($arguments); |
|
101 | - |
|
102 | - return $this; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Reveals double. |
|
107 | - * |
|
108 | - * @return object |
|
109 | - * |
|
110 | - * @throws \Prophecy\Exception\Prophecy\ObjectProphecyException If double doesn't implement needed interface |
|
111 | - */ |
|
112 | - public function reveal() |
|
113 | - { |
|
114 | - $double = $this->lazyDouble->getInstance(); |
|
115 | - |
|
116 | - if (null === $double || !$double instanceof ProphecySubjectInterface) { |
|
117 | - throw new ObjectProphecyException( |
|
118 | - "Generated double must implement ProphecySubjectInterface, but it does not.\n". |
|
119 | - 'It seems you have wrongly configured doubler without required ClassPatch.', |
|
120 | - $this |
|
121 | - ); |
|
122 | - } |
|
123 | - |
|
124 | - $double->setProphecy($this); |
|
125 | - |
|
126 | - return $double; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Adds method prophecy to object prophecy. |
|
131 | - * |
|
132 | - * @param MethodProphecy $methodProphecy |
|
133 | - * |
|
134 | - * @throws \Prophecy\Exception\Prophecy\MethodProphecyException If method prophecy doesn't |
|
135 | - * have arguments wildcard |
|
136 | - */ |
|
137 | - public function addMethodProphecy(MethodProphecy $methodProphecy) |
|
138 | - { |
|
139 | - $argumentsWildcard = $methodProphecy->getArgumentsWildcard(); |
|
140 | - if (null === $argumentsWildcard) { |
|
141 | - throw new MethodProphecyException(sprintf( |
|
142 | - "Can not add prophecy for a method `%s::%s()`\n". |
|
143 | - "as you did not specify arguments wildcard for it.", |
|
144 | - get_class($this->reveal()), |
|
145 | - $methodProphecy->getMethodName() |
|
146 | - ), $methodProphecy); |
|
147 | - } |
|
148 | - |
|
149 | - $methodName = strtolower($methodProphecy->getMethodName()); |
|
150 | - |
|
151 | - if (!isset($this->methodProphecies[$methodName])) { |
|
152 | - $this->methodProphecies[$methodName] = array(); |
|
153 | - } |
|
154 | - |
|
155 | - $this->methodProphecies[$methodName][] = $methodProphecy; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Returns either all or related to single method prophecies. |
|
160 | - * |
|
161 | - * @param null|string $methodName |
|
162 | - * |
|
163 | - * @return MethodProphecy[] |
|
164 | - */ |
|
165 | - public function getMethodProphecies($methodName = null) |
|
166 | - { |
|
167 | - if (null === $methodName) { |
|
168 | - return $this->methodProphecies; |
|
169 | - } |
|
170 | - |
|
171 | - $methodName = strtolower($methodName); |
|
172 | - |
|
173 | - if (!isset($this->methodProphecies[$methodName])) { |
|
174 | - return array(); |
|
175 | - } |
|
176 | - |
|
177 | - return $this->methodProphecies[$methodName]; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Makes specific method call. |
|
182 | - * |
|
183 | - * @param string $methodName |
|
184 | - * @param array $arguments |
|
185 | - * |
|
186 | - * @return mixed |
|
187 | - */ |
|
188 | - public function makeProphecyMethodCall($methodName, array $arguments) |
|
189 | - { |
|
190 | - $arguments = $this->revealer->reveal($arguments); |
|
191 | - $return = $this->callCenter->makeCall($this, $methodName, $arguments); |
|
192 | - |
|
193 | - return $this->revealer->reveal($return); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Finds calls by method name & arguments wildcard. |
|
198 | - * |
|
199 | - * @param string $methodName |
|
200 | - * @param ArgumentsWildcard $wildcard |
|
201 | - * |
|
202 | - * @return Call[] |
|
203 | - */ |
|
204 | - public function findProphecyMethodCalls($methodName, ArgumentsWildcard $wildcard) |
|
205 | - { |
|
206 | - return $this->callCenter->findCalls($methodName, $wildcard); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Checks that registered method predictions do not fail. |
|
211 | - * |
|
212 | - * @throws \Prophecy\Exception\Prediction\AggregateException If any of registered predictions fail |
|
213 | - * @throws \Prophecy\Exception\Call\UnexpectedCallException |
|
214 | - */ |
|
215 | - public function checkProphecyMethodsPredictions() |
|
216 | - { |
|
217 | - $exception = new AggregateException(sprintf("%s:\n", get_class($this->reveal()))); |
|
218 | - $exception->setObjectProphecy($this); |
|
219 | - |
|
220 | - $this->callCenter->checkUnexpectedCalls(); |
|
221 | - |
|
222 | - foreach ($this->methodProphecies as $prophecies) { |
|
223 | - foreach ($prophecies as $prophecy) { |
|
224 | - try { |
|
225 | - $prophecy->checkPrediction(); |
|
226 | - } catch (PredictionException $e) { |
|
227 | - $exception->append($e); |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - if (count($exception->getExceptions())) { |
|
233 | - throw $exception; |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Creates new method prophecy using specified method name and arguments. |
|
239 | - * |
|
240 | - * @param string $methodName |
|
241 | - * @param array $arguments |
|
242 | - * |
|
243 | - * @return MethodProphecy |
|
244 | - */ |
|
245 | - public function __call($methodName, array $arguments) |
|
246 | - { |
|
247 | - $arguments = new ArgumentsWildcard($this->revealer->reveal($arguments)); |
|
248 | - |
|
249 | - foreach ($this->getMethodProphecies($methodName) as $prophecy) { |
|
250 | - $argumentsWildcard = $prophecy->getArgumentsWildcard(); |
|
251 | - $comparator = $this->comparatorFactory->getComparatorFor( |
|
252 | - $argumentsWildcard, $arguments |
|
253 | - ); |
|
254 | - |
|
255 | - try { |
|
256 | - $comparator->assertEquals($argumentsWildcard, $arguments); |
|
257 | - return $prophecy; |
|
258 | - } catch (ComparisonFailure $failure) {} |
|
259 | - } |
|
260 | - |
|
261 | - return new MethodProphecy($this, $methodName, $arguments); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Tries to get property value from double. |
|
266 | - * |
|
267 | - * @param string $name |
|
268 | - * |
|
269 | - * @return mixed |
|
270 | - */ |
|
271 | - public function __get($name) |
|
272 | - { |
|
273 | - return $this->reveal()->$name; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Tries to set property value to double. |
|
278 | - * |
|
279 | - * @param string $name |
|
280 | - * @param mixed $value |
|
281 | - */ |
|
282 | - public function __set($name, $value) |
|
283 | - { |
|
284 | - $this->reveal()->$name = $this->revealer->reveal($value); |
|
285 | - } |
|
32 | + private $lazyDouble; |
|
33 | + private $callCenter; |
|
34 | + private $revealer; |
|
35 | + private $comparatorFactory; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var MethodProphecy[][] |
|
39 | + */ |
|
40 | + private $methodProphecies = array(); |
|
41 | + |
|
42 | + /** |
|
43 | + * Initializes object prophecy. |
|
44 | + * |
|
45 | + * @param LazyDouble $lazyDouble |
|
46 | + * @param CallCenter $callCenter |
|
47 | + * @param RevealerInterface $revealer |
|
48 | + * @param ComparatorFactory $comparatorFactory |
|
49 | + */ |
|
50 | + public function __construct( |
|
51 | + LazyDouble $lazyDouble, |
|
52 | + CallCenter $callCenter = null, |
|
53 | + RevealerInterface $revealer = null, |
|
54 | + ComparatorFactory $comparatorFactory = null |
|
55 | + ) { |
|
56 | + $this->lazyDouble = $lazyDouble; |
|
57 | + $this->callCenter = $callCenter ?: new CallCenter; |
|
58 | + $this->revealer = $revealer ?: new Revealer; |
|
59 | + |
|
60 | + $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance(); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Forces double to extend specific class. |
|
65 | + * |
|
66 | + * @param string $class |
|
67 | + * |
|
68 | + * @return $this |
|
69 | + */ |
|
70 | + public function willExtend($class) |
|
71 | + { |
|
72 | + $this->lazyDouble->setParentClass($class); |
|
73 | + |
|
74 | + return $this; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Forces double to implement specific interface. |
|
79 | + * |
|
80 | + * @param string $interface |
|
81 | + * |
|
82 | + * @return $this |
|
83 | + */ |
|
84 | + public function willImplement($interface) |
|
85 | + { |
|
86 | + $this->lazyDouble->addInterface($interface); |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Sets constructor arguments. |
|
93 | + * |
|
94 | + * @param array $arguments |
|
95 | + * |
|
96 | + * @return $this |
|
97 | + */ |
|
98 | + public function willBeConstructedWith(array $arguments = null) |
|
99 | + { |
|
100 | + $this->lazyDouble->setArguments($arguments); |
|
101 | + |
|
102 | + return $this; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Reveals double. |
|
107 | + * |
|
108 | + * @return object |
|
109 | + * |
|
110 | + * @throws \Prophecy\Exception\Prophecy\ObjectProphecyException If double doesn't implement needed interface |
|
111 | + */ |
|
112 | + public function reveal() |
|
113 | + { |
|
114 | + $double = $this->lazyDouble->getInstance(); |
|
115 | + |
|
116 | + if (null === $double || !$double instanceof ProphecySubjectInterface) { |
|
117 | + throw new ObjectProphecyException( |
|
118 | + "Generated double must implement ProphecySubjectInterface, but it does not.\n". |
|
119 | + 'It seems you have wrongly configured doubler without required ClassPatch.', |
|
120 | + $this |
|
121 | + ); |
|
122 | + } |
|
123 | + |
|
124 | + $double->setProphecy($this); |
|
125 | + |
|
126 | + return $double; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Adds method prophecy to object prophecy. |
|
131 | + * |
|
132 | + * @param MethodProphecy $methodProphecy |
|
133 | + * |
|
134 | + * @throws \Prophecy\Exception\Prophecy\MethodProphecyException If method prophecy doesn't |
|
135 | + * have arguments wildcard |
|
136 | + */ |
|
137 | + public function addMethodProphecy(MethodProphecy $methodProphecy) |
|
138 | + { |
|
139 | + $argumentsWildcard = $methodProphecy->getArgumentsWildcard(); |
|
140 | + if (null === $argumentsWildcard) { |
|
141 | + throw new MethodProphecyException(sprintf( |
|
142 | + "Can not add prophecy for a method `%s::%s()`\n". |
|
143 | + "as you did not specify arguments wildcard for it.", |
|
144 | + get_class($this->reveal()), |
|
145 | + $methodProphecy->getMethodName() |
|
146 | + ), $methodProphecy); |
|
147 | + } |
|
148 | + |
|
149 | + $methodName = strtolower($methodProphecy->getMethodName()); |
|
150 | + |
|
151 | + if (!isset($this->methodProphecies[$methodName])) { |
|
152 | + $this->methodProphecies[$methodName] = array(); |
|
153 | + } |
|
154 | + |
|
155 | + $this->methodProphecies[$methodName][] = $methodProphecy; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Returns either all or related to single method prophecies. |
|
160 | + * |
|
161 | + * @param null|string $methodName |
|
162 | + * |
|
163 | + * @return MethodProphecy[] |
|
164 | + */ |
|
165 | + public function getMethodProphecies($methodName = null) |
|
166 | + { |
|
167 | + if (null === $methodName) { |
|
168 | + return $this->methodProphecies; |
|
169 | + } |
|
170 | + |
|
171 | + $methodName = strtolower($methodName); |
|
172 | + |
|
173 | + if (!isset($this->methodProphecies[$methodName])) { |
|
174 | + return array(); |
|
175 | + } |
|
176 | + |
|
177 | + return $this->methodProphecies[$methodName]; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Makes specific method call. |
|
182 | + * |
|
183 | + * @param string $methodName |
|
184 | + * @param array $arguments |
|
185 | + * |
|
186 | + * @return mixed |
|
187 | + */ |
|
188 | + public function makeProphecyMethodCall($methodName, array $arguments) |
|
189 | + { |
|
190 | + $arguments = $this->revealer->reveal($arguments); |
|
191 | + $return = $this->callCenter->makeCall($this, $methodName, $arguments); |
|
192 | + |
|
193 | + return $this->revealer->reveal($return); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Finds calls by method name & arguments wildcard. |
|
198 | + * |
|
199 | + * @param string $methodName |
|
200 | + * @param ArgumentsWildcard $wildcard |
|
201 | + * |
|
202 | + * @return Call[] |
|
203 | + */ |
|
204 | + public function findProphecyMethodCalls($methodName, ArgumentsWildcard $wildcard) |
|
205 | + { |
|
206 | + return $this->callCenter->findCalls($methodName, $wildcard); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Checks that registered method predictions do not fail. |
|
211 | + * |
|
212 | + * @throws \Prophecy\Exception\Prediction\AggregateException If any of registered predictions fail |
|
213 | + * @throws \Prophecy\Exception\Call\UnexpectedCallException |
|
214 | + */ |
|
215 | + public function checkProphecyMethodsPredictions() |
|
216 | + { |
|
217 | + $exception = new AggregateException(sprintf("%s:\n", get_class($this->reveal()))); |
|
218 | + $exception->setObjectProphecy($this); |
|
219 | + |
|
220 | + $this->callCenter->checkUnexpectedCalls(); |
|
221 | + |
|
222 | + foreach ($this->methodProphecies as $prophecies) { |
|
223 | + foreach ($prophecies as $prophecy) { |
|
224 | + try { |
|
225 | + $prophecy->checkPrediction(); |
|
226 | + } catch (PredictionException $e) { |
|
227 | + $exception->append($e); |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + if (count($exception->getExceptions())) { |
|
233 | + throw $exception; |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Creates new method prophecy using specified method name and arguments. |
|
239 | + * |
|
240 | + * @param string $methodName |
|
241 | + * @param array $arguments |
|
242 | + * |
|
243 | + * @return MethodProphecy |
|
244 | + */ |
|
245 | + public function __call($methodName, array $arguments) |
|
246 | + { |
|
247 | + $arguments = new ArgumentsWildcard($this->revealer->reveal($arguments)); |
|
248 | + |
|
249 | + foreach ($this->getMethodProphecies($methodName) as $prophecy) { |
|
250 | + $argumentsWildcard = $prophecy->getArgumentsWildcard(); |
|
251 | + $comparator = $this->comparatorFactory->getComparatorFor( |
|
252 | + $argumentsWildcard, $arguments |
|
253 | + ); |
|
254 | + |
|
255 | + try { |
|
256 | + $comparator->assertEquals($argumentsWildcard, $arguments); |
|
257 | + return $prophecy; |
|
258 | + } catch (ComparisonFailure $failure) {} |
|
259 | + } |
|
260 | + |
|
261 | + return new MethodProphecy($this, $methodName, $arguments); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Tries to get property value from double. |
|
266 | + * |
|
267 | + * @param string $name |
|
268 | + * |
|
269 | + * @return mixed |
|
270 | + */ |
|
271 | + public function __get($name) |
|
272 | + { |
|
273 | + return $this->reveal()->$name; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Tries to set property value to double. |
|
278 | + * |
|
279 | + * @param string $name |
|
280 | + * @param mixed $value |
|
281 | + */ |
|
282 | + public function __set($name, $value) |
|
283 | + { |
|
284 | + $this->reveal()->$name = $this->revealer->reveal($value); |
|
285 | + } |
|
286 | 286 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | { |
114 | 114 | $double = $this->lazyDouble->getInstance(); |
115 | 115 | |
116 | - if (null === $double || !$double instanceof ProphecySubjectInterface) { |
|
116 | + if (null === $double || ! $double instanceof ProphecySubjectInterface) { |
|
117 | 117 | throw new ObjectProphecyException( |
118 | 118 | "Generated double must implement ProphecySubjectInterface, but it does not.\n". |
119 | 119 | 'It seems you have wrongly configured doubler without required ClassPatch.', |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | |
149 | 149 | $methodName = strtolower($methodProphecy->getMethodName()); |
150 | 150 | |
151 | - if (!isset($this->methodProphecies[$methodName])) { |
|
151 | + if ( ! isset($this->methodProphecies[$methodName])) { |
|
152 | 152 | $this->methodProphecies[$methodName] = array(); |
153 | 153 | } |
154 | 154 | |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | |
171 | 171 | $methodName = strtolower($methodName); |
172 | 172 | |
173 | - if (!isset($this->methodProphecies[$methodName])) { |
|
173 | + if ( ! isset($this->methodProphecies[$methodName])) { |
|
174 | 174 | return array(); |
175 | 175 | } |
176 | 176 |