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\Server\Utils\Strategies; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
22
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Response; |
24
|
|
|
use Mcustiel\Phiremock\Domain\ScenarioStateInfo; |
25
|
|
|
use Mcustiel\Phiremock\Server\Model\ScenarioStorage; |
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
27
|
|
|
use Psr\Log\LoggerInterface; |
28
|
|
|
use RuntimeException; |
29
|
|
|
|
30
|
|
|
class AbstractResponse |
31
|
|
|
{ |
32
|
|
|
/** @var LoggerInterface */ |
33
|
|
|
protected $logger; |
34
|
|
|
/** @var ScenarioStorage */ |
35
|
|
|
private $scenariosStorage; |
36
|
|
|
|
37
|
|
|
public function __construct(ScenarioStorage $scenarioStorage, LoggerInterface $logger) |
38
|
|
|
{ |
39
|
|
|
$this->scenariosStorage = $scenarioStorage; |
40
|
|
|
$this->logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function processDelay(Response $responseConfig): void |
44
|
|
|
{ |
45
|
|
|
if ($responseConfig->getDelayMillis()) { |
46
|
|
|
$this->logger->debug( |
47
|
|
|
'Delaying the response for ' . $responseConfig->getDelayMillis()->asInt() . ' milliseconds' |
48
|
|
|
); |
49
|
|
|
usleep($responseConfig->getDelayMillis()->asInt() * 1000); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function processScenario(Expectation $foundExpectation): void |
54
|
|
|
{ |
55
|
|
|
if ($foundExpectation->getResponse()->hasNewScenarioState()) { |
56
|
|
|
if (!$foundExpectation->hasScenarioName()) { |
57
|
|
|
throw new RuntimeException('Expecting scenario state without specifying scenario name'); |
58
|
|
|
} |
59
|
|
|
$this->logger->debug( |
60
|
|
|
sprintf( |
61
|
|
|
'Setting scenario %s to %s', |
62
|
|
|
$foundExpectation->getScenarioName()->asString(), |
63
|
|
|
$foundExpectation->getResponse()->getNewScenarioState()->asString() |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
$this->scenariosStorage->setScenarioState( |
67
|
|
|
new ScenarioStateInfo( |
68
|
|
|
$foundExpectation->getScenarioName(), |
69
|
|
|
$foundExpectation->getResponse()->getNewScenarioState() |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getResponseWithHeaders(HttpResponse $responseConfig, ResponseInterface $httpResponse): ResponseInterface |
76
|
|
|
{ |
77
|
|
|
if ($responseConfig->getHeaders()) { |
78
|
|
|
foreach ($responseConfig->getHeaders() as $header) { |
79
|
|
|
$httpResponse = $httpResponse->withHeader( |
80
|
|
|
$header->getName()->asString(), |
81
|
|
|
$header->getValue()->asString() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $httpResponse; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getResponseWithStatusCode(HttpResponse $responseConfig, ResponseInterface $httpResponse): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
if ($responseConfig->getStatusCode()) { |
92
|
|
|
$httpResponse = $httpResponse->withStatus($responseConfig->getStatusCode()->asInt()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $httpResponse; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|