From 50028936cf215724f41363b92ec8dda952caae3c Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 3 Sep 2023 21:22:42 +0100 Subject: [PATCH] Added test for fake middleware --- tests/Feature/MockRequestTest.php | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/Feature/MockRequestTest.php b/tests/Feature/MockRequestTest.php index 28ebf52a..cdf07510 100644 --- a/tests/Feature/MockRequestTest.php +++ b/tests/Feature/MockRequestTest.php @@ -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(); +});