1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock 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 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. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Common\Utils\V2; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\Utils\ArrayToRequestConditionConverter as ArrayToRequestConditionConverterInterface; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\BodyCondition; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormDataCondition; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldCondition; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldConditionIterator; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition; |
27
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionCollection; |
28
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionIterator; |
29
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\JsonPathCondition; |
30
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\JsonPathConditionCollection; |
31
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\JsonPathConditionIterator; |
32
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\MethodCondition; |
33
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition; |
34
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory; |
35
|
|
|
use Mcustiel\Phiremock\Domain\Conditions; |
36
|
|
|
use Mcustiel\Phiremock\Domain\Http\FormFieldName; |
37
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
38
|
|
|
use Mcustiel\Phiremock\Domain\Http\JsonPathName; |
39
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
40
|
|
|
|
41
|
|
|
class ArrayToRequestConditionConverter implements ArrayToRequestConditionConverterInterface |
42
|
|
|
{ |
43
|
|
|
const ALLOWED_OPTIONS = [ |
44
|
|
|
'scenarioStateIs' => null, |
45
|
|
|
'method' => null, |
46
|
|
|
'url' => null, |
47
|
|
|
'body' => null, |
48
|
|
|
'headers' => null, |
49
|
|
|
'formData' => null, |
50
|
|
|
'jsonPath' => null, |
51
|
15 |
|
]; |
52
|
|
|
|
53
|
15 |
|
/** @var MatcherFactory */ |
54
|
15 |
|
private $matcherFactory; |
55
|
|
|
|
56
|
15 |
|
public function __construct() |
57
|
|
|
{ |
58
|
15 |
|
$this->matcherFactory = new MatcherFactory(); |
59
|
|
|
} |
60
|
15 |
|
|
61
|
15 |
|
public function convert(array $requestArray): Conditions |
62
|
15 |
|
{ |
63
|
15 |
|
$this->ensureNotInvalidOptionsAreProvided($requestArray); |
64
|
15 |
|
|
65
|
15 |
|
return new Conditions( |
66
|
15 |
|
$this->convertMethodCondition($requestArray), |
67
|
|
|
$this->convertUrlCondition($requestArray), |
68
|
|
|
$this->convertBodyCondition($requestArray), |
69
|
|
|
$this->convertHeadersConditions($requestArray), |
70
|
15 |
|
$this->convertFormDataConditions($requestArray), |
71
|
|
|
$this->convertScenarioState($requestArray), |
72
|
15 |
|
$this->convertJsonPathConditions($requestArray) |
73
|
15 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
15 |
|
protected function ensureNotInvalidOptionsAreProvided(array $requestArray): void |
77
|
|
|
{ |
78
|
15 |
|
$invalidOptions = array_diff_key($requestArray, static::ALLOWED_OPTIONS); |
79
|
|
|
if (!empty($invalidOptions)) { |
80
|
15 |
|
throw new \Exception('Unknown request conditions: ' . var_export($invalidOptions, true)); |
81
|
3 |
|
} |
82
|
3 |
|
} |
83
|
|
|
|
84
|
|
|
protected function convertHeadersConditions(array $requestArray): ?HeaderConditionIterator |
85
|
3 |
|
{ |
86
|
3 |
|
if (!empty($requestArray['headers'])) { |
87
|
3 |
|
$headers = $requestArray['headers']; |
88
|
3 |
|
if (!\is_array($headers)) { |
89
|
3 |
|
throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headers, true)); |
90
|
|
|
} |
91
|
|
|
$headersCollection = new HeaderConditionCollection(); |
92
|
|
|
foreach ($headers as $headerName => $header) { |
93
|
3 |
|
$headersCollection->setHeaderCondition( |
94
|
|
|
new HeaderName($headerName), |
95
|
|
|
$this->convertHeaderCondition($header) |
96
|
12 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return $headersCollection->iterator(); |
100
|
|
|
} |
101
|
3 |
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
3 |
|
|
105
|
3 |
|
protected function convertHeaderCondition($headerCondition): HeaderCondition |
106
|
|
|
{ |
107
|
|
|
if (!\is_array($headerCondition)) { |
108
|
|
|
throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headerCondition, true)); |
109
|
3 |
|
} |
110
|
3 |
|
$value = current($headerCondition); |
111
|
|
|
if (!\is_string($value)) { |
112
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
113
|
|
|
} |
114
|
15 |
|
|
115
|
|
|
return new HeaderCondition( |
116
|
15 |
|
$this->matcherFactory->createFrom(key($headerCondition), $value) |
117
|
3 |
|
); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
protected function convertFormDataConditions(array $requestArray): ?FormFieldConditionIterator |
121
|
3 |
|
{ |
122
|
3 |
|
if (!empty($requestArray['formData'])) { |
123
|
3 |
|
$formFields = $requestArray['formData']; |
124
|
3 |
|
if (!\is_array($formFields)) { |
125
|
3 |
|
throw new \InvalidArgumentException('Form data condition is invalid: ' . var_export($formFields, true)); |
126
|
|
|
} |
127
|
|
|
$formData = new FormDataCondition(); |
128
|
|
|
foreach ($formFields as $formFieldName => $condition) { |
129
|
3 |
|
$formData->setFieldCondition( |
130
|
|
|
new FormFieldName($formFieldName), |
131
|
|
|
$this->convertFormFieldCondition($condition) |
132
|
12 |
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
return $formData->iterator(); |
136
|
|
|
} |
137
|
3 |
|
|
138
|
|
|
return null; |
139
|
|
|
} |
140
|
3 |
|
|
141
|
3 |
|
protected function convertFormFieldCondition($fieldCondition): FormFieldCondition |
142
|
|
|
{ |
143
|
|
|
if (!\is_array($fieldCondition)) { |
144
|
|
|
throw new \InvalidArgumentException('Form field condition is invalid: ' . var_export($fieldCondition, true)); |
145
|
3 |
|
} |
146
|
3 |
|
$value = current($fieldCondition); |
147
|
|
|
if (!\is_string($value)) { |
148
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
149
|
|
|
} |
150
|
15 |
|
|
151
|
|
|
return new FormFieldCondition( |
152
|
15 |
|
$this->matcherFactory->createFrom(key($fieldCondition), $value) |
153
|
3 |
|
); |
154
|
3 |
|
} |
155
|
|
|
|
156
|
|
|
protected function convertUrlCondition(array $requestArray): ?UrlCondition |
157
|
3 |
|
{ |
158
|
3 |
|
if (!empty($requestArray['url'])) { |
159
|
|
|
$urlCondition = $requestArray['url']; |
160
|
|
|
if (!\is_array($urlCondition)) { |
161
|
|
|
throw new \InvalidArgumentException('Url condition is invalid: ' . var_export($urlCondition, true)); |
162
|
3 |
|
} |
163
|
3 |
|
$value = current($urlCondition); |
164
|
|
|
if (!\is_string($value)) { |
165
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
166
|
|
|
} |
167
|
12 |
|
|
168
|
|
|
return new UrlCondition( |
169
|
|
|
$this->matcherFactory->createFrom(key($urlCondition), $value) |
170
|
15 |
|
); |
171
|
|
|
} |
172
|
15 |
|
|
173
|
6 |
|
return null; |
174
|
6 |
|
} |
175
|
|
|
|
176
|
|
|
protected function convertBodyCondition(array $requestArray): ?BodyCondition |
177
|
6 |
|
{ |
178
|
6 |
|
if (!empty($requestArray['body'])) { |
179
|
|
|
$bodyCondition = $requestArray['body']; |
180
|
|
|
if (!\is_array($bodyCondition)) { |
181
|
|
|
throw new \InvalidArgumentException('Body condition is invalid: ' . var_export($bodyCondition, true)); |
182
|
6 |
|
} |
183
|
6 |
|
$value = current($bodyCondition); |
184
|
|
|
if (!\is_string($value)) { |
185
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
186
|
|
|
} |
187
|
9 |
|
|
188
|
|
|
return new BodyCondition( |
189
|
|
|
$this->matcherFactory->createFrom(key($bodyCondition), $value) |
190
|
15 |
|
); |
191
|
|
|
} |
192
|
15 |
|
|
193
|
3 |
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
12 |
|
protected function convertScenarioState(array $requestArray): ?ScenarioState |
197
|
|
|
{ |
198
|
|
|
if (!empty($requestArray['scenarioStateIs'])) { |
199
|
9 |
|
return new ScenarioState($requestArray['scenarioStateIs']); |
200
|
|
|
} |
201
|
9 |
|
|
202
|
|
|
return null; |
203
|
|
|
} |
204
|
15 |
|
|
205
|
|
|
protected function getMatcherFactory(): MatcherFactory |
206
|
15 |
|
{ |
207
|
9 |
|
return $this->matcherFactory; |
208
|
9 |
|
} |
209
|
|
|
|
210
|
|
|
protected function convertMethodCondition(array $requestArray): ?MethodCondition |
211
|
9 |
|
{ |
212
|
9 |
|
if (!empty($requestArray['method'])) { |
213
|
|
|
$methodCondition = $requestArray['method']; |
214
|
|
|
if (!\is_array($methodCondition)) { |
215
|
|
|
throw new \InvalidArgumentException('Method condition is invalid: ' . var_export($methodCondition, true)); |
216
|
9 |
|
} |
217
|
9 |
|
$value = current($methodCondition); |
218
|
|
|
if (!\is_string($value)) { |
219
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
220
|
|
|
} |
221
|
6 |
|
|
222
|
|
|
return new MethodCondition( |
223
|
|
|
$this->getMatcherFactory()->createFrom(key($methodCondition), $value) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return null; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function convertJsonPathConditions(array $requestArray): ?JsonPathConditionIterator |
231
|
|
|
{ |
232
|
|
|
if (!empty($requestArray['jsonPath'])) { |
233
|
|
|
$jsonPaths = $requestArray['jsonPath']; |
234
|
|
|
if (!\is_array($jsonPaths)) { |
235
|
|
|
throw new \InvalidArgumentException('Json path condition is invalid: ' . var_export($jsonPaths, true)); |
236
|
|
|
} |
237
|
|
|
$collection = new JsonPathConditionCollection(); |
238
|
|
|
foreach ($jsonPaths as $path => $condition) { |
239
|
|
|
if (!\is_array($condition)) { |
240
|
|
|
throw new \InvalidArgumentException('Json path condition is invalid: ' . var_export($condition, true)); |
241
|
|
|
} |
242
|
|
|
$value = current($condition); |
243
|
|
|
if (!\is_scalar($value)) { |
244
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected scalar value, got: ' . \gettype($value)); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$collection->setPathCondition( |
248
|
|
|
new JsonPathName($path), |
249
|
|
|
new JsonPathCondition( |
250
|
|
|
$this->getMatcherFactory()->createFrom(key($condition), $value) |
251
|
|
|
) |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
return $collection->iterator(); |
255
|
|
|
} |
256
|
|
|
return null; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|