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