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: * Mocker for invocations which are sent from Chris@0: * PHPUnit_Framework_MockObject_MockObject objects. Chris@0: * Chris@0: * Keeps track of all expectations and stubs as well as registering Chris@0: * identifications for builders. Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace Chris@0: { Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Matcher_Invocation[] Chris@0: */ Chris@0: protected $matchers = array(); Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Builder_Match[] Chris@0: */ Chris@0: protected $builderMap = array(); Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher Chris@0: */ Chris@0: public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher) Chris@0: { Chris@0: $this->matchers[] = $matcher; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @since Method available since Release 1.1.0 Chris@0: */ Chris@0: public function hasMatchers() Chris@0: { Chris@0: foreach ($this->matchers as $matcher) { Chris@0: if ($matcher->hasMatchers()) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $id Chris@0: * @return bool|null Chris@0: */ Chris@0: public function lookupId($id) Chris@0: { Chris@0: if (isset($this->builderMap[$id])) { Chris@0: return $this->builderMap[$id]; Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $id Chris@0: * @param PHPUnit_Framework_MockObject_Builder_Match $builder Chris@0: * @throws PHPUnit_Framework_Exception Chris@0: */ Chris@0: public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder) Chris@0: { Chris@0: if (isset($this->builderMap[$id])) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'Match builder with id <' . $id . '> is already registered.' Chris@0: ); Chris@0: } Chris@0: Chris@0: $this->builderMap[$id] = $builder; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher) Chris@0: { Chris@0: return new PHPUnit_Framework_MockObject_Builder_InvocationMocker( Chris@0: $this, Chris@0: $matcher Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @return mixed Chris@0: */ Chris@0: public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: $exception = null; Chris@0: $hasReturnValue = false; Chris@0: Chris@0: if (strtolower($invocation->methodName) == '__tostring') { Chris@0: $returnValue = ''; Chris@0: } else { Chris@0: $returnValue = null; Chris@0: } Chris@0: Chris@0: foreach ($this->matchers as $match) { Chris@0: try { Chris@0: if ($match->matches($invocation)) { Chris@0: $value = $match->invoked($invocation); Chris@0: Chris@0: if (!$hasReturnValue) { Chris@0: $returnValue = $value; Chris@0: $hasReturnValue = true; Chris@0: } Chris@0: } Chris@0: } catch (Exception $e) { Chris@0: $exception = $e; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($exception !== null) { Chris@0: throw $exception; Chris@0: } Chris@0: Chris@0: return $returnValue; 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: foreach ($this->matchers as $matcher) { Chris@0: if (!$matcher->matches($invocation)) { Chris@0: return false; Chris@0: } Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return bool Chris@0: */ Chris@0: public function verify() Chris@0: { Chris@0: foreach ($this->matchers as $matcher) { Chris@0: $matcher->verify(); Chris@0: } Chris@0: } Chris@0: }