Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Main matcher which defines a full expectation using method, parameter and Chris@0: * invocation matchers. Chris@0: * This matcher encapsulates all the other matchers and allows the builder to Chris@0: * set the specific matchers when the appropriate methods are called (once(), Chris@0: * where() etc.). Chris@0: * Chris@0: * All properties are public so that they can easily be accessed by the builder. Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation Chris@0: { Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Matcher_Invocation Chris@0: */ Chris@0: public $invocationMatcher; Chris@0: Chris@0: /** Chris@0: * @var mixed Chris@0: */ Chris@0: public $afterMatchBuilderId = null; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: public $afterMatchBuilderIsInvoked = false; Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Matcher_MethodName Chris@0: */ Chris@0: public $methodNameMatcher = null; Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Matcher_Parameters Chris@0: */ Chris@0: public $parametersMatcher = null; Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Stub Chris@0: */ Chris@0: public $stub = null; Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher Chris@0: */ Chris@0: public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher) Chris@0: { Chris@0: $this->invocationMatcher = $invocationMatcher; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function toString() Chris@0: { Chris@0: $list = array(); Chris@0: Chris@0: if ($this->invocationMatcher !== null) { Chris@0: $list[] = $this->invocationMatcher->toString(); Chris@0: } Chris@0: Chris@0: if ($this->methodNameMatcher !== null) { Chris@0: $list[] = 'where ' . $this->methodNameMatcher->toString(); Chris@0: } Chris@0: Chris@0: if ($this->parametersMatcher !== null) { Chris@0: $list[] = 'and ' . $this->parametersMatcher->toString(); Chris@0: } Chris@0: Chris@0: if ($this->afterMatchBuilderId !== null) { Chris@0: $list[] = 'after ' . $this->afterMatchBuilderId; Chris@0: } Chris@0: Chris@0: if ($this->stub !== null) { Chris@0: $list[] = 'will ' . $this->stub->toString(); Chris@0: } Chris@0: Chris@0: return implode(' ', $list); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @return mixed Chris@0: */ Chris@0: public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: if ($this->invocationMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'No invocation matcher is set' Chris@0: ); Chris@0: } Chris@0: Chris@0: if ($this->methodNameMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception('No method matcher is set'); Chris@0: } Chris@0: Chris@0: if ($this->afterMatchBuilderId !== null) { Chris@0: $builder = $invocation->object Chris@0: ->__phpunit_getInvocationMocker() Chris@0: ->lookupId($this->afterMatchBuilderId); Chris@0: Chris@0: if (!$builder) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: sprintf( Chris@0: 'No builder found for match builder identification <%s>', Chris@0: $this->afterMatchBuilderId Chris@0: ) Chris@0: ); Chris@0: } Chris@0: Chris@0: $matcher = $builder->getMatcher(); Chris@0: Chris@0: if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) { Chris@0: $this->afterMatchBuilderIsInvoked = true; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->invocationMatcher->invoked($invocation); Chris@0: Chris@0: try { Chris@0: if ($this->parametersMatcher !== null && Chris@0: !$this->parametersMatcher->matches($invocation)) { Chris@0: $this->parametersMatcher->verify(); Chris@0: } Chris@0: } catch (PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf( Chris@0: "Expectation failed for %s when %s\n%s", Chris@0: $this->methodNameMatcher->toString(), Chris@0: $this->invocationMatcher->toString(), Chris@0: $e->getMessage() Chris@0: ), Chris@0: $e->getComparisonFailure() Chris@0: ); Chris@0: } Chris@0: Chris@0: if ($this->stub) { Chris@0: return $this->stub->invoke($invocation); Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @return bool Chris@0: */ Chris@0: public function matches(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: if ($this->afterMatchBuilderId !== null) { Chris@0: $builder = $invocation->object Chris@0: ->__phpunit_getInvocationMocker() Chris@0: ->lookupId($this->afterMatchBuilderId); Chris@0: Chris@0: if (!$builder) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: sprintf( Chris@0: 'No builder found for match builder identification <%s>', Chris@0: $this->afterMatchBuilderId Chris@0: ) Chris@0: ); Chris@0: } Chris@0: Chris@0: $matcher = $builder->getMatcher(); Chris@0: Chris@0: if (!$matcher) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if (!$matcher->invocationMatcher->hasBeenInvoked()) { Chris@0: return false; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($this->invocationMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'No invocation matcher is set' Chris@0: ); Chris@0: } Chris@0: Chris@0: if ($this->methodNameMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception('No method matcher is set'); Chris@0: } Chris@0: Chris@0: if (!$this->invocationMatcher->matches($invocation)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: try { Chris@0: if (!$this->methodNameMatcher->matches($invocation)) { Chris@0: return false; Chris@0: } Chris@0: } catch (PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf( Chris@0: "Expectation failed for %s when %s\n%s", Chris@0: $this->methodNameMatcher->toString(), Chris@0: $this->invocationMatcher->toString(), Chris@0: $e->getMessage() Chris@0: ), Chris@0: $e->getComparisonFailure() Chris@0: ); Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @throws PHPUnit_Framework_Exception Chris@0: * @throws PHPUnit_Framework_ExpectationFailedException Chris@0: */ Chris@0: public function verify() Chris@0: { Chris@0: if ($this->invocationMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'No invocation matcher is set' Chris@0: ); Chris@0: } Chris@0: Chris@0: if ($this->methodNameMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception('No method matcher is set'); Chris@0: } Chris@0: Chris@0: try { Chris@0: $this->invocationMatcher->verify(); Chris@0: Chris@0: if ($this->parametersMatcher === null) { Chris@0: $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters; Chris@0: } Chris@0: Chris@0: $invocationIsAny = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount'; Chris@0: $invocationIsNever = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_InvokedCount' && $this->invocationMatcher->isNever(); Chris@0: if (!$invocationIsAny && !$invocationIsNever) { Chris@0: $this->parametersMatcher->verify(); Chris@0: } Chris@0: } catch (PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf( Chris@0: "Expectation failed for %s when %s.\n%s", Chris@0: $this->methodNameMatcher->toString(), Chris@0: $this->invocationMatcher->toString(), Chris@0: PHPUnit_Framework_TestFailure::exceptionToString($e) Chris@0: ) Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @since Method available since Release 1.2.4 Chris@0: */ Chris@0: public function hasMatchers() Chris@0: { Chris@0: if ($this->invocationMatcher !== null && Chris@0: !$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: }