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