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