-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a response class with some basic assertions (#4)
* Added response with assertions * Updatd docs * Update order of arguments * Fixed tests
- Loading branch information
Showing
5 changed files
with
162 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Happyr\BrefHookHandler; | ||
|
||
use Bref\Lambda\InvocationResult; | ||
use Happyr\BrefHookHandler\Exception\AssertionFailed; | ||
|
||
class ApiGatewayResponse | ||
{ | ||
/** | ||
* @var InvocationResult | ||
*/ | ||
private $result; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $payload; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $url; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $body; | ||
|
||
public function __construct(InvocationResult $result, string $url) | ||
{ | ||
$this->result = $result; | ||
$this->payload = $result->getPayload(); | ||
$this->url = $url; | ||
} | ||
|
||
public function getResult(): InvocationResult | ||
{ | ||
return $this->result; | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getBody(): string | ||
{ | ||
if (null !== $this->body) { | ||
return $this->body; | ||
} | ||
|
||
$requestBody = $this->payload['body'] ?? ''; | ||
if ($this->payload['isBase64Encoded'] ?? false) { | ||
$requestBody = base64_decode($requestBody); | ||
} | ||
|
||
return $this->body = $requestBody; | ||
} | ||
|
||
public function assertStatusCode(int $expected) | ||
{ | ||
if ($expected !== $this->payload['statusCode']) { | ||
throw AssertionFailed::create('URL "%s" did not response with HTTP status code "%d"', $this->url, $expected); | ||
} | ||
} | ||
|
||
public function assertBodyContains(string $string) | ||
{ | ||
if (false === \mb_stripos($this->getBody(), $string)) { | ||
throw AssertionFailed::create('URL "%s" does not have a body with string "%s"', $this->url, $string); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Happyr\BrefHookHandler\Exception; | ||
|
||
class AssertionFailed extends \RuntimeException | ||
{ | ||
public static function create(string $message, ...$args): self | ||
{ | ||
return new self(($args ? vsprintf($message, $args) : $message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters