Skip to content

Commit 67ab3d7

Browse files
committed
Refactor HTTP testing API and improve header handling
- Remove testing assertion methods from Http class and move to TestingHttpHandler - Add comprehensive HttpTestingAssistant class for dedicated testing functionality - Update test files to use TestingHttpHandler assertions directly - Fix MockedRequest header handling to support multiple values for same header name - Improve code organization by separating testing concerns from main Http API
1 parent 5c3cea5 commit 67ab3d7

File tree

4 files changed

+505
-79
lines changed

4 files changed

+505
-79
lines changed

src/Api/Http.php

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,20 @@ public static function getTestingHandler(): TestingHttpHandler
155155
}
156156

157157
/**
158-
* Check if Http is currently in testing mode.
158+
* Convenience method to quickly mock a request in testing mode.
159+
* Follows Laravel's Http::fake() pattern.
160+
*
161+
* @param string $method HTTP method to mock (default: '*' for any)
159162
*
160-
* @return bool True if in testing mode, false otherwise
163+
* @throws \RuntimeException If not in testing mode
161164
*/
162-
public static function isTesting(): bool
165+
public static function mock(string $method = '*'): MockRequestBuilder
163166
{
164-
return self::$isTesting;
167+
if (! self::$isTesting || self::$testingInstance === null) {
168+
throw new \RuntimeException('Not in testing mode. Call Http::testing() first.');
169+
}
170+
171+
return self::$testingInstance->mock($method);
165172
}
166173

167174
/**
@@ -202,73 +209,6 @@ public static function setInstance(HttpHandler $handler): void
202209
}
203210
}
204211

205-
/**
206-
* Convenience method to quickly mock a request in testing mode.
207-
*
208-
* @param string $method HTTP method to mock (default: '*' for any)
209-
*
210-
* @throws \RuntimeException If not in testing mode
211-
*/
212-
public static function mock(string $method = '*'): MockRequestBuilder
213-
{
214-
if (! self::$isTesting || self::$testingInstance === null) {
215-
throw new \RuntimeException('Not in testing mode. Call Http::testing() first.');
216-
}
217-
218-
return self::$testingInstance->mock($method);
219-
}
220-
221-
/**
222-
* Assert that a specific request was made during testing.
223-
*
224-
* @param string $method HTTP method
225-
* @param string $url URL pattern
226-
* @param array<string, mixed> $options Additional matching options
227-
*
228-
* @throws \RuntimeException If not in testing mode
229-
* @throws \Exception If assertion fails
230-
*/
231-
public static function assertRequestMade(string $method, string $url, array $options = []): void
232-
{
233-
if (! self::$isTesting || self::$testingInstance === null) {
234-
throw new \RuntimeException('Not in testing mode. Call Http::testing() first.');
235-
}
236-
237-
self::$testingInstance->assertRequestMade($method, $url, $options);
238-
}
239-
240-
/**
241-
* Assert that no HTTP requests were made during testing.
242-
*
243-
* @throws \RuntimeException If not in testing mode
244-
* @throws \Exception If assertion fails
245-
*/
246-
public static function assertNoRequestsMade(): void
247-
{
248-
if (! self::$isTesting || self::$testingInstance === null) {
249-
throw new \RuntimeException('Not in testing mode. Call Http::testing() first.');
250-
}
251-
252-
self::$testingInstance->assertNoRequestsMade();
253-
}
254-
255-
/**
256-
* Assert a specific number of requests were made during testing.
257-
*
258-
* @param int $expected Expected number of requests
259-
*
260-
* @throws \RuntimeException If not in testing mode
261-
* @throws \Exception If assertion fails
262-
*/
263-
public static function assertRequestCount(int $expected): void
264-
{
265-
if (! self::$isTesting || self::$testingInstance === null) {
266-
throw new \RuntimeException('Not in testing mode. Call Http::testing() first.');
267-
}
268-
269-
self::$testingInstance->assertRequestCount($expected);
270-
}
271-
272212
/**
273213
* Magic method to handle dynamic static calls.
274214
*
@@ -298,6 +238,6 @@ public static function __callStatic(string $method, array $arguments)
298238
return $handler->{$method}(...$arguments);
299239
}
300240

301-
throw new \BadMethodCallException("Method {$method} does not exist on ".static::class);
241+
throw new \BadMethodCallException("Method {$method} does not exist on " . static::class);
302242
}
303243
}

0 commit comments

Comments
 (0)