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