Skip to content

Commit 095aa5f

Browse files
committed
HttpClient PHPStan fix
1 parent 6e6d709 commit 095aa5f

File tree

1 file changed

+76
-94
lines changed

1 file changed

+76
-94
lines changed

src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php

Lines changed: 76 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -37,98 +37,52 @@ public function assertHttpClientRequest(
3737
array $expectedHeaders = [],
3838
string $httpClientId = 'http_client',
3939
): void {
40-
$httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__);
41-
42-
/**
43-
* @var array<string, array{traces: list<array{
44-
* info: array{url: string},
45-
* url: string,
46-
* method: string,
47-
* options: array{body: mixed, json: mixed, headers?: mixed}
48-
* }>} > $clients
49-
*/
50-
$clients = $httpClientCollector->getClients();
51-
52-
if (!array_key_exists($httpClientId, $clients)) {
53-
$this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId));
54-
}
55-
56-
/**
57-
* @var list<array{
58-
* info: array{url: string},
59-
* url: string,
60-
* method: string,
61-
* options: array{body: mixed, json: mixed, headers?: mixed}
62-
* }> $traces
63-
*/
64-
$traces = $clients[$httpClientId]['traces'];
65-
66-
$expectedRequestHasBeenFound = false;
40+
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
41+
$requestFound = false;
6742

6843
foreach ($traces as $trace) {
69-
if (($expectedUrl !== $trace['info']['url'] && $expectedUrl !== $trace['url'])
70-
|| $expectedMethod !== $trace['method']
71-
) {
44+
if (!$this->matchesUrlAndMethod($trace, $expectedUrl, $expectedMethod)) {
7245
continue;
7346
}
7447

7548
if ($expectedBody !== null) {
7649
$actualBody = null;
77-
7850
if (isset($trace['options']['body']) && !isset($trace['options']['json'])) {
79-
$body = $trace['options']['body'];
80-
$actualBody = is_string($body)
81-
? $body
82-
: ($body instanceof Data ? $body->getValue(true) : null);
51+
$actualBody = $this->extractValue($trace['options']['body']);
52+
} elseif (!isset($trace['options']['body']) && isset($trace['options']['json'])) {
53+
$actualBody = $this->extractValue($trace['options']['json']);
8354
}
8455

85-
if (!isset($trace['options']['body']) && isset($trace['options']['json'])) {
86-
$json = $trace['options']['json'];
87-
$actualBody = is_string($json)
88-
? $json
89-
: ($json instanceof Data ? $json->getValue(true) : null);
90-
}
91-
92-
if ($actualBody === null || $expectedBody !== $actualBody) {
56+
if ($expectedBody !== $actualBody) {
9357
continue;
9458
}
95-
96-
if ($expectedHeaders === []) {
97-
$expectedRequestHasBeenFound = true;
98-
break;
99-
}
10059
}
10160

10261
if ($expectedHeaders !== []) {
103-
/**
104-
* @var array<string, mixed> $actualHeaders
105-
*/
62+
/** @var array<string, mixed> $actualHeaders */
10663
$actualHeaders = $trace['options']['headers'] ?? [];
107-
108-
foreach ($actualHeaders as $headerKey => $actualHeaderValue) {
109-
if (!array_key_exists($headerKey, $expectedHeaders)) {
110-
continue;
64+
$allHeadersMatch = true;
65+
foreach ($expectedHeaders as $headerKey => $expectedValue) {
66+
if (!array_key_exists($headerKey, $actualHeaders)) {
67+
$allHeadersMatch = false;
68+
break;
11169
}
112-
113-
$actualHeaderValue = is_object($actualHeaderValue) && method_exists($actualHeaderValue, 'getValue')
114-
? $actualHeaderValue->getValue(true)
115-
: $actualHeaderValue;
116-
117-
if ($expectedHeaders[$headerKey] === $actualHeaderValue) {
118-
$expectedRequestHasBeenFound = true;
119-
break 2;
70+
if ($expectedValue !== $this->extractValue($actualHeaders[$headerKey])) {
71+
$allHeadersMatch = false;
72+
break;
12073
}
12174
}
75+
if (!$allHeadersMatch) {
76+
continue;
77+
}
12278
}
12379

124-
if ($expectedBody === null && $expectedHeaders === []) {
125-
$expectedRequestHasBeenFound = true;
126-
break;
127-
}
80+
$requestFound = true;
81+
break;
12882
}
12983

13084
$this->assertTrue(
131-
$expectedRequestHasBeenFound,
85+
$requestFound,
13286
sprintf('The expected request has not been called: "%s" - "%s"', $expectedMethod, $expectedUrl)
13387
);
13488
}
@@ -146,18 +100,8 @@ public function assertHttpClientRequestCount(
146100
int $count,
147101
string $httpClientId = 'http_client',
148102
): void {
149-
$httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__);
150-
151-
/**
152-
* @var array<string, array{traces: list<mixed>}> $clients
153-
*/
154-
$clients = $httpClientCollector->getClients();
155-
156-
if (!array_key_exists($httpClientId, $clients)) {
157-
$this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId));
158-
}
159-
160-
$this->assertCount($count, $clients[$httpClientId]['traces']);
103+
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
104+
$this->assertCount($count, $traces);
161105
}
162106

163107
/**
@@ -173,32 +117,70 @@ public function assertNotHttpClientRequest(
173117
string $expectedMethod = 'GET',
174118
string $httpClientId = 'http_client',
175119
): void {
176-
$httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__);
120+
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
121+
122+
foreach ($traces as $trace) {
123+
if ($this->matchesUrlAndMethod($trace, $unexpectedUrl, $expectedMethod)) {
124+
$this->fail(
125+
sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl)
126+
);
127+
}
128+
}
129+
130+
$this->assertTrue(true, 'The unexpected request was not made.');
131+
}
132+
133+
/**
134+
* @return list<array{
135+
* info: array{url: string},
136+
* url: string,
137+
* method: string,
138+
* options: array{body?: mixed, json?: mixed, headers?: mixed}
139+
* }>
140+
*/
141+
private function getHttpClientTraces(string $httpClientId, string $function): array
142+
{
143+
$httpClientCollector = $this->grabHttpClientCollector($function);
177144

178145
/**
179-
* @var array<string, array{traces: list<array{info: array{url: string}, url: string, method: string}>}> $clients
146+
* @var array<string, array{traces: list<array{
147+
* info: array{url: string},
148+
* url: string,
149+
* method: string,
150+
* options: array{body?: mixed, json?: mixed, headers?: mixed}
151+
* }>}> $clients
180152
*/
181153
$clients = $httpClientCollector->getClients();
182154

183155
if (!array_key_exists($httpClientId, $clients)) {
184156
$this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId));
185157
}
186158

187-
$unexpectedUrlHasBeenFound = false;
159+
return $clients[$httpClientId]['traces'];
160+
}
188161

189-
foreach ($clients[$httpClientId]['traces'] as $trace) {
190-
if (($unexpectedUrl === $trace['info']['url'] || $unexpectedUrl === $trace['url'])
191-
&& $expectedMethod === $trace['method']
192-
) {
193-
$unexpectedUrlHasBeenFound = true;
194-
break;
195-
}
162+
/** @param array{info: array{url: string}, url: string, method: string} $trace */
163+
private function matchesUrlAndMethod(array $trace, string $expectedUrl, string $expectedMethod): bool
164+
{
165+
return ($expectedUrl === $trace['info']['url'] || $expectedUrl === $trace['url'])
166+
&& $expectedMethod === $trace['method'];
167+
}
168+
169+
private function extractValue(mixed $value): mixed
170+
{
171+
if (is_string($value)) {
172+
return $value;
196173
}
197174

198-
$this->assertFalse(
199-
$unexpectedUrlHasBeenFound,
200-
sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl)
201-
);
175+
if ($value instanceof Data) {
176+
return $value->getValue(true);
177+
}
178+
179+
if (is_object($value) && method_exists($value, 'getValue')) {
180+
return $value->getValue(true);
181+
}
182+
183+
return $value;
202184
}
203185

204186
protected function grabHttpClientCollector(string $function): HttpClientDataCollector

0 commit comments

Comments
 (0)