1313use InvalidArgumentException ;
1414use Mantle \Contracts \Support \Arrayable ;
1515use Mantle \Http_Client \Request ;
16+ use Mantle \Support \Arr ;
1617use Mantle \Support \Collection ;
1718use Mantle \Support \Str ;
1819use Mantle \Testing \Mock_Http_Response ;
@@ -53,6 +54,13 @@ trait Interacts_With_Requests {
5354 */
5455 protected mixed $ preventing_stray_requests = false ;
5556
57+ /**
58+ * Stray requests that should be ignored (not reported).
59+ *
60+ * @var Collection<int, string>
61+ */
62+ protected Collection $ ignored_strayed_requests ;
63+
5664 /**
5765 * Recorded actual HTTP requests made during the test.
5866 *
@@ -66,6 +74,7 @@ trait Interacts_With_Requests {
6674 public function interacts_with_requests_set_up (): void {
6775 $ this ->stub_callbacks = collect ();
6876 $ this ->recorded_requests = collect ();
77+ $ this ->ignored_strayed_requests = collect ();
6978 $ this ->recorded_actual_requests = collect ();
7079
7180 \add_filter ( 'pre_http_request ' , [ $ this , 'pre_http_request ' ], PHP_INT_MAX , 3 );
@@ -96,6 +105,15 @@ public function allow_stray_requests(): void {
96105 $ this ->preventing_stray_requests = false ;
97106 }
98107
108+ /**
109+ * Ignore a stray request.
110+ *
111+ * @param array<string>|string $url URL to ignore. Supports wildcard matching with *.
112+ */
113+ public function ignore_stray_request ( array |string $ url ): void {
114+ $ this ->ignored_strayed_requests = $ this ->ignored_strayed_requests ->merge ( $ url );
115+ }
116+
99117 /**
100118 * Fake a remote request.
101119 *
@@ -306,6 +324,11 @@ protected function get_stub_response( string $url, array $request_args ): array|
306324 return $ prevent ->to_array ();
307325 }
308326
327+ // Check if the stray request should be ignored.
328+ if ( $ this ->ignored_strayed_requests ->contains ( fn ( $ ignored_url ) => Str::is ( $ ignored_url , $ url ) ) ) {
329+ return null ;
330+ }
331+
309332 throw new RuntimeException ( "Attempted request to [ {$ url }] without a matching fake. " );
310333 }
311334
0 commit comments