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