Chris@14
|
1 <?php
|
Chris@14
|
2 /*
|
Chris@14
|
3 * This file is part of the phpunit-mock-objects package.
|
Chris@14
|
4 *
|
Chris@14
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@14
|
6 *
|
Chris@14
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
8 * file that was distributed with this source code.
|
Chris@14
|
9 */
|
Chris@14
|
10 namespace PHPUnit\Framework\MockObject;
|
Chris@14
|
11
|
Chris@14
|
12 use Exception;
|
Chris@14
|
13 use PHPUnit\Framework\MockObject\Builder\InvocationMocker as BuilderInvocationMocker;
|
Chris@14
|
14 use PHPUnit\Framework\MockObject\Builder\Match;
|
Chris@14
|
15 use PHPUnit\Framework\MockObject\Builder\NamespaceMatch;
|
Chris@14
|
16 use PHPUnit\Framework\MockObject\Matcher\Invocation as MatcherInvocation;
|
Chris@14
|
17 use PHPUnit\Framework\MockObject\Stub\MatcherCollection;
|
Chris@14
|
18
|
Chris@14
|
19 /**
|
Chris@14
|
20 * Mocker for invocations which are sent from
|
Chris@14
|
21 * MockObject objects.
|
Chris@14
|
22 *
|
Chris@14
|
23 * Keeps track of all expectations and stubs as well as registering
|
Chris@14
|
24 * identifications for builders.
|
Chris@14
|
25 */
|
Chris@14
|
26 class InvocationMocker implements MatcherCollection, Invokable, NamespaceMatch
|
Chris@14
|
27 {
|
Chris@14
|
28 /**
|
Chris@14
|
29 * @var MatcherInvocation[]
|
Chris@14
|
30 */
|
Chris@14
|
31 private $matchers = [];
|
Chris@14
|
32
|
Chris@14
|
33 /**
|
Chris@14
|
34 * @var Match[]
|
Chris@14
|
35 */
|
Chris@14
|
36 private $builderMap = [];
|
Chris@14
|
37
|
Chris@14
|
38 /**
|
Chris@14
|
39 * @var string[]
|
Chris@14
|
40 */
|
Chris@14
|
41 private $configurableMethods = [];
|
Chris@14
|
42
|
Chris@14
|
43 /**
|
Chris@14
|
44 * @param array $configurableMethods
|
Chris@14
|
45 */
|
Chris@14
|
46 public function __construct(array $configurableMethods)
|
Chris@14
|
47 {
|
Chris@14
|
48 $this->configurableMethods = $configurableMethods;
|
Chris@14
|
49 }
|
Chris@14
|
50
|
Chris@14
|
51 /**
|
Chris@14
|
52 * @param MatcherInvocation $matcher
|
Chris@14
|
53 */
|
Chris@14
|
54 public function addMatcher(MatcherInvocation $matcher)
|
Chris@14
|
55 {
|
Chris@14
|
56 $this->matchers[] = $matcher;
|
Chris@14
|
57 }
|
Chris@14
|
58
|
Chris@14
|
59 public function hasMatchers()
|
Chris@14
|
60 {
|
Chris@14
|
61 foreach ($this->matchers as $matcher) {
|
Chris@14
|
62 if ($matcher->hasMatchers()) {
|
Chris@14
|
63 return true;
|
Chris@14
|
64 }
|
Chris@14
|
65 }
|
Chris@14
|
66
|
Chris@14
|
67 return false;
|
Chris@14
|
68 }
|
Chris@14
|
69
|
Chris@14
|
70 /**
|
Chris@14
|
71 * @param mixed $id
|
Chris@14
|
72 *
|
Chris@14
|
73 * @return bool|null
|
Chris@14
|
74 */
|
Chris@14
|
75 public function lookupId($id)
|
Chris@14
|
76 {
|
Chris@14
|
77 if (isset($this->builderMap[$id])) {
|
Chris@14
|
78 return $this->builderMap[$id];
|
Chris@14
|
79 }
|
Chris@14
|
80
|
Chris@14
|
81 return;
|
Chris@14
|
82 }
|
Chris@14
|
83
|
Chris@14
|
84 /**
|
Chris@14
|
85 * @param mixed $id
|
Chris@14
|
86 * @param Match $builder
|
Chris@14
|
87 *
|
Chris@14
|
88 * @throws RuntimeException
|
Chris@14
|
89 */
|
Chris@14
|
90 public function registerId($id, Match $builder)
|
Chris@14
|
91 {
|
Chris@14
|
92 if (isset($this->builderMap[$id])) {
|
Chris@14
|
93 throw new RuntimeException(
|
Chris@14
|
94 'Match builder with id <' . $id . '> is already registered.'
|
Chris@14
|
95 );
|
Chris@14
|
96 }
|
Chris@14
|
97
|
Chris@14
|
98 $this->builderMap[$id] = $builder;
|
Chris@14
|
99 }
|
Chris@14
|
100
|
Chris@14
|
101 /**
|
Chris@14
|
102 * @param MatcherInvocation $matcher
|
Chris@14
|
103 *
|
Chris@14
|
104 * @return BuilderInvocationMocker
|
Chris@14
|
105 */
|
Chris@14
|
106 public function expects(MatcherInvocation $matcher)
|
Chris@14
|
107 {
|
Chris@14
|
108 return new BuilderInvocationMocker(
|
Chris@14
|
109 $this,
|
Chris@14
|
110 $matcher,
|
Chris@14
|
111 $this->configurableMethods
|
Chris@14
|
112 );
|
Chris@14
|
113 }
|
Chris@14
|
114
|
Chris@14
|
115 /**
|
Chris@14
|
116 * @param Invocation $invocation
|
Chris@14
|
117 *
|
Chris@14
|
118 * @return mixed
|
Chris@14
|
119 *
|
Chris@14
|
120 * @throws Exception
|
Chris@14
|
121 */
|
Chris@14
|
122 public function invoke(Invocation $invocation)
|
Chris@14
|
123 {
|
Chris@14
|
124 $exception = null;
|
Chris@14
|
125 $hasReturnValue = false;
|
Chris@14
|
126 $returnValue = null;
|
Chris@14
|
127
|
Chris@14
|
128 foreach ($this->matchers as $match) {
|
Chris@14
|
129 try {
|
Chris@14
|
130 if ($match->matches($invocation)) {
|
Chris@14
|
131 $value = $match->invoked($invocation);
|
Chris@14
|
132
|
Chris@14
|
133 if (!$hasReturnValue) {
|
Chris@14
|
134 $returnValue = $value;
|
Chris@14
|
135 $hasReturnValue = true;
|
Chris@14
|
136 }
|
Chris@14
|
137 }
|
Chris@14
|
138 } catch (Exception $e) {
|
Chris@14
|
139 $exception = $e;
|
Chris@14
|
140 }
|
Chris@14
|
141 }
|
Chris@14
|
142
|
Chris@14
|
143 if ($exception !== null) {
|
Chris@14
|
144 throw $exception;
|
Chris@14
|
145 }
|
Chris@14
|
146
|
Chris@14
|
147 if ($hasReturnValue) {
|
Chris@14
|
148 return $returnValue;
|
Chris@14
|
149 }
|
Chris@14
|
150
|
Chris@14
|
151 if (\strtolower($invocation->getMethodName()) === '__tostring') {
|
Chris@14
|
152 return '';
|
Chris@14
|
153 }
|
Chris@14
|
154
|
Chris@14
|
155 return $invocation->generateReturnValue();
|
Chris@14
|
156 }
|
Chris@14
|
157
|
Chris@14
|
158 /**
|
Chris@14
|
159 * @param Invocation $invocation
|
Chris@14
|
160 *
|
Chris@14
|
161 * @return bool
|
Chris@14
|
162 */
|
Chris@14
|
163 public function matches(Invocation $invocation)
|
Chris@14
|
164 {
|
Chris@14
|
165 foreach ($this->matchers as $matcher) {
|
Chris@14
|
166 if (!$matcher->matches($invocation)) {
|
Chris@14
|
167 return false;
|
Chris@14
|
168 }
|
Chris@14
|
169 }
|
Chris@14
|
170
|
Chris@14
|
171 return true;
|
Chris@14
|
172 }
|
Chris@14
|
173
|
Chris@14
|
174 /**
|
Chris@14
|
175 * @return bool
|
Chris@14
|
176 *
|
Chris@14
|
177 * @throws \PHPUnit\Framework\ExpectationFailedException
|
Chris@14
|
178 */
|
Chris@14
|
179 public function verify()
|
Chris@14
|
180 {
|
Chris@14
|
181 foreach ($this->matchers as $matcher) {
|
Chris@14
|
182 $matcher->verify();
|
Chris@14
|
183 }
|
Chris@14
|
184 }
|
Chris@14
|
185 }
|