Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/Http/Faking/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
21 changes: 16 additions & 5 deletions tests/Unit/MockClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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é']);
Expand All @@ -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([
Expand Down