Skip to content

Commit

Permalink
Added test for fake middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammyjo20 committed Sep 3, 2023
1 parent 3657ac7 commit 5002893
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/Feature/MockRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,44 @@ function (PendingRequest $pendingRequest): MockResponse {
'twitter' => '**REDACTED-TWITTER**',
], JSON_THROW_ON_ERROR));
});

test('request and response middleware is invoked when using fake responses', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam'], 200, ['X-Foo' => 'Bar']),
MockResponse::make(['name' => 'Alex']),
MockResponse::make(['error' => 'Server Unavailable'], 500),
]);

$middlewareA = false;
$middlewareB = false;
$middlewareC = false;
$middlewareD = false;

$connector = new TestConnector;
$connector->withMockClient($mockClient);

$request = new UserRequest;

$connector->middleware()->onRequest(function () use (&$middlewareA) {
$middlewareA = true;
});

$connector->middleware()->onResponse(function () use (&$middlewareB) {
$middlewareB = true;
});

$request->middleware()->onRequest(function () use (&$middlewareC) {
$middlewareC = true;
});

$request->middleware()->onResponse(function () use (&$middlewareD) {
$middlewareD = true;
});

$responseA = $connector->send($request);

expect($middlewareA)->toBeTrue();
expect($middlewareB)->toBeTrue();
expect($middlewareC)->toBeTrue();
expect($middlewareD)->toBeTrue();
});

0 comments on commit 5002893

Please sign in to comment.