comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Builder/InvocationMocker.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Builder for mocked or stubbed invocations.
13 *
14 * Provides methods for building expectations without having to resort to
15 * instantiating the various matchers manually. These methods also form a
16 * more natural way of reading the expectation. This class should be together
17 * with the test case PHPUnit_Framework_MockObject_TestCase.
18 *
19 * @since Class available since Release 1.0.0
20 */
21 class PHPUnit_Framework_MockObject_Builder_InvocationMocker implements PHPUnit_Framework_MockObject_Builder_MethodNameMatch
22 {
23 /**
24 * @var PHPUnit_Framework_MockObject_Stub_MatcherCollection
25 */
26 protected $collection;
27
28 /**
29 * @var PHPUnit_Framework_MockObject_Matcher
30 */
31 protected $matcher;
32
33 /**
34 * @param PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection
35 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
36 */
37 public function __construct(PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection, PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
38 {
39 $this->collection = $collection;
40 $this->matcher = new PHPUnit_Framework_MockObject_Matcher(
41 $invocationMatcher
42 );
43
44 $this->collection->addMatcher($this->matcher);
45 }
46
47 /**
48 * @return PHPUnit_Framework_MockObject_Matcher
49 */
50 public function getMatcher()
51 {
52 return $this->matcher;
53 }
54
55 /**
56 * @param mixed $id
57 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
58 */
59 public function id($id)
60 {
61 $this->collection->registerId($id, $this);
62
63 return $this;
64 }
65
66 /**
67 * @param PHPUnit_Framework_MockObject_Stub $stub
68 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
69 */
70 public function will(PHPUnit_Framework_MockObject_Stub $stub)
71 {
72 $this->matcher->stub = $stub;
73
74 return $this;
75 }
76
77 /**
78 * @param mixed $value
79 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
80 */
81 public function willReturn($value)
82 {
83 $stub = new PHPUnit_Framework_MockObject_Stub_Return(
84 $value
85 );
86
87 return $this->will($stub);
88 }
89
90 /**
91 * @param array $valueMap
92 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
93 */
94 public function willReturnMap(array $valueMap)
95 {
96 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap(
97 $valueMap
98 );
99
100 return $this->will($stub);
101 }
102
103 /**
104 * @param mixed $argumentIndex
105 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
106 */
107 public function willReturnArgument($argumentIndex)
108 {
109 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
110 $argumentIndex
111 );
112
113 return $this->will($stub);
114 }
115
116 /**
117 * @param callable $callback
118 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
119 */
120 public function willReturnCallback($callback)
121 {
122 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
123 $callback
124 );
125
126 return $this->will($stub);
127 }
128
129 /**
130 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
131 */
132 public function willReturnSelf()
133 {
134 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf();
135
136 return $this->will($stub);
137 }
138
139 /**
140 * @param mixed $value, ...
141 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
142 */
143 public function willReturnOnConsecutiveCalls()
144 {
145 $args = func_get_args();
146
147 $stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
148
149 return $this->will($stub);
150 }
151
152 /**
153 * @param Exception $exception
154 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
155 */
156 public function willThrowException(Exception $exception)
157 {
158 $stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception);
159
160 return $this->will($stub);
161 }
162
163 /**
164 * @param mixed $id
165 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
166 */
167 public function after($id)
168 {
169 $this->matcher->afterMatchBuilderId = $id;
170
171 return $this;
172 }
173
174 /**
175 * Validate that a parameters matcher can be defined, throw exceptions otherwise.
176 *
177 * @throws PHPUnit_Framework_Exception
178 */
179 private function canDefineParameters()
180 {
181 if ($this->matcher->methodNameMatcher === null) {
182 throw new PHPUnit_Framework_Exception(
183 'Method name matcher is not defined, cannot define parameter ' .
184 ' matcher without one'
185 );
186 }
187
188 if ($this->matcher->parametersMatcher !== null) {
189 throw new PHPUnit_Framework_Exception(
190 'Parameter matcher is already defined, cannot redefine'
191 );
192 }
193 }
194
195 /**
196 * @param mixed $argument, ...
197 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
198 */
199 public function with()
200 {
201 $args = func_get_args();
202
203 $this->canDefineParameters();
204
205 $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($args);
206
207 return $this;
208 }
209
210 /**
211 * @param mixed ...$argument
212 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
213 */
214 public function withConsecutive()
215 {
216
217 $args = func_get_args();
218
219 $this->canDefineParameters();
220
221 $this->matcher->parametersMatcher =
222 new PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters($args);
223
224 return $this;
225 }
226
227 /**
228 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
229 */
230 public function withAnyParameters()
231 {
232 $this->canDefineParameters();
233
234 $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
235
236 return $this;
237 }
238
239 /**
240 * @param PHPUnit_Framework_Constraint|string $constraint
241 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
242 */
243 public function method($constraint)
244 {
245 if ($this->matcher->methodNameMatcher !== null) {
246 throw new PHPUnit_Framework_Exception(
247 'Method name matcher is already defined, cannot redefine'
248 );
249 }
250
251 $this->matcher->methodNameMatcher = new PHPUnit_Framework_MockObject_Matcher_MethodName($constraint);
252
253 return $this;
254 }
255 }