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\ArrayToResponseConverter as ArrayToResponseConverterInterface; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Options\Delay; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Response; |
25
|
|
|
|
26
|
|
|
abstract class ArrayToResponseConverter implements ArrayToResponseConverterInterface |
27
|
|
|
{ |
28
|
|
|
const ALLOWED_OPTIONS = ['delayMillis'=> null, 'newScenarioState' => null, 'response' => null, 'proxyTo' => null]; |
29
|
|
|
const NO_DELAY = 0; |
30
|
|
|
|
31
|
15 |
|
public function convert(array $responseArray): Response |
32
|
|
|
{ |
33
|
15 |
|
$this->ensureNotInvalidOptionsAreProvided($responseArray, self::ALLOWED_OPTIONS); |
34
|
|
|
|
35
|
15 |
|
return $this->convertResponse( |
36
|
15 |
|
$responseArray, |
37
|
15 |
|
$this->getDelay($responseArray), |
38
|
15 |
|
$this->getNewScenarioState($responseArray) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
15 |
|
protected function ensureNotInvalidOptionsAreProvided( |
43
|
|
|
array $responseArray, |
44
|
|
|
array $validOptions |
45
|
|
|
): void { |
46
|
15 |
|
$invalidOptions = array_diff_key($responseArray, $validOptions); |
47
|
15 |
|
if (!empty($invalidOptions)) { |
48
|
|
|
throw new \Exception('Unknown expectation options: ' . var_export($invalidOptions, true)); |
49
|
|
|
} |
50
|
15 |
|
} |
51
|
|
|
|
52
|
|
|
abstract protected function convertResponse( |
53
|
|
|
array $response, |
54
|
|
|
?Delay $delay, |
55
|
|
|
?ScenarioState $newScenarioState |
56
|
|
|
): Response; |
57
|
|
|
|
58
|
15 |
|
private function getDelay(array $responseArray): ?Delay |
59
|
|
|
{ |
60
|
15 |
|
if (!empty($responseArray['delayMillis'])) { |
61
|
3 |
|
return new Delay($responseArray['delayMillis']); |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
15 |
|
private function getNewScenarioState(array $expectationArray): ?ScenarioState |
68
|
|
|
{ |
69
|
15 |
|
$newScenarioState = null; |
70
|
15 |
|
if (!empty($expectationArray['newScenarioState'])) { |
71
|
3 |
|
$newScenarioState = new ScenarioState($expectationArray['newScenarioState']); |
72
|
|
|
} |
73
|
|
|
|
74
|
15 |
|
return $newScenarioState; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|