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