Skip to content

Commit b36bb7a

Browse files
authored
Merge pull request #327 from saloonphp/fix/v2-mock-client-assertions-with-fixtures
Fix | V2 - MockClient assertions with fixtures
2 parents 78c804a + daea88c commit b36bb7a

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Http/Middleware/DetermineMockResponse.php

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

5353
if (is_null($mockResponse) && $mockObject instanceof Fixture) {
54-
$pendingRequest->middleware()->onResponse(new RecordFixture($mockObject), true, 'recordFixture');
54+
$pendingRequest->middleware()->onResponse(new RecordFixture($mockObject, $mockClient), true, 'recordFixture');
5555
}
5656

5757
return $pendingRequest;

src/Http/Middleware/RecordFixture.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Saloon\Contracts\Response;
88
use Saloon\Http\Faking\Fixture;
9+
use Saloon\Contracts\MockClient;
910
use Saloon\Helpers\ResponseRecorder;
1011
use Saloon\Contracts\ResponseMiddleware;
1112

@@ -18,14 +19,21 @@ class RecordFixture implements ResponseMiddleware
1819
*/
1920
protected Fixture $fixture;
2021

22+
/**
23+
* Mock Client
24+
*/
25+
protected MockClient $mockClient;
26+
2127
/**
2228
* Constructor
2329
*
2430
* @param \Saloon\Http\Faking\Fixture $fixture
31+
* @param \Saloon\Contracts\MockClient $mockClient
2532
*/
26-
public function __construct(Fixture $fixture)
33+
public function __construct(Fixture $fixture, MockClient $mockClient)
2734
{
2835
$this->fixture = $fixture;
36+
$this->mockClient = $mockClient;
2937
}
3038

3139
/**
@@ -34,6 +42,7 @@ public function __construct(Fixture $fixture)
3442
* @param \Saloon\Contracts\Response $response
3543
* @return void
3644
* @throws \JsonException
45+
* @throws \Saloon\Exceptions\FixtureException
3746
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
3847
* @throws \Saloon\Exceptions\UnableToCreateFileException
3948
*/
@@ -42,5 +51,7 @@ public function __invoke(Response $response): void
4251
$this->fixture->store(
4352
ResponseRecorder::record($response)
4453
);
54+
55+
$this->mockClient->recordResponse($response);
4556
}
4657
}

tests/Feature/MockRequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,3 +687,13 @@ function (PendingRequest $pendingRequest): MockResponse {
687687
'twitter' => '**REDACTED-TWITTER**',
688688
], JSON_THROW_ON_ERROR));
689689
});
690+
691+
test('fixtures are still recorded on the first request', function () {
692+
$mockClient = new MockClient([
693+
MockResponse::fixture('user'), // Test Exact Route
694+
]);
695+
696+
connector()->send(new UserRequest, $mockClient);
697+
698+
$mockClient->assertSent(UserRequest::class);
699+
});

0 commit comments

Comments
 (0)