Skip to content

Commit 118d911

Browse files
committed
HttpClient PHPStan fix
1 parent 6e6d709 commit 118d911

File tree

1 file changed

+81
-95
lines changed

1 file changed

+81
-95
lines changed

src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php

Lines changed: 81 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -37,98 +37,56 @@ 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-
*/
106-
$actualHeaders = $trace['options']['headers'] ?? [];
107-
108-
foreach ($actualHeaders as $headerKey => $actualHeaderValue) {
109-
if (!array_key_exists($headerKey, $expectedHeaders)) {
110-
continue;
62+
/** @var array<string, mixed> $actualHeaders */
63+
$rawHeaders = $trace['options']['headers'] ?? [];
64+
$actualHeaders = $this->extractValue($rawHeaders);
65+
if (!is_array($actualHeaders)) {
66+
continue;
67+
}
68+
$allHeadersMatch = true;
69+
foreach ($expectedHeaders as $headerKey => $expectedValue) {
70+
if (!array_key_exists($headerKey, $actualHeaders)) {
71+
$allHeadersMatch = false;
72+
break;
11173
}
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;
74+
if ($expectedValue !== $this->extractValue($actualHeaders[$headerKey])) {
75+
$allHeadersMatch = false;
76+
break;
12077
}
12178
}
79+
if (!$allHeadersMatch) {
80+
continue;
81+
}
12282
}
12383

124-
if ($expectedBody === null && $expectedHeaders === []) {
125-
$expectedRequestHasBeenFound = true;
126-
break;
127-
}
84+
$requestFound = true;
85+
break;
12886
}
12987

13088
$this->assertTrue(
131-
$expectedRequestHasBeenFound,
89+
$requestFound,
13290
sprintf('The expected request has not been called: "%s" - "%s"', $expectedMethod, $expectedUrl)
13391
);
13492
}
@@ -146,18 +104,8 @@ public function assertHttpClientRequestCount(
146104
int $count,
147105
string $httpClientId = 'http_client',
148106
): 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']);
107+
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
108+
$this->assertCount($count, $traces);
161109
}
162110

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

178149
/**
179-
* @var array<string, array{traces: list<array{info: array{url: string}, url: string, method: string}>}> $clients
150+
* @var array<string, array{traces: list<array{
151+
* info: array{url: string},
152+
* url: string,
153+
* method: string,
154+
* options: array{body?: mixed, json?: mixed, headers?: mixed}
155+
* }>}> $clients
180156
*/
181157
$clients = $httpClientCollector->getClients();
182158

183159
if (!array_key_exists($httpClientId, $clients)) {
184160
$this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId));
185161
}
186162

187-
$unexpectedUrlHasBeenFound = false;
163+
return $clients[$httpClientId]['traces'];
164+
}
188165

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-
}
166+
/** @param array{info: array{url: string}, url: string, method: string} $trace */
167+
private function matchesUrlAndMethod(array $trace, string $expectedUrl, string $expectedMethod): bool
168+
{
169+
return ($expectedUrl === $trace['info']['url'] || $expectedUrl === $trace['url'])
170+
&& $expectedMethod === $trace['method'];
171+
}
172+
173+
private function extractValue(mixed $value): mixed
174+
{
175+
if (is_string($value)) {
176+
return $value;
196177
}
197178

198-
$this->assertFalse(
199-
$unexpectedUrlHasBeenFound,
200-
sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl)
201-
);
179+
if ($value instanceof Data) {
180+
return $value->getValue(true);
181+
}
182+
183+
if (is_object($value) && method_exists($value, 'getValue')) {
184+
return $value->getValue(true);
185+
}
186+
187+
return $value;
202188
}
203189

204190
protected function grabHttpClientCollector(string $function): HttpClientDataCollector

0 commit comments

Comments
 (0)