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\ArrayToExpectationConverter as ArrayToExpectationConverterInterface; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Conditions; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Http\StatusCode; |
25
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Options\Priority; |
27
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioName; |
28
|
|
|
use Mcustiel\Phiremock\Domain\Response; |
29
|
|
|
use Mcustiel\Phiremock\Domain\Version; |
30
|
|
|
|
31
|
|
|
class ArrayToExpectationConverter implements ArrayToExpectationConverterInterface |
32
|
|
|
{ |
33
|
|
|
const ALLOWED_OPTIONS = [ |
34
|
|
|
'version' => null, |
35
|
|
|
'scenarioName' => null, |
36
|
|
|
'scenarioStateIs' => null, |
37
|
|
|
'newScenarioState'=> null, |
38
|
|
|
'priority' => null, |
39
|
|
|
'proxyTo' => null, |
40
|
|
|
'request' => null, |
41
|
|
|
'response' => null, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** @var ArrayToRequestConditionConverter */ |
45
|
|
|
private $arrayToConditionsConverter; |
46
|
|
|
/** @var ArrayToResponseConverterLocator */ |
47
|
|
|
private $arrayToResponseConverterLocator; |
48
|
|
|
|
49
|
9 |
|
public function __construct( |
50
|
|
|
ArrayToRequestConditionConverter $arrayToConditionsConverter, |
51
|
|
|
ArrayToResponseConverterLocator $arrayToResponseConverterLocator |
52
|
|
|
) { |
53
|
9 |
|
$this->arrayToConditionsConverter = $arrayToConditionsConverter; |
54
|
9 |
|
$this->arrayToResponseConverterLocator = $arrayToResponseConverterLocator; |
55
|
9 |
|
} |
56
|
|
|
|
57
|
9 |
|
public function convert(array $expectationArray): Expectation |
58
|
|
|
{ |
59
|
9 |
|
$this->ensureNotInvalidOptionsAreProvided($expectationArray); |
60
|
9 |
|
$version = $this->getVersion($expectationArray); |
61
|
9 |
|
$request = $this->convertRequest($expectationArray); |
62
|
9 |
|
$response = $this->convertResponse($expectationArray); |
63
|
9 |
|
$scenarioName = $this->getScenarioName($expectationArray); |
64
|
9 |
|
$priority = $this->getPriority($expectationArray); |
65
|
|
|
|
66
|
9 |
|
return new Expectation($request, $response, $scenarioName, $priority, $version); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
private function ensureNotInvalidOptionsAreProvided(array $expectationArray): void |
70
|
|
|
{ |
71
|
9 |
|
$invalidOptions = array_diff_key($expectationArray, self::ALLOWED_OPTIONS); |
72
|
9 |
|
if (!empty($invalidOptions)) { |
73
|
|
|
throw new \Exception('Unknown expectation options: ' . var_export($invalidOptions, true)); |
74
|
|
|
} |
75
|
9 |
|
} |
76
|
|
|
|
77
|
9 |
|
private function getVersion(array $expectationArray): Version |
78
|
|
|
{ |
79
|
9 |
|
if (isset($expectationArray['version'])) { |
80
|
|
|
return new Version((int) $expectationArray['version']); |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
return new Version(1); |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
private function getPriority(array $expectationArray): ?Priority |
87
|
|
|
{ |
88
|
9 |
|
$priority = null; |
89
|
9 |
|
if (!empty($expectationArray['priority'])) { |
90
|
3 |
|
$priority = new Priority((int) $expectationArray['priority']); |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
return $priority; |
94
|
|
|
} |
95
|
|
|
|
96
|
9 |
|
private function getScenarioName(array $expectationArray): ?ScenarioName |
97
|
|
|
{ |
98
|
9 |
|
$scenarioName = null; |
99
|
9 |
|
if (!empty($expectationArray['scenarioName'])) { |
100
|
3 |
|
$scenarioName = new ScenarioName($expectationArray['scenarioName']); |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
return $scenarioName; |
104
|
|
|
} |
105
|
|
|
|
106
|
9 |
|
private function convertResponse(array $expectationArray): Response |
107
|
|
|
{ |
108
|
9 |
|
if (!isset($expectationArray['response']) && !isset($expectationArray['proxyTo'])) { |
109
|
|
|
return new HttpResponse(new StatusCode(200), null, null, null, null); |
110
|
|
|
} |
111
|
9 |
|
if (!isset($expectationArray['proxyTo']) && !\is_array($expectationArray['response'])) { |
112
|
|
|
throw new \InvalidArgumentException('Invalid response definition: ' . var_export($expectationArray['response'], true)); |
113
|
|
|
} |
114
|
|
|
|
115
|
9 |
|
return $this->arrayToResponseConverterLocator |
116
|
9 |
|
->locate($expectationArray) |
117
|
9 |
|
->convert($expectationArray); |
118
|
|
|
} |
119
|
|
|
|
120
|
9 |
|
private function convertRequest(array $expectationArray): Conditions |
121
|
|
|
{ |
122
|
9 |
|
if (!isset($expectationArray['request'])) { |
123
|
|
|
throw new \InvalidArgumentException('Expectation request is not set'); |
124
|
|
|
} |
125
|
|
|
|
126
|
9 |
|
return $this->arrayToConditionsConverter->convert($expectationArray); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|