@@ -37,98 +37,52 @@ 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
- */
62
+ /** @var array<string, mixed> $actualHeaders */
106
63
$ 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 ;
111
69
}
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 ;
120
73
}
121
74
}
75
+ if (!$ allHeadersMatch ) {
76
+ continue ;
77
+ }
122
78
}
123
79
124
- if ($ expectedBody === null && $ expectedHeaders === []) {
125
- $ expectedRequestHasBeenFound = true ;
126
- break ;
127
- }
80
+ $ requestFound = true ;
81
+ break ;
128
82
}
129
83
130
84
$ this ->assertTrue (
131
- $ expectedRequestHasBeenFound ,
85
+ $ requestFound ,
132
86
sprintf ('The expected request has not been called: "%s" - "%s" ' , $ expectedMethod , $ expectedUrl )
133
87
);
134
88
}
@@ -146,18 +100,8 @@ public function assertHttpClientRequestCount(
146
100
int $ count ,
147
101
string $ httpClientId = 'http_client ' ,
148
102
): 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 );
161
105
}
162
106
163
107
/**
@@ -173,32 +117,70 @@ public function assertNotHttpClientRequest(
173
117
string $ expectedMethod = 'GET ' ,
174
118
string $ httpClientId = 'http_client ' ,
175
119
): 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 );
177
144
178
145
/**
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
180
152
*/
181
153
$ clients = $ httpClientCollector ->getClients ();
182
154
183
155
if (!array_key_exists ($ httpClientId , $ clients )) {
184
156
$ this ->fail (sprintf ('HttpClient "%s" is not registered. ' , $ httpClientId ));
185
157
}
186
158
187
- $ unexpectedUrlHasBeenFound = false ;
159
+ return $ clients [$ httpClientId ]['traces ' ];
160
+ }
188
161
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 ;
196
173
}
197
174
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 ;
202
184
}
203
185
204
186
protected function grabHttpClientCollector (string $ function ): HttpClientDataCollector
0 commit comments