5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Jan Borsodi <jb@ez.no>
40 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 * @link http://www.phpunit.de/
45 * @since File available since Release 3.0.0
48 require_once 'PHPUnit/Framework.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Framework/MockObject/Matcher/Invocation.php';
51 require_once 'PHPUnit/Framework/MockObject/Invocation.php';
53 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
56 * Main matcher which defines a full expectation using method, parameter and invocation matchers.
58 * This matcher encapsulates all the other matchers and allows the builder to set
59 * the specific matchers when the appropriate methods are called (once(), where()
62 * All properties are public so that they can easily be accessed by the builder.
66 * @author Jan Borsodi <jb@ez.no>
67 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
68 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
69 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
70 * @version Release: 3.3.17
71 * @link http://www.phpunit.de/
72 * @since Class available since Release 3.0.0
74 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
76 public $invocationMatcher;
78 public $afterMatchBuilderId = NULL;
80 public $afterMatchBuilderIsInvoked = FALSE;
82 public $methodNameMatcher = NULL;
84 public $parametersMatcher = NULL;
88 public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
90 $this->invocationMatcher = $invocationMatcher;
93 public function toString()
97 if ($this->invocationMatcher !== NULL) {
98 $list[] = $this->invocationMatcher->toString();
101 if ($this->methodNameMatcher !== NULL) {
102 $list[] = 'where ' . $this->methodNameMatcher->toString();
105 if ($this->parametersMatcher !== NULL) {
106 $list[] = 'and ' . $this->parametersMatcher->toString();
109 if ($this->afterMatchBuilderId !== NULL) {
110 $list[] = 'after ' . $this->afterMatchBuilderId;
113 if ($this->stub !== NULL) {
114 $list[] = 'will ' . $this->stub->toString();
117 return join(' ', $list);
120 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
122 if ($this->invocationMatcher === NULL) {
123 throw new RuntimeException('No invocation matcher is set');
126 if ($this->methodNameMatcher === NULL) {
127 throw new RuntimeException('No method matcher is set');
130 if ($this->afterMatchBuilderId !== NULL) {
131 $builder = $invocation->object->__phpunit_getInvocationMocker()->lookupId($this->afterMatchBuilderId);
134 throw new RuntimeException(
136 'No builder found for match builder identification <%s>',
138 $this->afterMatchBuilderId
143 $matcher = $builder->getMatcher();
145 if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
146 $this->afterMatchBuilderIsInvoked = TRUE;
150 $this->invocationMatcher->invoked($invocation);
153 if ( $this->parametersMatcher !== NULL &&
154 !$this->parametersMatcher->matches($invocation)) {
155 $this->parametersMatcher->verify();
159 catch (PHPUnit_Framework_ExpectationFailedException $e) {
160 throw new PHPUnit_Framework_ExpectationFailedException(
162 "Expectation failed for %s when %s\n%s",
164 $this->methodNameMatcher->toString(),
165 $this->invocationMatcher->toString(),
168 $e->getComparisonFailure()
173 return $this->stub->invoke($invocation);
179 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
181 if ($this->afterMatchBuilderId !== NULL) {
182 $builder = $invocation->object->__phpunit_getInvocationMocker()->lookupId($this->afterMatchBuilderId);
185 throw new RuntimeException(
187 'No builder found for match builder identification <%s>',
189 $this->afterMatchBuilderId
194 $matcher = $builder->getMatcher();
200 if (!$matcher->invocationMatcher->hasBeenInvoked()) {
205 if ($this->invocationMatcher === NULL) {
206 throw new RuntimeException('No invocation matcher is set');
209 if ($this->methodNameMatcher === NULL) {
210 throw new RuntimeException('No method matcher is set');
213 if (!$this->invocationMatcher->matches($invocation)) {
218 if (!$this->methodNameMatcher->matches($invocation)) {
223 catch (PHPUnit_Framework_ExpectationFailedException $e) {
224 throw new PHPUnit_Framework_ExpectationFailedException(
226 "Expectation failed for %s when %s\n%s",
228 $this->methodNameMatcher->toString(),
229 $this->invocationMatcher->toString(),
232 $e->getComparisonFailure()
239 public function verify()
241 if ($this->invocationMatcher === NULL) {
242 throw new RuntimeException('No invocation matcher is set');
245 if ($this->methodNameMatcher === NULL) {
246 throw new RuntimeException('No method matcher is set');
250 $this->invocationMatcher->verify();
252 if ($this->parametersMatcher !== NULL) {
253 $this->parametersMatcher->verify();
257 catch (PHPUnit_Framework_ExpectationFailedException $e) {
258 throw new PHPUnit_Framework_ExpectationFailedException(
260 "Expectation failed for %s when %s.\n%s",
262 $this->methodNameMatcher->toString(),
263 $this->invocationMatcher->toString(),
271 require_once 'PHPUnit/Framework/MockObject/Matcher/AnyInvokedCount.php';
272 require_once 'PHPUnit/Framework/MockObject/Matcher/AnyParameters.php';
273 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtIndex.php';
274 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtLeastOnce.php';
275 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedCount.php';
276 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedRecorder.php';
277 require_once 'PHPUnit/Framework/MockObject/Matcher/MethodName.php';
278 require_once 'PHPUnit/Framework/MockObject/Matcher/Parameters.php';
279 require_once 'PHPUnit/Framework/MockObject/Matcher/StatelessInvocation.php';