Skip to content

Commit 6f84669

Browse files
committed
HttpClient PHPStan fix
1 parent 94484f0 commit 6f84669

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector;
88
use Symfony\Component\VarDumper\Cloner\Data;
99
use function array_change_key_case;
10+
use function array_filter;
1011
use function array_intersect_key;
1112
use function array_key_exists;
1213
use function in_array;
@@ -43,45 +44,29 @@ public function assertHttpClientRequest(
4344
array $expectedHeaders = [],
4445
string $httpClientId = 'http_client',
4546
): void {
46-
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
47-
$requestFound = false;
48-
49-
foreach ($traces as $trace) {
50-
if (!$this->matchesUrlAndMethod($trace, $expectedUrl, $expectedMethod)) {
51-
continue;
52-
}
53-
54-
$rawOptions = $trace['options'] ?? [];
55-
56-
if ($expectedBody !== null) {
57-
$actualBody = $this->extractValue(
58-
$rawOptions['body'] ?? $rawOptions['json'] ?? null
59-
);
60-
if ($expectedBody !== $actualBody) {
61-
continue;
47+
$matchingRequests = array_filter(
48+
$this->getHttpClientTraces($httpClientId, __FUNCTION__),
49+
function (array $trace) use ($expectedUrl, $expectedMethod, $expectedBody, $expectedHeaders): bool {
50+
if (!$this->matchesUrlAndMethod($trace, $expectedUrl, $expectedMethod)) {
51+
return false;
6252
}
63-
}
6453

65-
if ($expectedHeaders !== []) {
66-
$rawHeaders = $rawOptions['headers'] ?? [];
67-
$actualHeaders = $this->extractValue($rawHeaders);
68-
if (!is_array($actualHeaders)) {
69-
continue;
70-
}
54+
$options = $trace['options'] ?? [];
55+
$actualBody = $this->extractValue($options['body'] ?? $options['json'] ?? null);
56+
$bodyMatches = $expectedBody === null || $expectedBody === $actualBody;
7157

72-
$expected = array_change_key_case($expectedHeaders);
73-
$actual = array_change_key_case($actualHeaders);
74-
if ($expected !== array_intersect_key($actual, $expected)) {
75-
continue;
76-
}
77-
}
58+
$headersMatch = $expectedHeaders === [] || (
59+
is_array($headerValues = $this->extractValue($options['headers'] ?? []))
60+
&& ($normalizedExpected = array_change_key_case($expectedHeaders))
61+
=== array_intersect_key(array_change_key_case($headerValues), $normalizedExpected)
62+
);
7863

79-
$requestFound = true;
80-
break;
81-
}
64+
return $bodyMatches && $headersMatch;
65+
},
66+
);
8267

83-
$this->assertTrue(
84-
$requestFound,
68+
$this->assertNotEmpty(
69+
$matchingRequests,
8570
sprintf('The expected request has not been called: "%s" - "%s"', $expectedMethod, $expectedUrl)
8671
);
8772
}
@@ -97,8 +82,7 @@ public function assertHttpClientRequest(
9782
*/
9883
public function assertHttpClientRequestCount(int $count, string $httpClientId = 'http_client'): void
9984
{
100-
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
101-
$this->assertCount($count, $traces);
85+
$this->assertCount($count, $this->getHttpClientTraces($httpClientId, __FUNCTION__));
10286
}
10387

10488
/**
@@ -111,18 +95,18 @@ public function assertHttpClientRequestCount(int $count, string $httpClientId =
11195
*/
11296
public function assertNotHttpClientRequest(
11397
string $unexpectedUrl,
114-
string $expectedMethod = 'GET',
98+
string $unexpectedMethod = 'GET',
11599
string $httpClientId = 'http_client',
116100
): void {
117-
$traces = $this->getHttpClientTraces($httpClientId, __FUNCTION__);
118-
119-
foreach ($traces as $trace) {
120-
if ($this->matchesUrlAndMethod($trace, $unexpectedUrl, $expectedMethod)) {
121-
$this->fail(sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl));
122-
}
123-
}
101+
$matchingRequests = array_filter(
102+
$this->getHttpClientTraces($httpClientId, __FUNCTION__),
103+
fn(array $trace): bool => $this->matchesUrlAndMethod($trace, $unexpectedUrl, $unexpectedMethod)
104+
);
124105

125-
$this->assertTrue(true, 'The unexpected request was not made.');
106+
$this->assertEmpty(
107+
$matchingRequests,
108+
sprintf('Unexpected URL was called: "%s" - "%s"', $unexpectedMethod, $unexpectedUrl)
109+
);
126110
}
127111

128112
/**

0 commit comments

Comments
 (0)