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\V1; |
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\HeaderCondition; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionCollection; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionIterator; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\MethodCondition; |
27
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition; |
28
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory; |
29
|
|
|
use Mcustiel\Phiremock\Domain\Condition\MatchersEnum; |
30
|
|
|
use Mcustiel\Phiremock\Domain\Conditions; |
31
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
32
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
33
|
|
|
|
34
|
|
|
class ArrayToRequestConditionConverter implements ArrayToRequestConditionConverterInterface |
35
|
|
|
{ |
36
|
|
|
const ALLOWED_OPTIONS = [ |
37
|
|
|
'method' => null, |
38
|
|
|
'url' => null, |
39
|
|
|
'body' => null, |
40
|
|
|
'headers'=> null, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** @var MatcherFactory */ |
44
|
|
|
private $matcherFactory; |
45
|
|
|
|
46
|
3 |
|
public function __construct() |
47
|
|
|
{ |
48
|
3 |
|
$this->matcherFactory = new MatcherFactory(); |
49
|
3 |
|
} |
50
|
|
|
|
51
|
3 |
|
public function convert(array $requestArray): Conditions |
52
|
|
|
{ |
53
|
3 |
|
$this->ensureNotInvalidOptionsAreProvided($requestArray); |
54
|
|
|
|
55
|
3 |
|
return new Conditions( |
56
|
3 |
|
$this->convertMethodCondition($requestArray['request']), |
57
|
3 |
|
$this->convertUrlCondition($requestArray['request']), |
58
|
3 |
|
$this->convertBodyCondition($requestArray['request']), |
59
|
3 |
|
$this->convertHeadersConditions($requestArray['request']), |
60
|
3 |
|
$this->convertScenarioState($requestArray) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
protected function ensureNotInvalidOptionsAreProvided(array $requestArray): void |
65
|
|
|
{ |
66
|
3 |
|
$invalidOptions = array_diff_key($requestArray['request'], static::ALLOWED_OPTIONS); |
67
|
3 |
|
if (!empty($invalidOptions)) { |
68
|
|
|
throw new \Exception('Unknown request conditions: ' . var_export($invalidOptions, true)); |
69
|
|
|
} |
70
|
3 |
|
} |
71
|
|
|
|
72
|
3 |
|
protected function convertHeadersConditions(array $requestArray): ?HeaderConditionIterator |
73
|
|
|
{ |
74
|
3 |
|
if (!empty($requestArray['headers'])) { |
75
|
1 |
|
$headers = $requestArray['headers']; |
76
|
1 |
|
if (!\is_array($headers)) { |
77
|
|
|
throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headers, true)); |
78
|
|
|
} |
79
|
1 |
|
$headersCollection = new HeaderConditionCollection(); |
80
|
1 |
|
foreach ($headers as $headerName => $header) { |
81
|
1 |
|
$headersCollection->setHeaderCondition( |
82
|
1 |
|
new HeaderName($headerName), |
83
|
1 |
|
$this->convertHeaderCondition($header) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $headersCollection->iterator(); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
protected function convertHeaderCondition($headerCondition): HeaderCondition |
94
|
|
|
{ |
95
|
1 |
|
if (!\is_array($headerCondition)) { |
96
|
|
|
throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headerCondition, true)); |
97
|
|
|
} |
98
|
1 |
|
$value = current($headerCondition); |
99
|
1 |
|
if (!\is_string($value)) { |
100
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return new HeaderCondition( |
104
|
1 |
|
$this->matcherFactory->createFrom(key($headerCondition), $value) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
protected function convertUrlCondition(array $requestArray): ?UrlCondition |
109
|
|
|
{ |
110
|
3 |
|
if (!empty($requestArray['url'])) { |
111
|
1 |
|
$urlCondition = $requestArray['url']; |
112
|
1 |
|
if (!\is_array($urlCondition)) { |
113
|
|
|
throw new \InvalidArgumentException('Url condition is invalid: ' . var_export($urlCondition, true)); |
114
|
|
|
} |
115
|
1 |
|
$value = current($urlCondition); |
116
|
1 |
|
if (!\is_string($value)) { |
117
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return new UrlCondition( |
121
|
1 |
|
$this->matcherFactory->createFrom(key($urlCondition), $value) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
protected function convertMethodCondition(array $requestArray): ?MethodCondition |
129
|
|
|
{ |
130
|
3 |
|
if (!empty($requestArray['method'])) { |
131
|
3 |
|
$method = $requestArray['method']; |
132
|
3 |
|
if (!\is_string($method)) { |
133
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($method)); |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
return new MethodCondition( |
137
|
3 |
|
$this->matcherFactory->createFrom(MatchersEnum::SAME_STRING, $method) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
protected function convertBodyCondition(array $requestArray): ?BodyCondition |
145
|
|
|
{ |
146
|
3 |
|
if (!empty($requestArray['body'])) { |
147
|
2 |
|
$bodyCondition = $requestArray['body']; |
148
|
2 |
|
if (!\is_array($bodyCondition)) { |
149
|
|
|
throw new \InvalidArgumentException('Body condition is invalid: ' . var_export($bodyCondition, true)); |
150
|
|
|
} |
151
|
2 |
|
$value = current($bodyCondition); |
152
|
2 |
|
if (!\is_string($value)) { |
153
|
|
|
throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value)); |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
return new BodyCondition( |
157
|
2 |
|
$this->matcherFactory->createFrom(key($bodyCondition), $value) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
protected function convertScenarioState(array $requestArray): ?ScenarioState |
165
|
|
|
{ |
166
|
3 |
|
if (!empty($requestArray['scenarioStateIs'])) { |
167
|
1 |
|
return new ScenarioState($requestArray['scenarioStateIs']); |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function getMatcherFactory(): MatcherFactory |
174
|
|
|
{ |
175
|
|
|
return $this->matcherFactory; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|