3 * This file is part of the PHPUnit_MockObject package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Builder for mocked or stubbed invocations.
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.
19 * @since Class available since Release 1.0.0
21 class PHPUnit_Framework_MockObject_Builder_InvocationMocker implements PHPUnit_Framework_MockObject_Builder_MethodNameMatch
24 * @var PHPUnit_Framework_MockObject_Stub_MatcherCollection
26 protected $collection;
29 * @var PHPUnit_Framework_MockObject_Matcher
34 * @param PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection
35 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
37 public function __construct(PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection, PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
39 $this->collection = $collection;
40 $this->matcher = new PHPUnit_Framework_MockObject_Matcher(
44 $this->collection->addMatcher($this->matcher);
48 * @return PHPUnit_Framework_MockObject_Matcher
50 public function getMatcher()
52 return $this->matcher;
57 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
59 public function id($id)
61 $this->collection->registerId($id, $this);
67 * @param PHPUnit_Framework_MockObject_Stub $stub
68 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
70 public function will(PHPUnit_Framework_MockObject_Stub $stub)
72 $this->matcher->stub = $stub;
79 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
81 public function willReturn($value)
83 $stub = new PHPUnit_Framework_MockObject_Stub_Return(
87 return $this->will($stub);
91 * @param array $valueMap
92 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
94 public function willReturnMap(array $valueMap)
96 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap(
100 return $this->will($stub);
104 * @param mixed $argumentIndex
105 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
107 public function willReturnArgument($argumentIndex)
109 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
113 return $this->will($stub);
117 * @param callable $callback
118 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
120 public function willReturnCallback($callback)
122 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
126 return $this->will($stub);
130 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
132 public function willReturnSelf()
134 $stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf();
136 return $this->will($stub);
140 * @param mixed $value, ...
141 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
143 public function willReturnOnConsecutiveCalls()
145 $args = func_get_args();
147 $stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
149 return $this->will($stub);
153 * @param Exception $exception
154 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
156 public function willThrowException(Exception $exception)
158 $stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception);
160 return $this->will($stub);
165 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
167 public function after($id)
169 $this->matcher->afterMatchBuilderId = $id;
175 * Validate that a parameters matcher can be defined, throw exceptions otherwise.
177 * @throws PHPUnit_Framework_Exception
179 private function canDefineParameters()
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'
188 if ($this->matcher->parametersMatcher !== null) {
189 throw new PHPUnit_Framework_Exception(
190 'Parameter matcher is already defined, cannot redefine'
196 * @param mixed $argument, ...
197 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
199 public function with()
201 $args = func_get_args();
203 $this->canDefineParameters();
205 $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($args);
211 * @param mixed ...$argument
212 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
214 public function withConsecutive()
217 $args = func_get_args();
219 $this->canDefineParameters();
221 $this->matcher->parametersMatcher =
222 new PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters($args);
228 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
230 public function withAnyParameters()
232 $this->canDefineParameters();
234 $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
240 * @param PHPUnit_Framework_Constraint|string $constraint
241 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
243 public function method($constraint)
245 if ($this->matcher->methodNameMatcher !== null) {
246 throw new PHPUnit_Framework_Exception(
247 'Method name matcher is already defined, cannot redefine'
251 $this->matcher->methodNameMatcher = new PHPUnit_Framework_MockObject_Matcher_MethodName($constraint);