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 * Mocker for invocations which are sent from
13 * PHPUnit_Framework_MockObject_MockObject objects.
15 * Keeps track of all expectations and stubs as well as registering
16 * identifications for builders.
18 * @since Class available since Release 1.0.0
20 class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
23 * @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
25 protected $matchers = array();
28 * @var PHPUnit_Framework_MockObject_Builder_Match[]
30 protected $builderMap = array();
33 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
35 public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
37 $this->matchers[] = $matcher;
41 * @since Method available since Release 1.1.0
43 public function hasMatchers()
45 foreach ($this->matchers as $matcher) {
46 if ($matcher->hasMatchers()) {
58 public function lookupId($id)
60 if (isset($this->builderMap[$id])) {
61 return $this->builderMap[$id];
69 * @param PHPUnit_Framework_MockObject_Builder_Match $builder
70 * @throws PHPUnit_Framework_Exception
72 public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
74 if (isset($this->builderMap[$id])) {
75 throw new PHPUnit_Framework_Exception(
76 'Match builder with id <' . $id . '> is already registered.'
80 $this->builderMap[$id] = $builder;
84 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
85 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
87 public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
89 return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
96 * @param PHPUnit_Framework_MockObject_Invocation $invocation
99 public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
102 $hasReturnValue = false;
104 if (strtolower($invocation->methodName) == '__tostring') {
110 foreach ($this->matchers as $match) {
112 if ($match->matches($invocation)) {
113 $value = $match->invoked($invocation);
115 if (!$hasReturnValue) {
116 $returnValue = $value;
117 $hasReturnValue = true;
120 } catch (Exception $e) {
125 if ($exception !== null) {
133 * @param PHPUnit_Framework_MockObject_Invocation $invocation
136 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
138 foreach ($this->matchers as $matcher) {
139 if (!$matcher->matches($invocation)) {
150 public function verify()
152 foreach ($this->matchers as $matcher) {