Last active
December 20, 2015 03:49
-
-
Save omerucel/6066452 to your computer and use it in GitHub Desktop.
Behat RestContext example
This file contains hidden or 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
<?php | |
use Behat\Behat\Context\BehatContext; | |
require_once 'RestContext.php'; | |
/** | |
* Features context. | |
*/ | |
class FeatureContext extends BehatContext | |
{ | |
/** | |
* Initializes context. | |
* Every scenario gets it's own context object. | |
* | |
* @param array $parameters context parameters (set them up through behat.yml) | |
*/ | |
public function __construct(array $parameters) | |
{ | |
$this->useContext('RestContext', new RestContext('http://127.0.0.1:8080')); | |
} | |
} |
This file contains hidden or 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
<?php | |
require_once 'PHPUnit/Autoload.php'; | |
require_once 'PHPUnit/Framework/Assert/Functions.php'; | |
class RestContext extends \Behat\Behat\Context\BehatContext | |
{ | |
/** | |
* @var string | |
*/ | |
protected $requestObjectType; | |
/** | |
* @var string | |
*/ | |
protected $requestMethod; | |
/** | |
* @var array | |
*/ | |
protected $requestParams = array(); | |
/** | |
* @var Requests_Response | |
*/ | |
protected $response; | |
/** | |
* @var object | |
*/ | |
protected $responseParams; | |
/** | |
* @var string | |
*/ | |
protected $apiUrl; | |
/** | |
* @param $apiUrl | |
*/ | |
public function __construct($apiUrl) | |
{ | |
$this->apiUrl = $apiUrl; | |
} | |
/** | |
* @Given /^that I want to start a new "([^"]*)"$/ | |
*/ | |
public function thatIWantToStartANew($objectType) | |
{ | |
$this->requestObjectType = $objectType; | |
$this->requestMethod = Requests::POST; | |
} | |
/** | |
* @Given /^that its "([^"]*)" is "([^"]*)"$/ | |
*/ | |
public function thatItsIs($paramName, $value) | |
{ | |
$this->requestParams[$paramName] = $value; | |
} | |
/** | |
* @When /^I request "([^"]*)"$/ | |
*/ | |
public function iRequest($path) | |
{ | |
// TODO : http build quuery for GET & DELETE method. | |
$this->response = Requests::request($this->apiUrl . $path, array(), $this->requestParams, $this->requestMethod); | |
$this->responseParams = json_decode($this->response->body); | |
} | |
/** | |
* @Then /^the response content type should be "([^"]*)"$/ | |
*/ | |
public function theResponseContentTypeShouldBe($contentType) | |
{ | |
assertNotEmpty(json_decode($this->response->body)); | |
assertEquals($contentType, $this->response->headers->offsetGet('content-type')); | |
} | |
/** | |
* @Given /^the response status code should be (\d+)$/ | |
*/ | |
public function theResponseStatusCodeShouldBe($statusCode) | |
{ | |
assertEquals($statusCode, $this->response->status_code); | |
} | |
/** | |
* @Given /^the response has a "([^"]*)" property$/ | |
*/ | |
public function theResponseHasAProperty($propertyName) | |
{ | |
$propertyNames = explode('.', $propertyName); | |
$params = $this->responseParams; | |
foreach ($propertyNames as $key) { | |
assertObjectHasAttribute($key, $params); | |
$params = $params->$key; | |
} | |
} | |
/** | |
* @Given /^the "([^"]*)" property should be "([^"]*)"$/ | |
*/ | |
public function thePropertyShouldBe($propertyName, $expectedValue) | |
{ | |
$propertyNames = explode('.', $propertyName); | |
$value = null; | |
$params = $this->responseParams; | |
foreach ($propertyNames as $key) { | |
$value = $params->$key; | |
$params = $params->$key; | |
} | |
assertEquals($expectedValue, $value); | |
} | |
} |
This file contains hidden or 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
# features/Subscription.feature | |
Feature: Subscription | |
Scenario: Start subscription with only required parameters | |
Given that I want to start a new "Subscription" | |
And that its "token" is "token1" | |
And that its "operator_id" is "1" | |
And that its "channel_id" is "1" | |
And that its "identifier" is "customer1" | |
When I request "/api/services/1/subscriptions" | |
Then the response content type should be "application/json" | |
And the response status code should be 201 | |
And the response has a "meta" property | |
And the response has a "meta.requestId" property | |
And the response has a "meta.httpStatusCode" property | |
And the response has a "id" property | |
And the "meta.httpStatusCode" property should be "201" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment