|
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\Actions; |
|
20
|
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
|
22
|
|
|
use Mcustiel\Phiremock\Server\Model\ExpectationStorage; |
|
23
|
|
|
use Mcustiel\Phiremock\Server\Model\RequestStorage; |
|
24
|
|
|
use Mcustiel\Phiremock\Server\Utils\RequestExpectationComparator; |
|
25
|
|
|
use Mcustiel\Phiremock\Server\Utils\ResponseStrategyLocator; |
|
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
27
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
28
|
|
|
use Psr\Log\LoggerInterface; |
|
29
|
|
|
|
|
30
|
|
|
class SearchRequestAction implements ActionInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var ExpectationStorage */ |
|
33
|
|
|
private $expectationsStorage; |
|
34
|
|
|
/** @var RequestExpectationComparator */ |
|
35
|
|
|
private $comparator; |
|
36
|
|
|
/** @var LoggerInterface */ |
|
37
|
|
|
private $logger; |
|
38
|
|
|
/** @var ResponseStrategyLocator */ |
|
39
|
|
|
private $responseStrategyFactory; |
|
40
|
|
|
/** @var RequestStorage */ |
|
41
|
|
|
private $requestsStorage; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ExpectationStorage $expectationsStorage, |
|
45
|
|
|
RequestExpectationComparator $comparator, |
|
46
|
|
|
ResponseStrategyLocator $responseStrategyLocator, |
|
47
|
|
|
RequestStorage $requestsStorage, |
|
48
|
|
|
LoggerInterface $logger |
|
49
|
|
|
) { |
|
50
|
|
|
$this->expectationsStorage = $expectationsStorage; |
|
51
|
|
|
$this->comparator = $comparator; |
|
52
|
|
|
$this->logger = $logger; |
|
53
|
|
|
$this->requestsStorage = $requestsStorage; |
|
54
|
|
|
$this->responseStrategyFactory = $responseStrategyLocator; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function execute(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$this->logger->info('Request received: ' . $this->getLoggableRequest($request)); |
|
60
|
|
|
$this->requestsStorage->addRequest($request); |
|
61
|
|
|
$foundExpectation = $this->searchForMatchingExpectation($request); |
|
62
|
|
|
if (null === $foundExpectation) { |
|
63
|
|
|
return $response->withStatus(404, 'Not Found'); |
|
64
|
|
|
} |
|
65
|
|
|
$response = $this->responseStrategyFactory |
|
66
|
|
|
->getStrategyForExpectation($foundExpectation) |
|
67
|
|
|
->createResponse($foundExpectation, $response, $request); |
|
68
|
|
|
$this->logger->debug('Responding: ' . $this->getLoggableResponse($response)); |
|
69
|
|
|
|
|
70
|
|
|
return $response; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function searchForMatchingExpectation(ServerRequestInterface $request): ?Expectation |
|
74
|
|
|
{ |
|
75
|
|
|
$lastFound = null; |
|
76
|
|
|
foreach ($this->expectationsStorage->listExpectations() as $expectation) { |
|
77
|
|
|
$lastFound = $this->getNextMatchingExpectation($lastFound, $request, $expectation); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $lastFound; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function getNextMatchingExpectation(?Expectation $lastFound, ServerRequestInterface $request, Expectation $expectation): ?Expectation |
|
84
|
|
|
{ |
|
85
|
|
|
if (null === $lastFound || $expectation->getPriority() > $lastFound->getPriority()) { |
|
86
|
|
|
if ($this->comparator->equals($request, $expectation)) { |
|
87
|
|
|
$lastFound = $expectation; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $lastFound; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function getLoggableRequest(ServerRequestInterface $request): string |
|
95
|
|
|
{ |
|
96
|
|
|
$body = $request->getBody()->__toString(); |
|
97
|
|
|
$longBody = '--VERY LONG CONTENTS--'; |
|
98
|
|
|
$body = isset($body[2000]) ? $longBody : preg_replace('|\s+|', ' ', $body); |
|
99
|
|
|
|
|
100
|
|
|
return sprintf( |
|
101
|
|
|
'%s: %s || %s', |
|
102
|
|
|
$request->getMethod(), |
|
103
|
|
|
$request->getUri()->__toString(), |
|
104
|
|
|
$body |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function getLoggableResponse(ResponseInterface $response): string |
|
109
|
|
|
{ |
|
110
|
|
|
$body = $response->getBody()->__toString(); |
|
111
|
|
|
|
|
112
|
|
|
return sprintf( |
|
113
|
|
|
'%d / %s', |
|
114
|
|
|
$response->getStatusCode(), |
|
115
|
|
|
isset($body[2000]) ? '--VERY LONG CONTENTS--' : preg_replace('|\s+|', ' ', $body) |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|