Skip to content

Commit ceca372

Browse files
committed
Fix | MockClient assertions with fixtures
1 parent a39a59d commit ceca372

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Http/Middleware/DetermineMockResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __invoke(PendingRequest $pendingRequest): PendingRequest
5353
// middleware on the response to record the response.
5454

5555
if (is_null($mockResponse) && $mockObject instanceof Fixture) {
56-
$pendingRequest->middleware()->onResponse(new RecordFixture($mockObject), 'recordFixture', PipeOrder::FIRST);
56+
$pendingRequest->middleware()->onResponse(new RecordFixture($mockObject, $mockClient), 'recordFixture', PipeOrder::FIRST);
5757
}
5858

5959
return $pendingRequest;

src/Http/Middleware/RecordFixture.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Saloon\Http\Response;
88
use Saloon\Http\Faking\Fixture;
99
use Saloon\Data\RecordedResponse;
10+
use Saloon\Http\Faking\MockClient;
1011
use Saloon\Contracts\ResponseMiddleware;
1112

1213
class RecordFixture implements ResponseMiddleware
@@ -16,12 +17,18 @@ class RecordFixture implements ResponseMiddleware
1617
*/
1718
protected Fixture $fixture;
1819

20+
/**
21+
* Mock Client
22+
*/
23+
protected MockClient $mockClient;
24+
1925
/**
2026
* Constructor
2127
*/
22-
public function __construct(Fixture $fixture)
28+
public function __construct(Fixture $fixture, MockClient $mockClient)
2329
{
2430
$this->fixture = $fixture;
31+
$this->mockClient = $mockClient;
2532
}
2633

2734
/**
@@ -37,5 +44,7 @@ public function __invoke(Response $response): void
3744
$this->fixture->store(
3845
RecordedResponse::fromResponse($response)
3946
);
47+
48+
$this->mockClient->recordResponse($response);
4049
}
4150
}

tests/Feature/MockRequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,13 @@ function (PendingRequest $pendingRequest): MockResponse {
729729
expect($middlewareC)->toBeTrue();
730730
expect($middlewareD)->toBeTrue();
731731
});
732+
733+
test('fixtures are still recorded on the first request', function () {
734+
$mockClient = new MockClient([
735+
MockResponse::fixture('user'), // Test Exact Route
736+
]);
737+
738+
connector()->send(new UserRequest, $mockClient);
739+
740+
$mockClient->assertSent(UserRequest::class);
741+
});

0 commit comments

Comments
 (0)