5 * Copyright (c) 2010-2011, 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.
37 * @package PHPUnit_MockObject
38 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
39 * @copyright 2010-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
41 * @link http://github.com/sebastianbergmann/phpunit-mock-objects
42 * @since File available since Release 1.0.0
46 * Main matcher which defines a full expectation using method, parameter and
47 * invocation matchers.
48 * This matcher encapsulates all the other matchers and allows the builder to
49 * set the specific matchers when the appropriate methods are called (once(),
52 * All properties are public so that they can easily be accessed by the builder.
54 * @package PHPUnit_MockObject
55 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
56 * @copyright 2010-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
57 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
58 * @version Release: 1.0.9
59 * @link http://github.com/sebastianbergmann/phpunit-mock-objects
60 * @since Class available since Release 1.0.0
62 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
65 * @var PHPUnit_Framework_MockObject_Matcher_Invocation
67 public $invocationMatcher;
72 public $afterMatchBuilderId = NULL;
77 public $afterMatchBuilderIsInvoked = FALSE;
80 * @var PHPUnit_Framework_MockObject_Matcher_MethodName
82 public $methodNameMatcher = NULL;
85 * @var PHPUnit_Framework_MockObject_Matcher_Parameters
87 public $parametersMatcher = NULL;
90 * @var PHPUnit_Framework_MockObject_Stub
95 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
97 public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
99 $this->invocationMatcher = $invocationMatcher;
105 public function toString()
109 if ($this->invocationMatcher !== NULL) {
110 $list[] = $this->invocationMatcher->toString();
113 if ($this->methodNameMatcher !== NULL) {
114 $list[] = 'where ' . $this->methodNameMatcher->toString();
117 if ($this->parametersMatcher !== NULL) {
118 $list[] = 'and ' . $this->parametersMatcher->toString();
121 if ($this->afterMatchBuilderId !== NULL) {
122 $list[] = 'after ' . $this->afterMatchBuilderId;
125 if ($this->stub !== NULL) {
126 $list[] = 'will ' . $this->stub->toString();
129 return join(' ', $list);
133 * @param PHPUnit_Framework_MockObject_Invocation $invocation
136 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
138 if ($this->invocationMatcher === NULL) {
139 throw new PHPUnit_Framework_Exception(
140 'No invocation matcher is set'
144 if ($this->methodNameMatcher === NULL) {
145 throw new PHPUnit_Framework_Exception('No method matcher is set');
148 if ($this->afterMatchBuilderId !== NULL) {
149 $builder = $invocation->object
150 ->__phpunit_getInvocationMocker()
151 ->lookupId($this->afterMatchBuilderId);
154 throw new PHPUnit_Framework_Exception(
156 'No builder found for match builder identification <%s>',
158 $this->afterMatchBuilderId
163 $matcher = $builder->getMatcher();
165 if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
166 $this->afterMatchBuilderIsInvoked = TRUE;
170 $this->invocationMatcher->invoked($invocation);
173 if ( $this->parametersMatcher !== NULL &&
174 !$this->parametersMatcher->matches($invocation)) {
175 $this->parametersMatcher->verify();
179 catch (PHPUnit_Framework_ExpectationFailedException $e) {
180 throw new PHPUnit_Framework_ExpectationFailedException(
182 "Expectation failed for %s when %s\n%s",
184 $this->methodNameMatcher->toString(),
185 $this->invocationMatcher->toString(),
188 $e->getComparisonFailure()
193 return $this->stub->invoke($invocation);
200 * @param PHPUnit_Framework_MockObject_Invocation $invocation
203 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
205 if ($this->afterMatchBuilderId !== NULL) {
206 $builder = $invocation->object
207 ->__phpunit_getInvocationMocker()
208 ->lookupId($this->afterMatchBuilderId);
211 throw new PHPUnit_Framework_Exception(
213 'No builder found for match builder identification <%s>',
215 $this->afterMatchBuilderId
220 $matcher = $builder->getMatcher();
226 if (!$matcher->invocationMatcher->hasBeenInvoked()) {
231 if ($this->invocationMatcher === NULL) {
232 throw new PHPUnit_Framework_Exception(
233 'No invocation matcher is set'
237 if ($this->methodNameMatcher === NULL) {
238 throw new PHPUnit_Framework_Exception('No method matcher is set');
241 if (!$this->invocationMatcher->matches($invocation)) {
246 if (!$this->methodNameMatcher->matches($invocation)) {
251 catch (PHPUnit_Framework_ExpectationFailedException $e) {
252 throw new PHPUnit_Framework_ExpectationFailedException(
254 "Expectation failed for %s when %s\n%s",
256 $this->methodNameMatcher->toString(),
257 $this->invocationMatcher->toString(),
260 $e->getComparisonFailure()
268 * @throws PHPUnit_Framework_Exception
269 * @throws PHPUnit_Framework_ExpectationFailedException
271 public function verify()
273 if ($this->invocationMatcher === NULL) {
274 throw new PHPUnit_Framework_Exception(
275 'No invocation matcher is set'
279 if ($this->methodNameMatcher === NULL) {
280 throw new PHPUnit_Framework_Exception('No method matcher is set');
284 $this->invocationMatcher->verify();
286 if ($this->parametersMatcher !== NULL) {
287 $this->parametersMatcher->verify();
291 catch (PHPUnit_Framework_ExpectationFailedException $e) {
292 throw new PHPUnit_Framework_ExpectationFailedException(
294 "Expectation failed for %s when %s.\n%s",
296 $this->methodNameMatcher->toString(),
297 $this->invocationMatcher->toString(),