1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phiremock-codeception-module. |
4
|
|
|
* |
5
|
|
|
* phiremock-codeception-module is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* phiremock-codeception-module is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with phiremock-codeception-module. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Codeception\Module; |
20
|
|
|
|
21
|
|
|
use Codeception\Exception\ConfigurationException; |
22
|
|
|
use Codeception\Exception\ModuleException; |
23
|
|
|
use Codeception\Exception\ParseException; |
24
|
|
|
use Codeception\Lib\ModuleContainer; |
25
|
|
|
use Codeception\Module as CodeceptionModule; |
26
|
|
|
use Codeception\TestInterface; |
27
|
|
|
use Exception; |
28
|
|
|
use InvalidArgumentException; |
29
|
|
|
use Mcustiel\Phiremock\Client\Connection\Scheme; |
30
|
|
|
use Mcustiel\Phiremock\Client\Utils\ConditionsBuilder; |
31
|
|
|
use Mcustiel\Phiremock\Codeception\Util\Config; |
32
|
|
|
use Mcustiel\Phiremock\Codeception\Util\ExpectationAnnotationParser; |
33
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
34
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
35
|
|
|
|
36
|
|
|
class Phiremock extends CodeceptionModule |
37
|
|
|
{ |
38
|
|
|
/** @var array */ |
39
|
|
|
protected array $config = Config::DEFAULT_CONFIG; |
40
|
|
|
|
41
|
|
|
/** @var \Mcustiel\Phiremock\Client\Phiremock */ |
42
|
|
|
private $phiremock; |
43
|
|
|
|
44
|
|
|
/** @var ExpectationAnnotationParser */ |
45
|
|
|
private $expectationsParser; |
46
|
|
|
|
47
|
|
|
/** @var Config */ |
48
|
|
|
private $moduleConfig; |
49
|
|
|
|
50
|
|
|
/** @var Phiremock[] */ |
51
|
|
|
private $extraConnections = []; |
52
|
|
|
|
53
|
|
|
/** @var bool */ |
54
|
|
|
private $isExtraConfig; |
55
|
|
|
|
56
|
|
|
/** @throws ModuleException */ |
57
|
|
|
public function __construct(ModuleContainer $moduleContainer, $config = null, bool $isExtra = false) |
58
|
|
|
{ |
59
|
|
|
parent::__construct($moduleContainer, $config); |
60
|
|
|
$this->isExtraConfig = $isExtra; |
61
|
|
|
$this->moduleConfig = new Config($this->config, $this->createDebugMethod()); |
62
|
|
|
$this->setupExtraConnections(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $settings |
67
|
|
|
* @throws ConfigurationException |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
public function _beforeSuite($settings = []) |
71
|
|
|
{ |
72
|
|
|
$this->config = array_merge($this->moduleConfig->asArray(), $settings); |
73
|
|
|
$this->moduleConfig = new Config($this->config, $this->createDebugMethod()); |
74
|
|
|
|
75
|
|
|
$this->phiremock = $this->moduleConfig->getClientFactory()->createPhiremockClient( |
76
|
|
|
$this->moduleConfig->getHost(), |
77
|
|
|
$this->moduleConfig->getPort(), |
78
|
|
|
new Scheme($this->moduleConfig->isSecure() ? Scheme::HTTPS: Scheme::HTTP) |
79
|
|
|
); |
80
|
|
|
$this->expectationsParser = new ExpectationAnnotationParser( |
81
|
|
|
$this->moduleConfig->getExpectationsPath() |
82
|
|
|
); |
83
|
|
|
$this->extraConfigsBeforeSuite($settings); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @throws ClientExceptionInterface */ |
87
|
|
|
public function _before(TestInterface $test) |
88
|
|
|
{ |
89
|
|
|
$this->resetBeforeTestsIfConfigured(); |
90
|
|
|
$this->loadExpectations($test); |
91
|
|
|
parent::_before($test); |
92
|
|
|
$this->extraConfigsBefore($test); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @throws ModuleException */ |
96
|
|
|
public function takeConnection(string $name): Phiremock |
97
|
|
|
{ |
98
|
|
|
if ($this->isExtraConfig) { |
99
|
|
|
throw new ModuleException($this, 'Trying to take a connection from an extra connection'); |
100
|
|
|
} |
101
|
|
|
if ($name === 'default') { |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
if (!isset($this->extraConnections[$name])) { |
105
|
|
|
throw new InvalidArgumentException( |
106
|
|
|
sprintf('Connection %s does not exist', $name) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
return $this->extraConnections[$name]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @throws ClientExceptionInterface */ |
113
|
|
|
public function expectARequestToRemoteServiceWithAResponse(Expectation $expectation): void |
114
|
|
|
{ |
115
|
|
|
$this->phiremock->createExpectation($expectation); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** @throws ClientExceptionInterface */ |
119
|
|
|
public function haveACleanSetupInRemoteService(): void |
120
|
|
|
{ |
121
|
|
|
$this->phiremock->reset(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** @throws ClientExceptionInterface */ |
125
|
|
|
public function dontExpectRequestsInRemoteService(): void |
126
|
|
|
{ |
127
|
|
|
$this->phiremock->clearExpectations(); |
128
|
|
|
$this->phiremock->resetRequestsCounter(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @throws ClientExceptionInterface */ |
132
|
|
|
public function haveCleanScenariosInRemoteService(): void |
133
|
|
|
{ |
134
|
|
|
$this->phiremock->resetScenarios(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @deprecated Name is confusing, sounds like an assertion |
139
|
|
|
* @throws ClientExceptionInterface |
140
|
|
|
*/ |
141
|
|
|
public function didNotReceiveRequestsInRemoteService(): void |
142
|
|
|
{ |
143
|
|
|
$this->dontHaveLoggedRequestsToRemoteService(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** @throws ClientExceptionInterface */ |
147
|
|
|
public function dontHaveLoggedRequestsToRemoteService(): void |
148
|
|
|
{ |
149
|
|
|
$this->phiremock->resetRequestsCounter(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @throws ClientExceptionInterface |
154
|
|
|
* @throws Exception |
155
|
|
|
*/ |
156
|
|
|
public function seeRemoteServiceReceived(int $times, ConditionsBuilder $builder): void |
157
|
|
|
{ |
158
|
|
|
$requests = $this->phiremock->countExecutions($builder); |
159
|
|
|
if ($times !== $requests) { |
160
|
|
|
throw new Exception( |
161
|
|
|
"Request expected to be executed $times times, called $requests times instead" |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** @throws ClientExceptionInterface */ |
167
|
|
|
public function grabRequestsMadeToRemoteService(ConditionsBuilder $builder): array |
168
|
|
|
{ |
169
|
|
|
return $this->phiremock->listExecutions($builder); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** @throws ClientExceptionInterface */ |
173
|
|
|
public function setScenarioState(string $name, string $state): void |
174
|
|
|
{ |
175
|
|
|
$this->phiremock->setScenarioState($name, $state); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** @throws ModuleException */ |
179
|
|
|
protected function setupExtraConnections(): void |
180
|
|
|
{ |
181
|
|
|
if (!$this->isExtraConfig) { |
182
|
|
|
foreach ($this->moduleConfig->getExtraConnectionsConfigs() as $name => $connectionConfig) { |
183
|
|
|
if ($name === 'default') { |
184
|
|
|
throw new ModuleException($this, 'The connection name "default" is reserved and can not be used for an extra connection'); |
185
|
|
|
} |
186
|
|
|
$this->extraConnections[$name] = new self($this->moduleContainer, $connectionConfig->asArray(), true); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function createDebugMethod(): callable |
192
|
|
|
{ |
193
|
|
|
return function (string $msg): void { |
194
|
|
|
$this->debug($msg); |
195
|
|
|
}; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** @throws ConfigurationException */ |
199
|
|
|
private function extraConfigsBeforeSuite(array $settings): void |
200
|
|
|
{ |
201
|
|
|
if (!$this->isExtraConfig) { |
202
|
|
|
foreach ($this->extraConnections as $module) { |
203
|
|
|
$module->_beforeSuite($settings); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** @throws ClientExceptionInterface */ |
209
|
|
|
private function resetBeforeTestsIfConfigured(): void |
210
|
|
|
{ |
211
|
|
|
if ($this->moduleConfig->isResetBeforeEachTest()) { |
212
|
|
|
$this->haveACleanSetupInRemoteService(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** @throws ClientExceptionInterface */ |
217
|
|
|
private function loadExpectations(TestInterface $test): void |
218
|
|
|
{ |
219
|
|
|
try { |
220
|
|
|
$expectations = $this->expectationsParser->getExpectations($test); |
221
|
|
|
if (!empty($expectations)) { |
222
|
|
|
foreach ($expectations as $expectation) { |
223
|
|
|
$this->phiremock->createExpectationFromJson( |
224
|
|
|
file_get_contents($expectation) |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} catch (ParseException $exception) { |
229
|
|
|
$this->debug('Error parsing expectation annotations: ' . $exception->getMessage()); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** @throws ClientExceptionInterface */ |
234
|
|
|
private function extraConfigsBefore(TestInterface $test): void |
235
|
|
|
{ |
236
|
|
|
if (!$this->isExtraConfig) { |
237
|
|
|
foreach ($this->extraConnections as $connection) { |
238
|
|
|
$connection->_before($test); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|