Skip to content

Commit

Permalink
Merge pull request #9 from Ricorocks-Digital-Agency/response-transfor…
Browse files Browse the repository at this point in the history
…mation

Adds a set method to transform a Response object
  • Loading branch information
lukeraymonddowning authored Dec 7, 2020
2 parents 87a7918 + 23d6d08 commit e049ad3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions tests/Hooks/GlobalHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions tests/Hooks/LocalHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e049ad3

Please sign in to comment.