Total Complexity | 41 |
Total Lines | 195 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RequestExpectationComparator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestExpectationComparator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class RequestExpectationComparator |
||
29 | { |
||
30 | /** @var ScenarioStorage */ |
||
31 | private $scenarioStorage; |
||
32 | /** @var LoggerInterface */ |
||
33 | private $logger; |
||
34 | |||
35 | public function __construct( |
||
41 | } |
||
42 | |||
43 | public function equals(ServerRequestInterface $httpRequest, Expectation $expectation): bool |
||
57 | } |
||
58 | |||
59 | private function compareRequestParts(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
60 | { |
||
61 | return $this->requestMethodMatchesExpectation($httpRequest, $expectedRequest) |
||
62 | && $this->requestUrlMatchesExpectation($httpRequest, $expectedRequest) |
||
63 | && $this->requestBodyMatchesExpectation($httpRequest, $expectedRequest) |
||
64 | && $this->requestHeadersMatchExpectation($httpRequest, $expectedRequest) |
||
65 | && $this->requestFormDataMatchExpectation($httpRequest, $expectedRequest) |
||
66 | && $this->requestJsonMatchesExpectation($httpRequest, $expectedRequest); |
||
67 | } |
||
68 | |||
69 | private function isExpectedScenarioState(Expectation $expectation): bool |
||
70 | { |
||
71 | if ($expectation->getRequest()->hasScenarioState()) { |
||
72 | $this->checkScenarioNameOrThrowException($expectation); |
||
73 | $this->logger->debug('Checking scenario state again expectation'); |
||
74 | $scenarioState = $this->scenarioStorage->getScenarioState( |
||
75 | $expectation->getScenarioName() |
||
76 | ); |
||
77 | if (!$expectation->getRequest()->getScenarioState()->equals($scenarioState)) { |
||
78 | return false; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return true; |
||
83 | } |
||
84 | |||
85 | /** @throws InvalidArgumentException */ |
||
86 | private function checkScenarioNameOrThrowException(Expectation $expectation) |
||
87 | { |
||
88 | if (!$expectation->hasScenarioName()) { |
||
89 | throw new InvalidArgumentException('Expecting scenario state without specifying scenario name'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | private function requestMethodMatchesExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
94 | { |
||
95 | $method = $expectedRequest->getMethod(); |
||
96 | if (!$method) { |
||
97 | return true; |
||
98 | } |
||
99 | $this->logger->debug('Checking METHOD against expectation'); |
||
100 | |||
101 | return $method->getMatcher()->matches($httpRequest->getMethod()); |
||
102 | } |
||
103 | |||
104 | private function requestUrlMatchesExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
105 | { |
||
106 | $url = $expectedRequest->getUrl(); |
||
107 | if (!$url) { |
||
108 | return true; |
||
109 | } |
||
110 | $this->logger->debug('Checking URL against expectation'); |
||
111 | |||
112 | $requestUrl = $this->getComparableRequestUrl($httpRequest); |
||
113 | |||
114 | return $url->getMatcher()->matches($requestUrl); |
||
115 | } |
||
116 | |||
117 | private function getComparableRequestUrl($httpRequest) |
||
118 | { |
||
119 | $requestUrl = $httpRequest->getUri()->getPath(); |
||
120 | if ($httpRequest->getUri()->getQuery()) { |
||
121 | $requestUrl .= '?' . $httpRequest->getUri()->getQuery(); |
||
122 | } |
||
123 | if ($httpRequest->getUri()->getFragment()) { |
||
124 | $requestUrl .= '#' . $httpRequest->getUri()->getFragment(); |
||
125 | } |
||
126 | |||
127 | return $requestUrl; |
||
128 | } |
||
129 | |||
130 | private function requestBodyMatchesExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
131 | { |
||
132 | $bodycondition = $expectedRequest->getBody(); |
||
133 | if (!$bodycondition) { |
||
134 | return true; |
||
135 | } |
||
136 | $this->logger->debug('Checking BODY against expectation'); |
||
137 | |||
138 | return $bodycondition->getMatcher()->matches($httpRequest->getBody()->__toString()); |
||
139 | } |
||
140 | |||
141 | private function requestHeadersMatchExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
142 | { |
||
143 | $headerConditions = $expectedRequest->getHeaders(); |
||
144 | if (!$headerConditions) { |
||
145 | return true; |
||
146 | } |
||
147 | $this->logger->debug('Checking HEADERS against expectation'); |
||
148 | foreach ($headerConditions as $header => $headerCondition) { |
||
149 | $headerName = $header->asString(); |
||
150 | $this->logger->debug("Checking $headerName against expectation"); |
||
151 | |||
152 | $matches = $headerCondition->getMatcher()->matches( |
||
153 | $httpRequest->getHeaderLine($headerName) |
||
154 | ); |
||
155 | if (!$matches) { |
||
156 | return false; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return true; |
||
161 | } |
||
162 | |||
163 | private function requestFormDataMatchExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
187 | } |
||
188 | |||
189 | private function requestJsonMatchesExpectation(ServerRequestInterface $httpRequest, Conditions $expectedRequest): bool |
||
223 | } |
||
224 | } |
||
225 |