|
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\Traits; |
|
20
|
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
|
22
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
|
|
25
|
|
|
trait ExpectationValidator |
|
26
|
|
|
{ |
|
27
|
|
|
/** @throws \RuntimeException */ |
|
28
|
|
|
protected function validateExpectationOrThrowException(Expectation $expectation, LoggerInterface $logger) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->validateResponseOrThrowException($expectation, $logger); |
|
31
|
|
|
$this->validateScenarioNameOrThrowException($expectation, $logger); |
|
32
|
|
|
$this->validateScenarioStateOrThrowException($expectation, $logger); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** @throws \RuntimeException */ |
|
36
|
|
|
protected function validateResponseOrThrowException(Expectation $expectation, LoggerInterface $logger) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->logger->debug('Validating response'); |
|
39
|
|
|
if ($this->responseIsInvalid($expectation)) { |
|
40
|
|
|
$logger->error('Invalid response specified in expectation'); |
|
41
|
|
|
throw new \RuntimeException('Invalid response specified in expectation'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function responseIsInvalid(Expectation $expectation): bool |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var HttpResponse $response */ |
|
48
|
|
|
$response = $expectation->getResponse(); |
|
49
|
|
|
|
|
50
|
|
|
return $response->isHttpResponse() && empty($response->getStatusCode()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @throws \RuntimeException */ |
|
54
|
|
|
protected function validateScenarioStateOrThrowException( |
|
55
|
|
|
Expectation $expectation, |
|
56
|
|
|
LoggerInterface $logger |
|
57
|
|
|
): void { |
|
58
|
|
|
if ($expectation->getResponse()->hasNewScenarioState() && !$expectation->getRequest()->hasScenarioState()) { |
|
59
|
|
|
$logger->error('Scenario states misconfiguration'); |
|
60
|
|
|
throw new \RuntimeException('Trying to set scenario state without specifying scenario previous state'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @throws \RuntimeException */ |
|
65
|
|
|
protected function validateScenarioNameOrThrowException( |
|
66
|
|
|
Expectation $expectation, |
|
67
|
|
|
LoggerInterface $logger |
|
68
|
|
|
): void { |
|
69
|
|
|
if (!$expectation->hasScenarioName() |
|
70
|
|
|
&& ($expectation->getRequest()->hasScenarioState() || $expectation->getResponse()->hasNewScenarioState()) |
|
71
|
|
|
) { |
|
72
|
|
|
$logger->error('Scenario name related misconfiguration'); |
|
73
|
|
|
throw new \RuntimeException('Expecting or trying to set scenario state without specifying scenario name'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|