@@ -37,98 +37,56 @@ public function assertHttpClientRequest(
37
37
array $ expectedHeaders = [],
38
38
string $ httpClientId = 'http_client ' ,
39
39
): 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 ;
67
42
68
43
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 )) {
72
45
continue ;
73
46
}
74
47
75
48
if ($ expectedBody !== null ) {
76
49
$ actualBody = null ;
77
-
78
50
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 ' ]);
83
54
}
84
55
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 ) {
93
57
continue ;
94
58
}
95
-
96
- if ($ expectedHeaders === []) {
97
- $ expectedRequestHasBeenFound = true ;
98
- break ;
99
- }
100
59
}
101
60
102
61
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 ;
111
73
}
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 ;
120
77
}
121
78
}
79
+ if (!$ allHeadersMatch ) {
80
+ continue ;
81
+ }
122
82
}
123
83
124
- if ($ expectedBody === null && $ expectedHeaders === []) {
125
- $ expectedRequestHasBeenFound = true ;
126
- break ;
127
- }
84
+ $ requestFound = true ;
85
+ break ;
128
86
}
129
87
130
88
$ this ->assertTrue (
131
- $ expectedRequestHasBeenFound ,
89
+ $ requestFound ,
132
90
sprintf ('The expected request has not been called: "%s" - "%s" ' , $ expectedMethod , $ expectedUrl )
133
91
);
134
92
}
@@ -146,18 +104,8 @@ public function assertHttpClientRequestCount(
146
104
int $ count ,
147
105
string $ httpClientId = 'http_client ' ,
148
106
): 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 );
161
109
}
162
110
163
111
/**
@@ -173,32 +121,70 @@ public function assertNotHttpClientRequest(
173
121
string $ expectedMethod = 'GET ' ,
174
122
string $ httpClientId = 'http_client ' ,
175
123
): 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 );
177
148
178
149
/**
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
180
156
*/
181
157
$ clients = $ httpClientCollector ->getClients ();
182
158
183
159
if (!array_key_exists ($ httpClientId , $ clients )) {
184
160
$ this ->fail (sprintf ('HttpClient "%s" is not registered. ' , $ httpClientId ));
185
161
}
186
162
187
- $ unexpectedUrlHasBeenFound = false ;
163
+ return $ clients [$ httpClientId ]['traces ' ];
164
+ }
188
165
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 ;
196
177
}
197
178
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 ;
202
188
}
203
189
204
190
protected function grabHttpClientCollector (string $ function ): HttpClientDataCollector
0 commit comments