From 23d6d023b3b2c3346c5c92b2a22b6ef40ae55eae Mon Sep 17 00:00:00 2001 From: Erich Oelschlegel Date: Mon, 23 Jun 2025 10:18:12 -0400 Subject: [PATCH] allow partial mocking --- src/Http/Faking/MockClient.php | 31 +++++++++++++++++++++++++++---- tests/Unit/MockClientTest.php | 21 ++++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Http/Faking/MockClient.php b/src/Http/Faking/MockClient.php index 15cba44c..900be1e4 100644 --- a/src/Http/Faking/MockClient.php +++ b/src/Http/Faking/MockClient.php @@ -59,6 +59,13 @@ class MockClient */ protected static ?MockClient $globalMockClient = null; + /** + * Whether to allow sending an actual request if no mock response is found. + * + * @var bool + */ + protected bool $allowFallback = false; + /** * Constructor * @@ -69,6 +76,16 @@ public function __construct(array $mockData = []) $this->addResponses($mockData); } + /** + * Allow sending an actual request if no mock response is found. + */ + public function allowFallback(bool $allowFallback = true): static + { + $this->allowFallback = $allowFallback; + + return $this; + } + /** * Store the mock responses in the correct places. * @@ -137,7 +154,7 @@ public function getNextFromSequence(): mixed * * @throws \Saloon\Exceptions\NoMockResponseFoundException */ - public function guessNextResponse(PendingRequest $pendingRequest): MockResponse|Fixture + public function guessNextResponse(PendingRequest $pendingRequest): MockResponse|Fixture|PendingRequest { $request = $pendingRequest->getRequest(); $requestClass = get_class($request); @@ -158,11 +175,17 @@ public function guessNextResponse(PendingRequest $pendingRequest): MockResponse| return $this->mockResponseValue($guessedResponse, $pendingRequest); } - if (empty($this->sequenceResponses)) { - throw new NoMockResponseFoundException($pendingRequest); + $sequenceResponse = $this->getNextFromSequence(); + + if (! empty($sequenceResponse)) { + return $this->mockResponseValue($sequenceResponse, $pendingRequest); + } + + if ($this->allowFallback) { + return $pendingRequest; } - return $this->mockResponseValue($this->getNextFromSequence(), $pendingRequest); + throw new NoMockResponseFoundException($pendingRequest); } /** diff --git a/tests/Unit/MockClientTest.php b/tests/Unit/MockClientTest.php index f542bee0..7fb40853 100644 --- a/tests/Unit/MockClientTest.php +++ b/tests/Unit/MockClientTest.php @@ -111,7 +111,7 @@ expect($mockClient->guessNextResponse($connectorB->createPendingRequest($requestC)))->toEqual($responseC); }); -test('saloon throws an exception if it cant work out the url response', function () { +test('saloon throws an exception if it cant work out the url response', function (bool $allowFallback) { $responseA = MockResponse::make(['name' => 'Sammyjo20']); $responseB = MockResponse::make(['name' => 'Alex']); $responseC = MockResponse::make(['name' => 'Sam Carré']); @@ -128,14 +128,25 @@ 'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes ]); + $mockClient->allowFallback($allowFallback); + expect($mockClient->guessNextResponse($connectorA->createPendingRequest($requestA)))->toEqual($responseA); expect($mockClient->guessNextResponse($connectorA->createPendingRequest($requestB)))->toEqual($responseB); - $this->expectException(NoMockResponseFoundException::class); - $this->expectExceptionMessage('Saloon was unable to guess a mock response for your request [https://google.com/user], consider using a wildcard url mock or a connector mock.'); + if ($allowFallback) { + $pendingRequest = $connectorB->createPendingRequest($requestC); - expect($mockClient->guessNextResponse($connectorB->createPendingRequest($requestC)))->toEqual($responseC); -}); + expect($mockClient->guessNextResponse($pendingRequest))->toEqual($pendingRequest); + } else { + $this->expectException(NoMockResponseFoundException::class); + $this->expectExceptionMessage('Saloon was unable to guess a mock response for your request [https://google.com/user], consider using a wildcard url mock or a connector mock.'); + + expect($mockClient->guessNextResponse($connectorB->createPendingRequest($requestC)))->toEqual($responseC); + } +})->with([ + true, + false, +]); test('you can get an array of the recorded requests', function () { $mockClient = new MockClient([