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\V1\ExpectationToArrayConverter as ExpectationToArrayConverterV1; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
23
|
|
|
|
24
|
|
|
class ExpectationToArrayConverter extends ExpectationToArrayConverterV1 |
25
|
|
|
{ |
26
|
|
|
/** @var RequestConditionToArrayConverter */ |
27
|
|
|
private $requestToArrayConverter; |
28
|
|
|
|
29
|
|
|
/** @var ResponseToArrayConverterLocator */ |
30
|
|
|
private $responseConverterLocator; |
31
|
|
|
|
32
|
3 |
|
public function __construct( |
33
|
|
|
RequestConditionToArrayConverter $requestConverter, |
34
|
|
|
ResponseToArrayConverterLocator $responseConverterLocator |
35
|
|
|
) { |
36
|
3 |
|
$this->requestToArrayConverter = $requestConverter; |
37
|
3 |
|
$this->responseConverterLocator = $responseConverterLocator; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
3 |
|
public function convert(Expectation $expectation): array |
41
|
|
|
{ |
42
|
3 |
|
$expectationArray = []; |
43
|
|
|
|
44
|
3 |
|
$expectationArray['version'] = $expectation->getVersion()->asString(); |
45
|
|
|
|
46
|
3 |
|
$expectationArray['scenarioName'] = $this->getScenarioName($expectation); |
47
|
3 |
|
$expectationArray['on'] = $this->requestToArrayConverter->convert($expectation->getRequest()); |
48
|
|
|
|
49
|
3 |
|
$response = $expectation->getResponse(); |
50
|
3 |
|
$expectationArray['then'] = $this->responseConverterLocator |
51
|
3 |
|
->locate($response) |
52
|
3 |
|
->convert($response); |
53
|
|
|
|
54
|
3 |
|
$expectationArray['priority'] = $this->getPriority($expectation); |
55
|
|
|
|
56
|
3 |
|
return $expectationArray; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function setResponse(Expectation $expectation, array &$expectationArray): void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$response = $expectation->getResponse(); |
62
|
|
|
|
63
|
|
|
if ($response->isHttpResponse()) { |
64
|
|
|
/* @var \Mcustiel\Phiremock\Domain\HttpResponse $response */ |
65
|
|
|
} else { |
66
|
|
|
/* @var \Mcustiel\Phiremock\Domain\ProxyResponse $response */ |
67
|
|
|
$expectationArray['response'] = null; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
private function getScenarioName(Expectation $expectation): ?string |
72
|
|
|
{ |
73
|
3 |
|
if ($expectation->hasScenarioName()) { |
74
|
1 |
|
return $expectation->getScenarioName()->asString(); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getScenarioState(Expectation $expectation): ?string |
81
|
|
|
{ |
82
|
|
|
if ($expectation->getRequest()->hasScenarioState()) { |
83
|
|
|
return $expectation->getRequest()->getScenarioState()->asString(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getNewScenarioState(Expectation $expectation): ?string |
90
|
|
|
{ |
91
|
|
|
if ($expectation->getResponse()->hasNewScenarioState()) { |
92
|
|
|
return $expectation->getResponse()->getNewScenarioState()->asString(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
private function getPriority(Expectation $expectation): int |
99
|
|
|
{ |
100
|
3 |
|
if ($expectation->hasPriority()) { |
101
|
1 |
|
return $expectation->getPriority()->asInt(); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return 0; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.