1 | <?php |
||
14 | class Mountebank extends Module |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $requiredFields = [ |
||
20 | 'host', |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $config = [ |
||
27 | 'port' => Juggler::DEFAULT_PORT, |
||
28 | 'imposters' => [], |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Mountebank client |
||
33 | * |
||
34 | * @var Juggler |
||
35 | */ |
||
36 | private $juggler; |
||
37 | |||
38 | /** |
||
39 | * Per-test imposters cache |
||
40 | * |
||
41 | * @var Imposter[] |
||
42 | */ |
||
43 | private $cachedImposters; |
||
44 | |||
45 | /** |
||
46 | * Map; imposter alias (key) to imposter port (value) |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $imposterAliasToPort = []; |
||
51 | |||
52 | /** |
||
53 | * List of imposters that were replaced during test |
||
54 | * Contains imposter aliases as keys and dummy values |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $replacedImposters = []; |
||
59 | |||
60 | /** |
||
61 | * @throws ConfigurationException |
||
62 | * @throws ModuleConfigException |
||
63 | */ |
||
64 | 4 | public function _initialize() |
|
70 | |||
71 | /** |
||
72 | * @return Juggler |
||
73 | */ |
||
74 | protected function createJuggler() |
||
78 | |||
79 | /** |
||
80 | * @throws ConfigurationException |
||
81 | * @throws Exception |
||
82 | */ |
||
83 | 4 | private function initializeImposters() |
|
88 | |||
89 | 4 | private function postConfiguredImposters() |
|
96 | |||
97 | /** |
||
98 | * @return Juggler |
||
99 | */ |
||
100 | public function _getJuggler() |
||
104 | |||
105 | /** |
||
106 | * @param TestInterface $test |
||
107 | */ |
||
108 | 1 | public function _before(TestInterface $test) |
|
116 | |||
117 | /** |
||
118 | * Imposter should be restored if it was replaced during test or it was specified as 'mock' in module configuration |
||
119 | * |
||
120 | * @param string $alias |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 1 | private function imposterShouldBeRestored($alias) |
|
129 | |||
130 | /** |
||
131 | * Restores imposter to initial state by posting contract from configuration |
||
132 | * |
||
133 | * @param string $alias |
||
134 | */ |
||
135 | 1 | public function restoreImposter($alias) |
|
143 | |||
144 | /** |
||
145 | * Replaces imposter without queueing it for restoration; |
||
146 | * Expects new imposter to have the same port as replaced imposter had |
||
147 | * |
||
148 | * @param string $alias |
||
149 | * @param string|Imposter $imposter_or_path |
||
150 | */ |
||
151 | 1 | private function silentlyReplaceImposter($alias, $imposter_or_path) |
|
168 | |||
169 | /** |
||
170 | * @param string $alias |
||
171 | * |
||
172 | * @return int Port |
||
173 | */ |
||
174 | 2 | private function resolveImposterPort($alias) |
|
182 | |||
183 | /** |
||
184 | * @throws Exception |
||
185 | */ |
||
186 | 1 | public function _afterSuite() |
|
190 | |||
191 | /** |
||
192 | * Saves imposter contracts to files if 'save' param was set in imposter config |
||
193 | * |
||
194 | * @throws Exception |
||
195 | */ |
||
196 | 1 | private function saveImposters() |
|
208 | |||
209 | /** |
||
210 | * Fetches imposter from mountebank or returns cached Imposter instance. |
||
211 | * Imposter instance gets cached for current test after fetching |
||
212 | * |
||
213 | * @param string $alias |
||
214 | * |
||
215 | * @return Imposter |
||
216 | * @see Mountebank::fetchImposter() To get Imposter without hitting cache |
||
217 | */ |
||
218 | public function getImposter($alias) |
||
226 | |||
227 | /** |
||
228 | * Retrieves imposter from mountebank |
||
229 | * Does not looks in cache but caches fetched Imposter instance |
||
230 | * |
||
231 | * @param string $alias |
||
232 | * |
||
233 | * @return Imposter |
||
234 | */ |
||
235 | public function fetchImposter($alias) |
||
241 | |||
242 | /** |
||
243 | * Asserts there is one or $exact_quantity of requests recorded in imposter that matches criteria |
||
244 | * |
||
245 | * @param string $alias |
||
246 | * @param array $criteria |
||
247 | * @param int $exact_quantity |
||
248 | * |
||
249 | * @see Imposter::findRequests() |
||
250 | */ |
||
251 | public function seeImposterHasRequestsByCriteria($alias, array $criteria, $exact_quantity = 1) |
||
270 | |||
271 | /** |
||
272 | * Asserts there is at least one request recorded in imposter |
||
273 | * |
||
274 | * @param string $alias |
||
275 | */ |
||
276 | public function seeImposterHasRequests($alias) |
||
283 | |||
284 | /** |
||
285 | * Asserts that there are no requests recorded in imposter |
||
286 | * |
||
287 | * @param string $alias |
||
288 | */ |
||
289 | public function seeImposterHasNoRequests($alias) |
||
296 | |||
297 | /** |
||
298 | * Replaces imposter with cached Imposter instance for current test |
||
299 | * Imposter instance gets cached when retrieved with Mountebank::getImposter() or Mountebank::fetchImposter() |
||
300 | * methods |
||
301 | * |
||
302 | * @param string $alias |
||
303 | * |
||
304 | * @see Mountebank::getImposter() |
||
305 | * @see Mountebank::fetchImposter() |
||
306 | */ |
||
307 | public function replaceImposterWithCached($alias) |
||
315 | |||
316 | /** |
||
317 | * Replaces imposter until next test |
||
318 | * |
||
319 | * @param string $alias |
||
320 | * @param string|Imposter $imposter_or_path Path to imposter contract or Imposter instance |
||
321 | */ |
||
322 | public function replaceImposter($alias, $imposter_or_path) |
||
327 | |||
328 | 8 | protected function validateConfig() |
|
333 | |||
334 | 7 | protected function validateImpostersConfig() |
|
342 | |||
343 | /** |
||
344 | * @param string $relative_path Path relative to project directory |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function getFullPath($relative_path) |
||
352 | } |
||
353 |