Skip to content

Instantly share code, notes, and snippets.

@carlosromero
Created March 10, 2015 09:35
Show Gist options
  • Save carlosromero/9da8dc688f0cff8a26fc to your computer and use it in GitHub Desktop.
Save carlosromero/9da8dc688f0cff8a26fc to your computer and use it in GitHub Desktop.
<?php
use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\Request;
use Behat\Behat\Context\SnippetAcceptingContext;
/**
* Defines application features from the specific context.
*/
class RestContext implements SnippetAcceptingContext
{
/**
* @var Client
*/
protected $client;
/**
* The request payload
*/
protected $requestPayload;
/**
* The Guzzle HTTP Response.
* @var Response
*/
protected $response;
/**
* The decoded response object.
*/
protected $responsePayload;
public function __construct(array $parameters)
{
$config = isset($parameters['guzzle']) && is_array($parameters['guzzle']) ? $parameters['guzzle'] : [];
$this->client = new Client($parameters['base_url'], $config);
}
/**
* @When /^I request "(GET|PUT|POST|DELETE) ([^"]*)"$/
*/
public function iRequest($httpMethod, $resource)
{
$this->resource = $resource;
$method = strtolower($httpMethod);
try {
switch ($httpMethod) {
case 'PUT':
$this->response = $this
->client
->$method($resource, null, $this->requestPayload)->send();
break;
case 'POST':
$post = json_decode($this->requestPayload, true);
$this->response = $this
->client
->$method($resource, array('body' => $post))->send();
break;
default:
$this->response = $this
->client
->$method($resource)->send();
}
} catch (BadResponseException $e) {
$response = $e->getResponse();
// Sometimes the request will fail, at which point we have
// no response at all. Let Guzzle give an error here, it's
// pretty self-explanatory.
if ($response === null) {
throw $e;
}
$this->response = $e->getResponse();
}
}
/**
* @Transform /^(\d+)$/
*/
public function castStringToNumber($string)
{
return intval($string);
}
/**
* Checks the response exists and returns it.
*
* @return Guzzle\Http\Message\Response
*/
protected function getResponse()
{
if (! $this->response) {
throw new Exception("You must first make a request to check a response.");
}
return $this->response;
}
/**
* Return the response payload from the current response.
*
* @return mixed
*/
protected function getResponsePayload()
{
if (! $this->responsePayload) {
$json = json_decode($this->getResponse()->getBody(true));
if (json_last_error() !== JSON_ERROR_NONE) {
$message = 'Failed to decode JSON body ';
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$message .= '(Maximum stack depth exceeded).';
break;
case JSON_ERROR_STATE_MISMATCH:
$message .= '(Underflow or the modes mismatch).';
break;
case JSON_ERROR_CTRL_CHAR:
$message .= '(Unexpected control character found).';
break;
case JSON_ERROR_SYNTAX:
$message .= '(Syntax error, malformed JSON).';
break;
case JSON_ERROR_UTF8:
$message .= '(Malformed UTF-8 characters, possibly incorrectly encoded).';
break;
default:
$message .= '(Unknown error).';
break;
}
throw new Exception($message);
}
$this->responsePayload = $json;
}
return $this->responsePayload;
}
/**
*
* @param $resource string Url to visit
*/
public function visit($resource) {
$this->iRequest('GET', $resource);
}
public function assertResponseStatus($code)
{
if ($this->response->getStatusCode() != $code ) {
throw new Exception("Status code ins't $code , provided: " . $this->response->getStatusCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment