comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/InvocationMocker.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Mocker for invocations which are sent from
13 * PHPUnit_Framework_MockObject_MockObject objects.
14 *
15 * Keeps track of all expectations and stubs as well as registering
16 * identifications for builders.
17 *
18 * @since Class available since Release 1.0.0
19 */
20 class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
21 {
22 /**
23 * @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
24 */
25 protected $matchers = array();
26
27 /**
28 * @var PHPUnit_Framework_MockObject_Builder_Match[]
29 */
30 protected $builderMap = array();
31
32 /**
33 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
34 */
35 public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
36 {
37 $this->matchers[] = $matcher;
38 }
39
40 /**
41 * @since Method available since Release 1.1.0
42 */
43 public function hasMatchers()
44 {
45 foreach ($this->matchers as $matcher) {
46 if ($matcher->hasMatchers()) {
47 return true;
48 }
49 }
50
51 return false;
52 }
53
54 /**
55 * @param mixed $id
56 * @return bool|null
57 */
58 public function lookupId($id)
59 {
60 if (isset($this->builderMap[$id])) {
61 return $this->builderMap[$id];
62 }
63
64 return;
65 }
66
67 /**
68 * @param mixed $id
69 * @param PHPUnit_Framework_MockObject_Builder_Match $builder
70 * @throws PHPUnit_Framework_Exception
71 */
72 public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
73 {
74 if (isset($this->builderMap[$id])) {
75 throw new PHPUnit_Framework_Exception(
76 'Match builder with id <' . $id . '> is already registered.'
77 );
78 }
79
80 $this->builderMap[$id] = $builder;
81 }
82
83 /**
84 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
85 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
86 */
87 public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
88 {
89 return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
90 $this,
91 $matcher
92 );
93 }
94
95 /**
96 * @param PHPUnit_Framework_MockObject_Invocation $invocation
97 * @return mixed
98 */
99 public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
100 {
101 $exception = null;
102 $hasReturnValue = false;
103
104 if (strtolower($invocation->methodName) == '__tostring') {
105 $returnValue = '';
106 } else {
107 $returnValue = null;
108 }
109
110 foreach ($this->matchers as $match) {
111 try {
112 if ($match->matches($invocation)) {
113 $value = $match->invoked($invocation);
114
115 if (!$hasReturnValue) {
116 $returnValue = $value;
117 $hasReturnValue = true;
118 }
119 }
120 } catch (Exception $e) {
121 $exception = $e;
122 }
123 }
124
125 if ($exception !== null) {
126 throw $exception;
127 }
128
129 return $returnValue;
130 }
131
132 /**
133 * @param PHPUnit_Framework_MockObject_Invocation $invocation
134 * @return bool
135 */
136 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
137 {
138 foreach ($this->matchers as $matcher) {
139 if (!$matcher->matches($invocation)) {
140 return false;
141 }
142 }
143
144 return true;
145 }
146
147 /**
148 * @return bool
149 */
150 public function verify()
151 {
152 foreach ($this->matchers as $matcher) {
153 $matcher->verify();
154 }
155 }
156 }