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 * Main matcher which defines a full expectation using method, parameter and
13 * invocation matchers.
14 * This matcher encapsulates all the other matchers and allows the builder to
15 * set the specific matchers when the appropriate methods are called (once(),
18 * All properties are public so that they can easily be accessed by the builder.
20 * @since Class available since Release 1.0.0
22 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
25 * @var PHPUnit_Framework_MockObject_Matcher_Invocation
27 public $invocationMatcher;
32 public $afterMatchBuilderId = null;
37 public $afterMatchBuilderIsInvoked = false;
40 * @var PHPUnit_Framework_MockObject_Matcher_MethodName
42 public $methodNameMatcher = null;
45 * @var PHPUnit_Framework_MockObject_Matcher_Parameters
47 public $parametersMatcher = null;
50 * @var PHPUnit_Framework_MockObject_Stub
55 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
57 public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
59 $this->invocationMatcher = $invocationMatcher;
65 public function toString()
69 if ($this->invocationMatcher !== null) {
70 $list[] = $this->invocationMatcher->toString();
73 if ($this->methodNameMatcher !== null) {
74 $list[] = 'where ' . $this->methodNameMatcher->toString();
77 if ($this->parametersMatcher !== null) {
78 $list[] = 'and ' . $this->parametersMatcher->toString();
81 if ($this->afterMatchBuilderId !== null) {
82 $list[] = 'after ' . $this->afterMatchBuilderId;
85 if ($this->stub !== null) {
86 $list[] = 'will ' . $this->stub->toString();
89 return implode(' ', $list);
93 * @param PHPUnit_Framework_MockObject_Invocation $invocation
96 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
98 if ($this->invocationMatcher === null) {
99 throw new PHPUnit_Framework_Exception(
100 'No invocation matcher is set'
104 if ($this->methodNameMatcher === null) {
105 throw new PHPUnit_Framework_Exception('No method matcher is set');
108 if ($this->afterMatchBuilderId !== null) {
109 $builder = $invocation->object
110 ->__phpunit_getInvocationMocker()
111 ->lookupId($this->afterMatchBuilderId);
114 throw new PHPUnit_Framework_Exception(
116 'No builder found for match builder identification <%s>',
117 $this->afterMatchBuilderId
122 $matcher = $builder->getMatcher();
124 if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
125 $this->afterMatchBuilderIsInvoked = true;
129 $this->invocationMatcher->invoked($invocation);
132 if ($this->parametersMatcher !== null &&
133 !$this->parametersMatcher->matches($invocation)) {
134 $this->parametersMatcher->verify();
136 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
137 throw new PHPUnit_Framework_ExpectationFailedException(
139 "Expectation failed for %s when %s\n%s",
140 $this->methodNameMatcher->toString(),
141 $this->invocationMatcher->toString(),
144 $e->getComparisonFailure()
149 return $this->stub->invoke($invocation);
156 * @param PHPUnit_Framework_MockObject_Invocation $invocation
159 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
161 if ($this->afterMatchBuilderId !== null) {
162 $builder = $invocation->object
163 ->__phpunit_getInvocationMocker()
164 ->lookupId($this->afterMatchBuilderId);
167 throw new PHPUnit_Framework_Exception(
169 'No builder found for match builder identification <%s>',
170 $this->afterMatchBuilderId
175 $matcher = $builder->getMatcher();
181 if (!$matcher->invocationMatcher->hasBeenInvoked()) {
186 if ($this->invocationMatcher === null) {
187 throw new PHPUnit_Framework_Exception(
188 'No invocation matcher is set'
192 if ($this->methodNameMatcher === null) {
193 throw new PHPUnit_Framework_Exception('No method matcher is set');
196 if (!$this->invocationMatcher->matches($invocation)) {
201 if (!$this->methodNameMatcher->matches($invocation)) {
204 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
205 throw new PHPUnit_Framework_ExpectationFailedException(
207 "Expectation failed for %s when %s\n%s",
208 $this->methodNameMatcher->toString(),
209 $this->invocationMatcher->toString(),
212 $e->getComparisonFailure()
220 * @throws PHPUnit_Framework_Exception
221 * @throws PHPUnit_Framework_ExpectationFailedException
223 public function verify()
225 if ($this->invocationMatcher === null) {
226 throw new PHPUnit_Framework_Exception(
227 'No invocation matcher is set'
231 if ($this->methodNameMatcher === null) {
232 throw new PHPUnit_Framework_Exception('No method matcher is set');
236 $this->invocationMatcher->verify();
238 if ($this->parametersMatcher === null) {
239 $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
242 $invocationIsAny = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount';
243 $invocationIsNever = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_InvokedCount' && $this->invocationMatcher->isNever();
244 if (!$invocationIsAny && !$invocationIsNever) {
245 $this->parametersMatcher->verify();
247 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
248 throw new PHPUnit_Framework_ExpectationFailedException(
250 "Expectation failed for %s when %s.\n%s",
251 $this->methodNameMatcher->toString(),
252 $this->invocationMatcher->toString(),
253 PHPUnit_Framework_TestFailure::exceptionToString($e)
260 * @since Method available since Release 1.2.4
262 public function hasMatchers()
264 if ($this->invocationMatcher !== null &&
265 !$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) {