7
7
use Symfony \Component \HttpClient \DataCollector \HttpClientDataCollector ;
8
8
use Symfony \Component \VarDumper \Cloner \Data ;
9
9
use function array_change_key_case ;
10
+ use function array_filter ;
10
11
use function array_intersect_key ;
11
12
use function array_key_exists ;
12
13
use function in_array ;
@@ -43,45 +44,29 @@ public function assertHttpClientRequest(
43
44
array $ expectedHeaders = [],
44
45
string $ httpClientId = 'http_client ' ,
45
46
): 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 ;
62
52
}
63
- }
64
53
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 ;
71
57
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
+ );
78
63
79
- $ requestFound = true ;
80
- break ;
81
- }
64
+ return $ bodyMatches && $ headersMatch ;
65
+ },
66
+ );
82
67
83
- $ this ->assertTrue (
84
- $ requestFound ,
68
+ $ this ->assertNotEmpty (
69
+ $ matchingRequests ,
85
70
sprintf ('The expected request has not been called: "%s" - "%s" ' , $ expectedMethod , $ expectedUrl )
86
71
);
87
72
}
@@ -97,8 +82,7 @@ public function assertHttpClientRequest(
97
82
*/
98
83
public function assertHttpClientRequestCount (int $ count , string $ httpClientId = 'http_client ' ): void
99
84
{
100
- $ traces = $ this ->getHttpClientTraces ($ httpClientId , __FUNCTION__ );
101
- $ this ->assertCount ($ count , $ traces );
85
+ $ this ->assertCount ($ count , $ this ->getHttpClientTraces ($ httpClientId , __FUNCTION__ ));
102
86
}
103
87
104
88
/**
@@ -111,18 +95,18 @@ public function assertHttpClientRequestCount(int $count, string $httpClientId =
111
95
*/
112
96
public function assertNotHttpClientRequest (
113
97
string $ unexpectedUrl ,
114
- string $ expectedMethod = 'GET ' ,
98
+ string $ unexpectedMethod = 'GET ' ,
115
99
string $ httpClientId = 'http_client ' ,
116
100
): 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
+ );
124
105
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
+ );
126
110
}
127
111
128
112
/**
0 commit comments