From 23d6d0821171dc0698e51e1d201b4dafd11687df Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 7 Dec 2020 12:42:14 +0000 Subject: [PATCH] Adds a set method to transform a Response object --- src/Response/Response.php | 6 ++++++ tests/Hooks/GlobalHooksTest.php | 12 ++++++++++++ tests/Hooks/LocalHooksTest.php | 13 +++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Response/Response.php b/src/Response/Response.php index 112c682..b857b2b 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -24,4 +24,10 @@ public function withXml($xmlRequest, $xmlResponse) $this->xmlResponse = $xmlResponse; return $this; } + + public function set($key, $value): self + { + data_set($this->response, $key, $value); + return $this; + } } diff --git a/tests/Hooks/GlobalHooksTest.php b/tests/Hooks/GlobalHooksTest.php index 357f3ce..9e8ec51 100644 --- a/tests/Hooks/GlobalHooksTest.php +++ b/tests/Hooks/GlobalHooksTest.php @@ -58,6 +58,18 @@ public function the_beforeRequesting_hooks_can_transform_the_request_object() Soap::assertSent(fn($request) => $request->getBody()['hello'] === ['world' => ['foo', 'bar'], 'person' => 'Richard']); } + /** @test */ + public function the_afterRequesting_hooks_can_transform_the_response_object() + { + Soap::fake(); + + Soap::afterRequesting(fn($request, Response $response) => $response->set('hello.world', ['foo', 'bar'])); + Soap::afterRequesting(fn($request, Response $response) => $response->set('hello.person', 'Richard')); + Soap::to('http://endpoint.com')->Test(); + + Soap::assertSent(fn($request, Response $response) => $response->response['hello'] === ['world' => ['foo', 'bar'], 'person' => 'Richard']); + } + protected function tearDown(): void { parent::tearDown(); diff --git a/tests/Hooks/LocalHooksTest.php b/tests/Hooks/LocalHooksTest.php index 4596b6c..87884b5 100644 --- a/tests/Hooks/LocalHooksTest.php +++ b/tests/Hooks/LocalHooksTest.php @@ -92,6 +92,19 @@ public function the_beforeRequesting_hooks_can_transform_the_request_object() Soap::assertSent(fn($request) => $request->getBody()['hello'] === ['world' => ['foo', 'bar'], 'person' => 'Richard']); } + /** @test */ + public function the_afterRequesting_hooks_can_transform_the_response_object() + { + Soap::fake(); + + Soap::to('http://endpoint.com') + ->afterRequesting(fn($request, Response $response) => $response->set('hello.world', ['foo', 'bar'])) + ->afterRequesting(fn($request, Response $response) => $response->set('hello.person', 'Richard')) + ->Test(); + + Soap::assertSent(fn($request, Response $response) => $response->response['hello'] === ['world' => ['foo', 'bar'], 'person' => 'Richard']); + } + protected function setUp(): void { parent::setUp();