2014-04-01 08:52:37 -04:00
|
|
|
<?php
|
2013-12-16 16:08:39 -04:00
|
|
|
use Behat\Behat\Context\BehatContext;
|
|
|
|
|
use Behat\Gherkin\Node\PyStringNode;
|
2013-12-17 07:39:05 -04:00
|
|
|
use Behat\Gherkin\Node\TableNode;
|
2014-01-05 17:39:28 -04:00
|
|
|
use Behat\Behat\Exception\PendingException;
|
2013-12-16 16:08:39 -04:00
|
|
|
/**
|
|
|
|
|
* Rest context.
|
|
|
|
|
*
|
|
|
|
|
* @category Framework
|
|
|
|
|
* @package restler
|
|
|
|
|
* @author R.Arul Kumaran <arul@luracast.com>
|
|
|
|
|
* @copyright 2010 Luracast
|
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
|
* @link http://luracast.com/products/restler/
|
|
|
|
|
* @version 3.0.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
|
|
class RestContext extends BehatContext
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private $_startTime = null;
|
|
|
|
|
private $_restObject = null;
|
|
|
|
|
private $_headers = array();
|
|
|
|
|
private $_restObjectType = null;
|
|
|
|
|
private $_restObjectMethod = 'get';
|
|
|
|
|
private $_client = null;
|
|
|
|
|
private $_response = null;
|
|
|
|
|
private $_request = null;
|
|
|
|
|
private $_requestBody = null;
|
|
|
|
|
private $_requestUrl = null;
|
|
|
|
|
private $_type = null;
|
|
|
|
|
private $_charset = null;
|
|
|
|
|
private $_language = null;
|
|
|
|
|
private $_contentType = null;
|
|
|
|
|
private $_data = null;
|
|
|
|
|
|
|
|
|
|
private $access_token = null;
|
|
|
|
|
|
|
|
|
|
private $_parameters = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes context.
|
|
|
|
|
* Every scenario gets it's own context object.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $parameters)
|
|
|
|
|
{
|
|
|
|
|
// Initialize your context here
|
|
|
|
|
|
|
|
|
|
$this->_restObject = new stdClass();
|
|
|
|
|
$this->_parameters = $parameters;
|
|
|
|
|
$this->_client = new Guzzle\Service\Client();
|
|
|
|
|
|
|
|
|
|
//suppress few errors
|
|
|
|
|
$this->_client
|
|
|
|
|
->getEventDispatcher()
|
|
|
|
|
->addListener('request.error',
|
|
|
|
|
function (\Guzzle\Common\Event $event) {
|
|
|
|
|
switch ($event['response']->getStatusCode()) {
|
|
|
|
|
case 400:
|
|
|
|
|
case 401:
|
|
|
|
|
case 404:
|
|
|
|
|
case 405:
|
|
|
|
|
case 406:
|
|
|
|
|
$event->stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
$timezone = ini_get('date.timezone');
|
|
|
|
|
if (empty($timezone)) {
|
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParameter($name)
|
|
|
|
|
{
|
|
|
|
|
if (count($this->_parameters) === 0) {
|
|
|
|
|
throw new \Exception('Parameters not loaded!');
|
|
|
|
|
} else {
|
|
|
|
|
$parameters = $this->_parameters;
|
|
|
|
|
return (isset($parameters[$name])) ? $parameters[$name] : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ============ json array ===================
|
|
|
|
|
* @Given /^that I send (\[[^]]*\])$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json object ==================
|
|
|
|
|
* @Given /^that I send (\{(?>[^\{\}]+|(?1))*\})$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json string ==================
|
|
|
|
|
* @Given /^that I send ("[^"]*")$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json int =====================
|
|
|
|
|
* @Given /^that I send ([-+]?[0-9]*\.?[0-9]+)$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json null or boolean =========
|
|
|
|
|
* @Given /^that I send (null|true|false)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatISend($data)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObject = json_decode($data);
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I send:/
|
|
|
|
|
* @param PyStringNode $data
|
|
|
|
|
*/
|
|
|
|
|
public function thatISendPyString(PyStringNode $data) {
|
|
|
|
|
$this->thatISend($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ============ json array ===================
|
|
|
|
|
* @Given /^the response equals (\[[^]]*\])$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json object ==================
|
|
|
|
|
* @Given /^the response equals (\{(?>[^\{\}]+|(?1))*\})$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json string ==================
|
|
|
|
|
* @Given /^the response equals ("[^"]*")$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json int =====================
|
|
|
|
|
* @Given /^the response equals ([-+]?[0-9]*\.?[0-9]+)$/
|
|
|
|
|
*
|
|
|
|
|
* ============ json null or boolean =========
|
|
|
|
|
* @Given /^the response equals (null|true|false)$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseEquals($response)
|
|
|
|
|
{
|
|
|
|
|
$data = json_encode($this->_data);
|
|
|
|
|
if ($data !== $response)
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response value does not match '$response'\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the response equals:/
|
|
|
|
|
* @param PyStringNode $data
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseEqualsPyString(PyStringNode $response)
|
|
|
|
|
{
|
|
|
|
|
$this->theResponseEquals($response);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to make a new "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToMakeANew($objectType)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectType = ucwords(strtolower($objectType));
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to update "([^"]*)"$/
|
|
|
|
|
* @Given /^that I want to update an "([^"]*)"$/
|
|
|
|
|
* @Given /^that I want to update a "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToUpdate($objectType)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectType = ucwords(strtolower($objectType));
|
|
|
|
|
$this->_restObjectMethod = 'put';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to find a "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToFindA($objectType)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectType = ucwords(strtolower($objectType));
|
|
|
|
|
$this->_restObjectMethod = 'get';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to delete a "([^"]*)"$/
|
|
|
|
|
* @Given /^that I want to delete an "([^"]*)"$/
|
|
|
|
|
* @Given /^that I want to delete "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToDeleteA($objectType)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectType = ucwords(strtolower($objectType));
|
|
|
|
|
$this->_restObjectMethod = 'delete';
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 15:53:36 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to delete a resource with the key "([^"]*)" stored in session array$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToDeleteAResourceWithTheKeyStoredInSessionArray($varName)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$varName) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$varName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restDeleteQueryStringSuffix = "/" . $varValue;
|
|
|
|
|
$this->_restObjectMethod = 'delete';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to update a resource with the key "([^"]*)" stored in session array$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToUpdateAResourceWithTheKeyStoredInSessionArray($varName)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$varName) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$varName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restUpdateQueryStringSuffix = "/" . $varValue;
|
|
|
|
|
$this->_restObjectMethod = 'put';
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 10:37:50 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to get a resource with the key "([^"]*)" stored in session array$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToGetAResourceWithTheKeyStoredInSessionArray($varName)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$varName) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$varName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restGetQueryStringSuffix = "/" . $varValue;
|
|
|
|
|
$this->_restObjectMethod = 'get';
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 16:08:39 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that "([^"]*)" header is set to "([^"]*)"$/
|
|
|
|
|
* @Given /^that "([^"]*)" header is set to (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatHeaderIsSetTo($header, $value)
|
|
|
|
|
{
|
|
|
|
|
$this->_headers[$header] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that its "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^that his "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^that her "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^its "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^his "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^her "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
* @Given /^that "([^"]*)" is set to "([^"]*)"$/
|
|
|
|
|
* @Given /^"([^"]*)" is set to "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatItsStringPropertyIs($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObject->$propertyName = $propertyValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that its "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^that his "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^that her "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^its "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^his "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^her "([^"]*)" is (\d+)$/
|
|
|
|
|
* @Given /^that "([^"]*)" is set to (\d+)$/
|
|
|
|
|
* @Given /^"([^"]*)" is set to (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatItsNumericPropertyIs($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObject->$propertyName = is_float($propertyValue)
|
|
|
|
|
? floatval($propertyValue)
|
|
|
|
|
: intval($propertyValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that its "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^that his "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^that her "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^its "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^his "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^her "([^"]*)" is (true|false)$/
|
|
|
|
|
* @Given /^that "([^"]*)" is set to (true|false)$/
|
|
|
|
|
* @Given /^"([^"]*)" is set to (true|false)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatItsBooleanPropertyIs($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObject->$propertyName = $propertyValue == 'true';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the request is sent as JSON$/
|
|
|
|
|
* @Given /^the request is sent as Json$/
|
|
|
|
|
*/
|
|
|
|
|
public function theRequestIsSentAsJson()
|
|
|
|
|
{
|
|
|
|
|
$this->_headers['Content-Type'] = 'application/json; charset=utf-8';
|
|
|
|
|
$this->_requestBody = json_encode(
|
|
|
|
|
is_object($this->_restObject)
|
|
|
|
|
? (array)$this->_restObject
|
|
|
|
|
: $this->_restObject
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I have a valid access_token$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIHaveAValidAccessToken()
|
|
|
|
|
{
|
|
|
|
|
$access_token = $this->getParameter('access_token');
|
|
|
|
|
if (strlen($access_token)<= 10) {
|
|
|
|
|
throw new PendingException();
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception ("Access token is not valid, please review behat.yml\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
$this->access_token = $access_token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @When /^I request "([^"]*)"$/
|
|
|
|
|
*/
|
2014-02-05 15:39:42 -04:00
|
|
|
public function iRequest($pageUrl, $urlType="")
|
2013-12-16 16:08:39 -04:00
|
|
|
{
|
|
|
|
|
$this->_startTime = microtime(true);
|
|
|
|
|
$baseUrl = $this->getParameter('base_url');
|
|
|
|
|
if ($this->access_token != null) {
|
|
|
|
|
$this->_headers['Authorization'] = 'Bearer ' . $this->access_token;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 08:52:37 -04:00
|
|
|
|
|
|
|
|
|
2014-02-11 17:00:26 -04:00
|
|
|
|
2014-02-05 15:39:42 -04:00
|
|
|
if($urlType=="absolute"){
|
|
|
|
|
$this->_requestUrl = $pageUrl;
|
|
|
|
|
}else{
|
|
|
|
|
$this->_requestUrl = $baseUrl . $pageUrl;
|
|
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
$url = false !== strpos($pageUrl, '{')
|
|
|
|
|
? array($this->_requestUrl, (array)$this->_restObject)
|
|
|
|
|
: $this->_requestUrl;
|
|
|
|
|
|
|
|
|
|
switch (strtoupper($this->_restObjectMethod)) {
|
|
|
|
|
case 'HEAD':
|
|
|
|
|
$this->_request = $this->_client
|
|
|
|
|
->head($url, $this->_headers);
|
|
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
case 'GET':
|
2014-01-07 10:37:50 -04:00
|
|
|
if (isset($this->_restGetQueryStringSuffix) &&
|
|
|
|
|
$this->_restGetQueryStringSuffix != '') {
|
|
|
|
|
$url .= $this->_restGetQueryStringSuffix;
|
|
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
$this->_request = $this->_client
|
2014-02-11 17:00:26 -04:00
|
|
|
->get($url, $this->_headers);
|
2013-12-16 16:08:39 -04:00
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
case 'POST':
|
|
|
|
|
$postFields = is_object($this->_restObject)
|
|
|
|
|
? (array)$this->_restObject
|
2014-04-01 08:52:37 -04:00
|
|
|
: $this->_restObject;
|
2013-12-16 16:08:39 -04:00
|
|
|
$this->_request = $this->_client
|
|
|
|
|
->post($url, $this->_headers,
|
|
|
|
|
(empty($this->_requestBody) ? $postFields :
|
|
|
|
|
$this->_requestBody));
|
|
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
case 'PUT' :
|
2013-12-17 15:53:36 -04:00
|
|
|
if (isset($this->_restUpdateQueryStringSuffix) &&
|
|
|
|
|
$this->_restUpdateQueryStringSuffix != '') {
|
|
|
|
|
$url .= $this->_restUpdateQueryStringSuffix;
|
|
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
$putFields = is_object($this->_restObject)
|
|
|
|
|
? (array)$this->_restObject
|
|
|
|
|
: $this->_restObject;
|
2014-04-07 10:06:20 -04:00
|
|
|
$this->printDebug("URL F: $url\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
$this->_request = $this->_client
|
|
|
|
|
->put($url, $this->_headers,
|
|
|
|
|
(empty($this->_requestBody) ? $putFields :
|
|
|
|
|
$this->_requestBody));
|
|
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
case 'PATCH' :
|
|
|
|
|
$putFields = is_object($this->_restObject)
|
|
|
|
|
? (array)$this->_restObject
|
|
|
|
|
: $this->_restObject;
|
|
|
|
|
$this->_request = $this->_client
|
|
|
|
|
->patch($url, $this->_headers,
|
|
|
|
|
(empty($this->_requestBody) ? $putFields :
|
|
|
|
|
$this->_requestBody));
|
|
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
case 'DELETE':
|
2013-12-17 15:53:36 -04:00
|
|
|
if (isset($this->_restDeleteQueryStringSuffix) &&
|
|
|
|
|
$this->_restDeleteQueryStringSuffix != '') {
|
|
|
|
|
$url .= $this->_restDeleteQueryStringSuffix;
|
|
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
$this->_request = $this->_client
|
|
|
|
|
->delete($url, $this->_headers);
|
|
|
|
|
$this->_response = $this->_request->send();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//detect type, extract data
|
|
|
|
|
$this->_language = $this->_response->getHeader('Content-Language');
|
|
|
|
|
|
|
|
|
|
$cType = explode('; ', $this->_response->getHeader('Content-type'));
|
|
|
|
|
if (count($cType) > 1) {
|
|
|
|
|
$charset = $cType[1];
|
|
|
|
|
$this->_charset = substr($charset, strpos($charset, '=') + 1);
|
|
|
|
|
}
|
|
|
|
|
$this->_contentType = $cType[0];
|
|
|
|
|
switch ($this->_contentType) {
|
|
|
|
|
case 'text/html':
|
|
|
|
|
$this->_data = $this->_response->getBody(true);
|
|
|
|
|
return;
|
|
|
|
|
break;
|
|
|
|
|
case 'application/json':
|
|
|
|
|
$this->_type = 'json';
|
|
|
|
|
$this->_data = json_decode($this->_response->getBody(true));
|
|
|
|
|
switch (json_last_error()) {
|
|
|
|
|
case JSON_ERROR_NONE :
|
|
|
|
|
return;
|
|
|
|
|
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 :
|
2014-02-10 09:43:08 -04:00
|
|
|
$message = "malformed JSON \n\n ------\n".$this->_response->getBody(true)."\n ------";
|
2013-12-16 16:08:39 -04:00
|
|
|
break;
|
|
|
|
|
case JSON_ERROR_UTF8 :
|
|
|
|
|
$message = 'malformed UTF-8 characters, possibly ' .
|
|
|
|
|
'incorrectly encoded';
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
$message = 'unknown error';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception ("Error parsing JSON, $message \n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
break;
|
|
|
|
|
case 'application/xml':
|
|
|
|
|
$this->_type = 'xml';
|
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
|
$this->_data = @simplexml_load_string(
|
|
|
|
|
$this->_response->getBody(true));
|
|
|
|
|
if (!$this->_data) {
|
|
|
|
|
$message = '';
|
|
|
|
|
foreach (libxml_get_errors() as $error) {
|
|
|
|
|
$message .= $error->message . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
throw new Exception ('Error parsing XML, ' . $message);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-01-17 13:03:56 -04:00
|
|
|
default:
|
|
|
|
|
$this->_data = $this->_response->getBody(true);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response is JSON$/
|
|
|
|
|
* @Then /^the response should be JSON$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseIsJson()
|
|
|
|
|
{
|
|
|
|
|
if ($this->_type != 'json') {
|
2014-01-17 13:03:56 -04:00
|
|
|
throw new Exception("Response was not JSON\n" . $this->_response->getBody(true) );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response is XML$/
|
|
|
|
|
* @Then /^the response should be XML$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseIsXml()
|
|
|
|
|
{
|
|
|
|
|
if ($this->_type != 'xml') {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response was not XML\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the content type is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseContentTypeIs($contentType)
|
|
|
|
|
{
|
2014-05-05 09:06:34 -04:00
|
|
|
|
2013-12-16 16:08:39 -04:00
|
|
|
if ($this->_contentType != $contentType) {
|
2014-05-05 09:06:34 -04:00
|
|
|
throw new Exception("Response Content Type was not $contentType\n\n".$this->_response->getBody(true));
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response charset is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseCharsetIs($charset)
|
|
|
|
|
{
|
|
|
|
|
if (strtoupper($this->_charset) != strtoupper($charset)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response charset was not $charset\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response language is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseLanguageIs($language)
|
|
|
|
|
{
|
|
|
|
|
if ($this->_language != $language) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response Language was not $language\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response "([^"]*)" header should be "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseHeaderShouldBe($header, $value)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->_response->hasHeader($header)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response header $header was not found\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if ((string)$this->_response->getHeader($header) !== $value) {
|
|
|
|
|
throw new Exception("Response header $header ("
|
|
|
|
|
. (string)$this->_response->getHeader($header)
|
2013-12-19 11:06:25 -04:00
|
|
|
. ") does not match '$value'\n\n"
|
|
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response "Expires" header should be Date\+(\d+) seconds$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseExpiresHeaderShouldBeDatePlusGivenSeconds($seconds)
|
|
|
|
|
{
|
|
|
|
|
$server_time = strtotime($this->_response->getHeader('Date')) + $seconds;
|
|
|
|
|
$expires_time = strtotime($this->_response->getHeader('Expires'));
|
|
|
|
|
if ($expires_time === $server_time || $expires_time === $server_time + 1)
|
|
|
|
|
return;
|
|
|
|
|
return $this->theResponseHeaderShouldBe(
|
|
|
|
|
'Expires',
|
|
|
|
|
gmdate('D, d M Y H:i:s \G\M\T', $server_time)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response time should at least be (\d+) milliseconds$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseTimeShouldAtLeastBeMilliseconds($milliSeconds)
|
|
|
|
|
{
|
|
|
|
|
usleep(1);
|
|
|
|
|
$diff = 1000 * (microtime(true) - $this->_startTime);
|
|
|
|
|
if ($diff < $milliSeconds) {
|
|
|
|
|
throw new Exception("Response time $diff is "
|
|
|
|
|
. "quicker than $milliSeconds\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 15:53:36 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^the json data is an empty array$/
|
|
|
|
|
*/
|
|
|
|
|
public function theJsonDataIsAnEmptyArray()
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (is_array($data) && count($data) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response is not an empty array\n\n" );
|
2013-12-17 15:53:36 -04:00
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the type is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theTypeIs($type)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'string':
|
|
|
|
|
if (is_string($data)) return;
|
|
|
|
|
case 'int':
|
|
|
|
|
if (is_int($data)) return;
|
|
|
|
|
case 'float':
|
|
|
|
|
if (is_float($data)) return;
|
|
|
|
|
case 'array' :
|
|
|
|
|
if (is_array($data)) return;
|
|
|
|
|
case 'object' :
|
|
|
|
|
if (is_object($data)) return;
|
|
|
|
|
case 'null' :
|
|
|
|
|
if (is_null($data)) return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response is not of type '$type'\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the "([^"]*)" property type is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyTypeIs($property, $type)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (isset($this->{$property}) ) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("The property $property is not defined in the Response\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
$theProperty = $data->{$property};
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'string':
|
|
|
|
|
if (is_string($theProperty)) return;
|
|
|
|
|
case 'int':
|
|
|
|
|
if (is_int($theProperty)) return;
|
|
|
|
|
case 'float':
|
|
|
|
|
if (is_float($theProperty)) return;
|
|
|
|
|
case 'array' :
|
|
|
|
|
if (is_array($theProperty)) return;
|
|
|
|
|
case 'object' :
|
|
|
|
|
if (is_object($theProperty)) return;
|
|
|
|
|
case 'null' :
|
|
|
|
|
if (is_null($theProperty)) return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("The property $property in Response is not of type '$type'\n\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the value equals "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theValueEquals($sample)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if ($data !== $sample) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response value does not match '$sample'\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the value equals (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function theNumericValueEquals($sample)
|
|
|
|
|
{
|
|
|
|
|
$sample = is_float($sample) ? floatval($sample) : intval($sample);
|
|
|
|
|
return $this->theValueEquals($sample);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the value equals (true|false)$/
|
|
|
|
|
*/
|
|
|
|
|
public function theBooleanValueEquals($sample)
|
|
|
|
|
{
|
|
|
|
|
$sample = $sample == 'true';
|
|
|
|
|
return $this->theValueEquals($sample);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response is JSON "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseIsJsonWithType($type)
|
|
|
|
|
{
|
|
|
|
|
if ($this->_type != 'json') {
|
2014-01-17 13:03:56 -04:00
|
|
|
throw new Exception("Response was not JSON\n" . $this->_response->getBody(true) );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'string':
|
|
|
|
|
if (is_string($data)) return;
|
|
|
|
|
case 'int':
|
|
|
|
|
if (is_int($data)) return;
|
|
|
|
|
case 'float':
|
|
|
|
|
if (is_float($data)) return;
|
|
|
|
|
case 'array' :
|
|
|
|
|
if (is_array($data)) return;
|
|
|
|
|
case 'object' :
|
|
|
|
|
if (is_object($data)) return;
|
|
|
|
|
case 'null' :
|
|
|
|
|
if (is_null($data)) return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Response was JSON\n but not of type '$type'\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the response has a "([^"]*)" property$/
|
|
|
|
|
* @Given /^the response has an "([^"]*)" property$/
|
|
|
|
|
* @Given /^the response has a property called "([^"]*)"$/
|
|
|
|
|
* @Given /^the response has an property called "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseHasAProperty($propertyName)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!isset($data->$propertyName)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Property '$propertyName' is not set!\n\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-08 10:23:53 -04:00
|
|
|
/**
|
2014-01-08 10:43:41 -04:00
|
|
|
* @Given /^the response has not a "([^"]*)" property$/
|
|
|
|
|
* @Given /^the response has not an "([^"]*)" property$/
|
|
|
|
|
* @Given /^the response has not a property called "([^"]*)"$/
|
|
|
|
|
* @Given /^the response has not an property called "([^"]*)"$/
|
2014-01-08 10:23:53 -04:00
|
|
|
*/
|
2014-01-08 10:43:41 -04:00
|
|
|
public function theResponseHasNotAProperty($propertyName)
|
2014-01-08 10:23:53 -04:00
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (isset($data->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '$propertyName' is set!\n\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 16:08:39 -04:00
|
|
|
/**
|
|
|
|
|
* @Then /^the "([^"]*)" property equals "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyEquals($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!isset($data->$propertyName)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Property '$propertyName' is not set!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if ($data->$propertyName != $propertyValue) {
|
|
|
|
|
throw new \Exception('Property value mismatch! (given: '
|
|
|
|
|
. $propertyValue . ', match: '
|
|
|
|
|
. $data->$propertyName . ")\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the "([^"]*)" property equals (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyEqualsNumber($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$propertyValue = is_float($propertyValue)
|
|
|
|
|
? floatval($propertyValue) : intval($propertyValue);
|
|
|
|
|
return $this->thePropertyEquals($propertyName, $propertyValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the "([^"]*)" property in row (\d+) equals "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyInRowEquals($propertyName, $row, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!is_array($data)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the Response data is not an array!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (is_array($data) && !isset($data[$row])) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the Response data is an array, but the row '$row' does not exists!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (!isset($data[$row]->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '"
|
|
|
|
|
. $propertyName . "' is not set!\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if ($data[$row]->$propertyName != $propertyValue) {
|
|
|
|
|
throw new \Exception('Property value mismatch! (given: '
|
|
|
|
|
. $propertyValue . ', match: '
|
|
|
|
|
. $data[$row]->$propertyName . ")\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the "([^"]*)" property in row (\d+) equals (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyInRowEqualsNumber($propertyName, $row, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$propertyValue = is_float($propertyValue)
|
|
|
|
|
? floatval($propertyValue) : intval($propertyValue);
|
|
|
|
|
return $this->thePropertyInRowEquals($propertyName, $row, $propertyValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the "([^"]*)" property equals (true|false)$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyEqualsBoolean($propertyName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
return $this->thePropertyEquals($propertyName, $propertyValue == 'true');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the "([^"]*)" property in row (\d+) of property "([^"]*)" equals "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyInRowOfPropertyEquals($propertyName, $row, $propertyParent, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
throw new Exception("Response is empty or was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($data->$propertyParent)) {
|
|
|
|
|
throw new Exception("Response has not the property '$propertyParent'\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $data->$propertyParent;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!is_array($data)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the $propertyParent in Response data is not an array!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (is_array($data) && !isset($data[$row])) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the Response data is an array, but the row '$row' does not exists!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (!isset($data[$row]->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '"
|
|
|
|
|
. $propertyName . "' is not set!\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (is_array($data[$row]->$propertyName)) {
|
|
|
|
|
throw new Exception("$propertyName is an array and we expected a value\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
if ($data[$row]->$propertyName != $propertyValue) {
|
|
|
|
|
throw new \Exception('Property value mismatch! (given: '
|
|
|
|
|
. $propertyValue . ', match: '
|
|
|
|
|
. $data[$row]->$propertyName . ")\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the "([^"]*)" property in row (\d+) of property "([^"]*)" is "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyInRowOfPropertyIs($propertyName, $row, $propertyParent, $propertyType)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
throw new Exception("Response is empty or was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($data->$propertyParent)) {
|
|
|
|
|
throw new Exception("Response has not the property '$propertyParent'\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $data->$propertyParent;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!is_array($data)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the property $propertyParent in Response data is not an array!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (is_array($data) && !isset($data[$row])) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("the Response data is an array, but the row '$row' does not exists!\n\n" );
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if (!isset($data[$row]->$propertyName)) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new Exception("Property '$propertyName' is not set in $propertyParent!\n\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
if ($propertyType == 'array' && is_array($data[$row]->$propertyName)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($propertyType == 'object' && is_object($data[$row]->$propertyName)) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("$propertyName is not an $propertyType\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the type of the "([^"]*)" property is ([^"]*)$/
|
|
|
|
|
*/
|
|
|
|
|
public function theTypeOfThePropertyIs($propertyName, $typeString)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!isset($data->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '"
|
2013-12-19 11:06:25 -04:00
|
|
|
. $propertyName . "' is not set!\n\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
// check our type
|
|
|
|
|
switch (strtolower($typeString)) {
|
|
|
|
|
case 'numeric':
|
|
|
|
|
if (!is_numeric($data->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '"
|
|
|
|
|
. $propertyName . "' is not of the correct type: "
|
2013-12-19 11:06:25 -04:00
|
|
|
. $typeString . "!\n\n");
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Then /^the response status code should be (\d+)$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseStatusCodeShouldBe($httpStatus)
|
|
|
|
|
{
|
2014-03-07 16:11:17 -04:00
|
|
|
if(!(isset($this->_response))){
|
|
|
|
|
throw new \Exception('HTTP code does not match ' . $httpStatus .
|
|
|
|
|
' (actual: No response defined)'
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-12-16 16:08:39 -04:00
|
|
|
if ((string)$this->_response->getStatusCode() !== $httpStatus) {
|
2014-01-16 08:57:30 -04:00
|
|
|
$message="";
|
|
|
|
|
if($bodyResponse=json_decode($this->_response->getBody(true))){
|
|
|
|
|
if(isset($bodyResponse->error->message)){
|
|
|
|
|
$message = $bodyResponse->error->message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2014-01-17 13:03:56 -04:00
|
|
|
|
2013-12-16 16:08:39 -04:00
|
|
|
throw new \Exception('HTTP code does not match ' . $httpStatus .
|
2014-01-16 08:57:30 -04:00
|
|
|
' (actual: ' . $this->_response->getStatusCode() . ") - $message\n\n"
|
2013-12-19 11:06:25 -04:00
|
|
|
);
|
2013-12-16 16:08:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 11:43:30 -04:00
|
|
|
/**
|
|
|
|
|
* @Then /^the response is equivalent to this json file "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseIsEquivalentToThisJsonFile($jsonFile)
|
|
|
|
|
{
|
|
|
|
|
//$this->_data;
|
|
|
|
|
$fileData = file_get_contents(__DIR__ . "/../json/" . $jsonFile);
|
|
|
|
|
$fileJson = json_decode($fileData);
|
|
|
|
|
if ($this->_data != $fileJson) {
|
2013-12-19 11:06:25 -04:00
|
|
|
throw new \Exception("JSON Response does not match json file: $jsonFile\n\n" );
|
2013-12-17 11:43:30 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 07:39:05 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to make a new "([^"]*)" with:$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToMakeANewWith($object, TableNode $table)
|
|
|
|
|
{
|
|
|
|
|
$rows = array();
|
|
|
|
|
foreach ($table->getHash() as $rowHash) {
|
|
|
|
|
printf ("%s %s \n", $rowHash['name'], $rowHash['followers'] );
|
|
|
|
|
//$user = new User();
|
|
|
|
|
//$user->setUsername($userHash['name']);
|
|
|
|
|
//$user->setFollowersCount($userHash['followers']);
|
|
|
|
|
//$users[] = $user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 15:53:36 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^POST this data:$/
|
|
|
|
|
*/
|
|
|
|
|
public function postThisData(PyStringNode $string)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
$this->_headers['Content-Type'] = 'application/json; charset=UTF-8';
|
|
|
|
|
$this->_requestBody = $string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^PUT this data:$/
|
|
|
|
|
*/
|
|
|
|
|
public function putThisData(PyStringNode $string)
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectMethod = 'put';
|
|
|
|
|
$this->_headers['Content-Type'] = 'application/json; charset=UTF-8';
|
|
|
|
|
$this->_requestBody = $string;
|
|
|
|
|
}
|
2014-01-07 10:37:50 -04:00
|
|
|
|
|
|
|
|
|
2013-12-17 15:53:36 -04:00
|
|
|
|
|
|
|
|
|
2013-12-17 07:39:05 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^I want to Insert a new "([^"]*)" with:$/
|
|
|
|
|
*/
|
|
|
|
|
public function iWantToInsertANewWith($url, PyStringNode $string)
|
|
|
|
|
{
|
|
|
|
|
//$this->_restObject = json_decode($string);
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
$this->_headers['Content-Type'] = 'application/json; charset=utf-8';
|
|
|
|
|
$this->_requestBody = $string;
|
|
|
|
|
$this->iRequest($url);
|
|
|
|
|
|
|
|
|
|
//$row = json_decode($string);
|
|
|
|
|
//print_r($row);
|
|
|
|
|
//print "************$string ***********";
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 15:53:36 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^store "([^"]*)" in session array$/
|
|
|
|
|
*/
|
|
|
|
|
public function storeIn($varName)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->_data->$varName)) {
|
2014-01-06 14:05:54 -04:00
|
|
|
throw new \Exception("JSON Response does not have '$varName' property\n\n" );
|
2013-12-17 15:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$varValue = $this->_data->$varName;
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = new StdClass();
|
|
|
|
|
}
|
|
|
|
|
$sessionData->$varName = $varValue;
|
|
|
|
|
file_put_contents("session.data", json_encode($sessionData));
|
|
|
|
|
}
|
2014-01-07 10:37:50 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^store "([^"]*)" in session array as variable "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function storeInAsVariable($varName, $sessionVarName)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->_data->$varName)) {
|
|
|
|
|
throw new \Exception("JSON Response does not have '$varName' property\n\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$varValue = $this->_data->$varName;
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = new StdClass();
|
|
|
|
|
}
|
|
|
|
|
$sessionData->$sessionVarName = $varValue;
|
|
|
|
|
file_put_contents("session.data", json_encode($sessionData));
|
|
|
|
|
}
|
2013-12-17 15:53:36 -04:00
|
|
|
|
2013-12-16 16:08:39 -04:00
|
|
|
/**
|
|
|
|
|
* @Then /^echo last response$/
|
|
|
|
|
*/
|
|
|
|
|
public function echoLastResponse()
|
|
|
|
|
{
|
|
|
|
|
$this->printDebug("$this->_request\n$this->_response");
|
|
|
|
|
}
|
2014-01-07 10:37:50 -04:00
|
|
|
|
|
|
|
|
|
2014-01-05 17:39:28 -04:00
|
|
|
//*********** WEN
|
2014-01-07 10:37:50 -04:00
|
|
|
|
2014-01-05 17:39:28 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^POST data from file "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function postDataFromFile($jsonFile)
|
|
|
|
|
{
|
|
|
|
|
$filePath = __DIR__ . "/../json/" . $jsonFile;
|
2014-01-07 10:37:50 -04:00
|
|
|
|
2014-01-05 17:39:28 -04:00
|
|
|
if(file_exists($filePath))
|
|
|
|
|
{
|
|
|
|
|
$fileData = file_get_contents($filePath);
|
|
|
|
|
$this->postThisData(new PyStringNode($fileData));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("JSON File: $filePath not found\n\n" );
|
|
|
|
|
}
|
|
|
|
|
// throw new PendingException();
|
|
|
|
|
}
|
2014-01-07 10:37:50 -04:00
|
|
|
|
2014-01-05 17:39:28 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^PUT data from file "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function putDataFromFile($jsonFile)
|
|
|
|
|
{
|
|
|
|
|
$filePath = __DIR__ . "/../json/" . $jsonFile;
|
2014-01-07 10:37:50 -04:00
|
|
|
|
2014-01-05 17:39:28 -04:00
|
|
|
if(file_exists($filePath))
|
|
|
|
|
{
|
|
|
|
|
$fileData = file_get_contents($filePath);
|
|
|
|
|
$this->putThisData(new PyStringNode($fileData));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("JSON File: $filePath not found\n\n" );
|
|
|
|
|
}
|
|
|
|
|
// throw new PendingException();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^This scenario is not implemented yet$/
|
|
|
|
|
* @Given /^this scenario is not implemented yet$/
|
|
|
|
|
*/
|
|
|
|
|
public function thisScenarioIsNotImplementedYet()
|
|
|
|
|
{
|
|
|
|
|
throw new PendingException();
|
|
|
|
|
}
|
2014-01-17 13:03:56 -04:00
|
|
|
|
2014-01-09 10:36:59 -04:00
|
|
|
/**
|
|
|
|
|
* @Then /^the response has (\d+) records$/
|
|
|
|
|
* @Then /^the response has (\d+) record$/
|
2014-04-02 09:07:48 -04:00
|
|
|
* @Then /^the response has (\d+) records in property "([^"]*)"$/
|
|
|
|
|
* @Then /^the response has (\d+) record in property "([^"]*)"$/
|
2014-01-09 10:36:59 -04:00
|
|
|
*/
|
2014-04-02 09:07:48 -04:00
|
|
|
public function theResponseHasRecords($quantityOfRecords, $responseProperty="")
|
2014-01-09 10:36:59 -04:00
|
|
|
{
|
2014-04-02 09:07:48 -04:00
|
|
|
if($responseProperty!=""){
|
|
|
|
|
if(!isset($this->_data->$responseProperty)){
|
|
|
|
|
throw new Exception("the Response data doesn't have a property named: $responseProperty\n\n" );
|
|
|
|
|
}
|
|
|
|
|
$data = $this->_data->$responseProperty;
|
|
|
|
|
}else{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-10 15:32:14 -04:00
|
|
|
if (!is_array($data)) {
|
2014-01-23 15:17:27 -04:00
|
|
|
if ($quantityOfRecords == 0) {
|
|
|
|
|
//if we expect 0 records and the response in fact is not an array, just return as a valid test
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("the Response data is not an array!\n\n" );
|
|
|
|
|
}
|
2014-01-10 15:32:14 -04:00
|
|
|
}
|
|
|
|
|
$currentRecordsCount=count($data);
|
|
|
|
|
if($currentRecordsCount!=$quantityOfRecords){
|
2014-01-09 10:36:59 -04:00
|
|
|
throw new Exception('Records quantity not match ' . $quantityOfRecords . ' (actual: ' . $currentRecordsCount . ")\n\n");
|
2014-01-10 15:32:14 -04:00
|
|
|
}
|
2014-01-09 10:36:59 -04:00
|
|
|
}
|
2014-01-07 10:37:50 -04:00
|
|
|
|
2014-01-14 11:57:46 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to update a resource with the key "([^"]*)" stored in session array as variable "([^"]*)"$/
|
2014-04-29 16:34:50 -04:00
|
|
|
* @Given /^that I want to update a resource with the key "([^"]*)" stored in session array as variable "([^"]*)" in position (\d+)$/
|
2014-01-14 11:57:46 -04:00
|
|
|
*/
|
2014-04-29 16:34:50 -04:00
|
|
|
public function thatIWantToUpdateAResourceWithTheKeyStoredInSessionArrayAsVariable($varName, $sessionVarName, $position=null)
|
2014-01-14 11:57:46 -04:00
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$sessionVarName) ) {
|
|
|
|
|
$varValue = '';
|
2014-04-29 16:34:50 -04:00
|
|
|
}elseif(!is_null($position)){
|
2014-05-05 09:06:34 -04:00
|
|
|
foreach ($sessionData->$sessionVarName as $key => $value) {
|
|
|
|
|
if($key == $position){
|
|
|
|
|
$varValue = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-14 11:57:46 -04:00
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$sessionVarName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restUpdateQueryStringSuffix = "/" . $varValue;
|
|
|
|
|
$this->_restObjectMethod = 'put';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to get a resource with the key "([^"]*)" stored in session array as variable "([^"]*)"$/
|
2014-05-05 09:06:34 -04:00
|
|
|
* @Given /^that I want to get a resource with the key "([^"]*)" stored in session array as variable "([^"]*)" in position (\d+)$/
|
2014-01-14 11:57:46 -04:00
|
|
|
*/
|
2014-04-29 16:34:50 -04:00
|
|
|
public function thatIWantToGetAResourceWithTheKeyStoredInSessionArrayAsVariable($varName, $sessionVarName, $position=null)
|
2014-01-14 11:57:46 -04:00
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$sessionVarName) ) {
|
|
|
|
|
$varValue = '';
|
2014-04-29 16:34:50 -04:00
|
|
|
}elseif(!is_null($position)){
|
2014-05-05 09:06:34 -04:00
|
|
|
foreach ($sessionData->$sessionVarName as $key => $value) {
|
|
|
|
|
if($key == $position){
|
|
|
|
|
$varValue = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-14 11:57:46 -04:00
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$sessionVarName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restGetQueryStringSuffix = "/" . $varValue;
|
|
|
|
|
$this->_restObjectMethod = 'get';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to delete a resource with the key "([^"]*)" stored in session array as variable "([^"]*)"$/
|
2014-04-29 16:34:50 -04:00
|
|
|
* @Given /^that I want to delete a resource with the key "([^"]*)" stored in session array as variable "([^"]*)" in position (\d+)$/
|
2014-01-14 11:57:46 -04:00
|
|
|
*/
|
2014-04-30 16:45:18 -04:00
|
|
|
public function thatIWantToDeleteAResourceWithTheKeyStoredInSessionArrayAsVariable($varName, $sessionVarName, $position=null)
|
2014-01-14 11:57:46 -04:00
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
2014-01-15 00:25:51 -04:00
|
|
|
if (!isset($sessionData->$sessionVarName) ) {
|
2014-01-14 11:57:46 -04:00
|
|
|
$varValue = '';
|
2014-04-29 16:34:50 -04:00
|
|
|
}elseif(!is_null($position)){
|
2014-05-05 09:06:34 -04:00
|
|
|
foreach ($sessionData->$sessionVarName as $key => $value) {
|
|
|
|
|
if($key == $position){
|
|
|
|
|
$varValue = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-14 11:57:46 -04:00
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$sessionVarName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->_restDeleteQueryStringSuffix = "/" . $varValue;
|
2014-03-27 09:40:24 -04:00
|
|
|
|
|
|
|
|
$this->printDebug("$varName = $varValue\nsessionVarName = $sessionVarName\n");
|
2014-03-05 09:58:35 -04:00
|
|
|
|
2014-01-14 11:57:46 -04:00
|
|
|
$this->_restObjectMethod = 'delete';
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 01:10:33 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^the response status message should have the following text "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseStatusMessageShouldHaveTheFollowingText($arg1)
|
|
|
|
|
{
|
2014-01-23 15:17:27 -04:00
|
|
|
|
2014-02-11 08:47:26 -04:00
|
|
|
if( $arg1!=""){
|
2014-01-21 01:10:33 -04:00
|
|
|
$message="";
|
|
|
|
|
if($bodyResponse=json_decode($this->_response->getBody(true))){
|
|
|
|
|
if(isset($bodyResponse->error->message)){
|
|
|
|
|
$message = $bodyResponse->error->message;
|
|
|
|
|
if (strpos($message,$arg1) === false) {
|
2014-02-12 09:33:52 -04:00
|
|
|
throw new \Exception("Error message text does not have: '" . $arg1 ."' (actual: '$message')\n\n");
|
|
|
|
|
}
|
|
|
|
|
}elseif(is_array($bodyResponse)){
|
|
|
|
|
$error_found=false;
|
|
|
|
|
$messages = array();
|
|
|
|
|
foreach($bodyResponse as $resp){
|
|
|
|
|
if(isset($resp->error)){
|
|
|
|
|
$messages[]=$resp->error;
|
|
|
|
|
if (strpos($resp->error,$arg1) !== false){
|
|
|
|
|
$error_found=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(!$error_found){
|
|
|
|
|
$message=implode("\n- ",$messages);
|
|
|
|
|
throw new \Exception("Error message text does not have: '" . $arg1 ."' \nCurrent messages: \n- $message\n\n");
|
2014-01-21 01:10:33 -04:00
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
throw new \Exception('This is not a valid error response');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
throw new \Exception('This is not a valid response');
|
|
|
|
|
|
|
|
|
|
}
|
2014-02-11 08:47:26 -04:00
|
|
|
}
|
2014-01-21 01:10:33 -04:00
|
|
|
|
2014-01-23 15:17:27 -04:00
|
|
|
|
|
|
|
|
|
2014-01-29 12:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^I request "([^"]*)" with the key "([^"]*)" stored in session array as variable "([^"]*)"$/
|
2014-02-05 15:39:42 -04:00
|
|
|
* @Given /^I request "([^"]*)" with the key "([^"]*)" stored in session array as variable "([^"]*)" and url is "([^"]*)"$/
|
2014-04-29 16:34:50 -04:00
|
|
|
* @Given /^I request "([^"]*)" with the key "([^"]*)" stored in session array as variable "([^"]*)"$/
|
|
|
|
|
* @Given /^I request "([^"]*)" with the key "([^"]*)" stored in session array as variable "([^"]*)" and url is "([^"]*)"$/
|
2014-01-29 12:09:13 -04:00
|
|
|
*/
|
2014-02-05 15:39:42 -04:00
|
|
|
public function iRequestWithTheKeyStoredInSessionArrayAsVariable($pageUrl, $varName, $sessionVarName, $urlType="")
|
2014-01-29 12:09:13 -04:00
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$sessionVarName) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$sessionVarName;
|
|
|
|
|
}
|
2014-02-11 17:00:26 -04:00
|
|
|
|
2014-01-29 12:09:13 -04:00
|
|
|
$pageUrl = str_replace($varName, $varValue, $pageUrl);
|
|
|
|
|
|
|
|
|
|
|
2014-03-27 09:40:24 -04:00
|
|
|
$this->printDebug("URL: $pageUrl\n$varName = $varValue\nsessionVarName = $sessionVarName\n");
|
2014-01-29 12:09:13 -04:00
|
|
|
|
|
|
|
|
|
2014-02-05 15:39:42 -04:00
|
|
|
$this->iRequest($pageUrl, $urlType);
|
2014-01-29 12:09:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-21 01:10:33 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-04 08:58:13 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^the property "([^"]*)" of "([^"]*)" is set to "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyOfIsSetTo($propertyName, $objName, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!isset($data->$objName)) {
|
|
|
|
|
throw new Exception("Object '$objName' is not set!\n\n" );
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data->$objName->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '$propertyName' is not set in object '$objName'!\n\n" );
|
|
|
|
|
}
|
|
|
|
|
if ($data->$objName->$propertyName != $propertyValue) {
|
|
|
|
|
throw new \Exception('Property value mismatch! (given: "'
|
|
|
|
|
. $propertyValue . '", match: "'
|
|
|
|
|
. $data->$objName->$propertyName . '")\n\n'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-14 11:57:46 -04:00
|
|
|
|
2014-02-11 17:00:26 -04:00
|
|
|
/**
|
|
|
|
|
* @When /^I request "([^"]*)" with the keys? "([^"]*)" stored in session array$/
|
|
|
|
|
*/
|
|
|
|
|
public function iRequestWithTheKeysStoredInSessionArray($url, $sessionVarName)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arraySessionVarName = explode(",", $sessionVarName);
|
|
|
|
|
|
|
|
|
|
foreach ($arraySessionVarName as $value) {
|
|
|
|
|
$varName = trim($value);
|
|
|
|
|
|
|
|
|
|
$varValue = (isset($sessionData->$varName))? $sessionData->$varName : "";
|
|
|
|
|
|
|
|
|
|
$url = str_replace($varName, $varValue, $url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->iRequest($url);
|
|
|
|
|
}
|
2014-02-21 09:00:17 -04:00
|
|
|
|
2014-02-26 12:22:22 -04:00
|
|
|
//UPLOAD FILE MANAGER
|
2014-02-21 09:00:17 -04:00
|
|
|
/**
|
2014-02-26 12:22:22 -04:00
|
|
|
* @Given /^POST I want to upload the file "([^"]*)" to path "([^"]*)". Url "([^"]*)"$/
|
2014-02-21 09:00:17 -04:00
|
|
|
*/
|
2014-02-26 12:22:22 -04:00
|
|
|
public function postIWantToUploadTheFileToPathPublicUrl($prfFile, $prfPath, $url)
|
2014-02-21 09:00:17 -04:00
|
|
|
{
|
2014-02-21 16:31:52 -04:00
|
|
|
$baseUrl = $this->getParameter('base_url');
|
|
|
|
|
$url = $baseUrl.$url;
|
2014-02-21 09:00:17 -04:00
|
|
|
$accesstoken = $this->getParameter('access_token');
|
|
|
|
|
$headr = array();
|
|
|
|
|
$headr[] = 'Authorization: Bearer '.$accesstoken;
|
|
|
|
|
$path = rtrim($prfPath, '/') . '/';
|
|
|
|
|
$sfile = end(explode("/",$prfFile));
|
|
|
|
|
$ch = curl_init();
|
2014-02-26 12:22:22 -04:00
|
|
|
curl_setopt($ch, CURLOPT_URL,$url);
|
2014-02-21 09:00:17 -04:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
|
2014-03-10 14:04:20 -04:00
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array('prf_filename'=>$sfile, "prf_path" => $path));
|
2014-02-21 09:00:17 -04:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
$postResult = curl_exec($ch);
|
2014-03-05 09:58:35 -04:00
|
|
|
|
2014-02-21 09:00:17 -04:00
|
|
|
curl_close($ch);
|
2014-03-05 09:58:35 -04:00
|
|
|
|
|
|
|
|
//Save result as usual
|
|
|
|
|
$this->_type = 'json';
|
|
|
|
|
$this->_data = json_decode($postResult);
|
|
|
|
|
|
2014-02-25 09:17:11 -04:00
|
|
|
$postResult = (array)json_decode($postResult);
|
2014-03-05 09:58:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-25 09:17:11 -04:00
|
|
|
if (sizeof($postResult) > 2) {
|
|
|
|
|
$prfUid = $postResult["prf_uid"];
|
|
|
|
|
} else {
|
2014-03-05 09:58:35 -04:00
|
|
|
throw new Exception($postResult["error"]->message);
|
|
|
|
|
//var_dump($postResult["error"]);
|
2014-02-25 09:17:11 -04:00
|
|
|
}
|
2014-02-26 12:22:22 -04:00
|
|
|
$url = $url.'/'.$prfUid."/upload";
|
2014-02-21 09:00:17 -04:00
|
|
|
$ch = curl_init();
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL,$url);
|
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
|
2014-02-25 09:17:11 -04:00
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array('prf_file'=>'@'.$prfFile));
|
2014-02-21 09:00:17 -04:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
$postResult = curl_exec($ch);
|
|
|
|
|
curl_close($ch);
|
2014-02-26 12:22:22 -04:00
|
|
|
|
|
|
|
|
//se guarda el prf_uid en una variable
|
2014-03-05 09:58:35 -04:00
|
|
|
|
|
|
|
|
//Wen: Esto borra todo el session data, por favor corregir o no guardar la variable desde aca
|
|
|
|
|
|
|
|
|
|
//$varName = 'prf_uid';
|
|
|
|
|
//$sessionData = new StdClass();
|
|
|
|
|
//$sessionData->$varName = $prfUid;
|
|
|
|
|
//file_put_contents("session.data", json_encode($sessionData));
|
2014-02-21 09:00:17 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-26 12:22:22 -04:00
|
|
|
//UPLOAD IMAGE
|
2014-02-21 09:00:17 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^POST I want to upload the image "([^"]*)" to user "([^"]*)". Url "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function postIWantToUploadTheImageToUser($imageFile, $usrUid, $url)
|
|
|
|
|
{
|
2014-02-21 16:31:52 -04:00
|
|
|
$baseUrl = $this->getParameter('base_url');
|
|
|
|
|
$url = $baseUrl.$url.$usrUid."/image-upload";
|
2014-02-25 11:15:21 -04:00
|
|
|
|
2014-02-21 09:00:17 -04:00
|
|
|
$accesstoken = $this->getParameter('access_token');
|
|
|
|
|
$headr = array();
|
|
|
|
|
$headr[] = 'Authorization: Bearer '.$accesstoken;
|
|
|
|
|
$ch = curl_init();
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL,$url);
|
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
|
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array('USR_PHOTO'=>'@'.$imageFile));
|
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
$postResult = curl_exec($ch);
|
2014-02-25 11:15:21 -04:00
|
|
|
|
|
|
|
|
if( $postResult === false)
|
|
|
|
|
{
|
|
|
|
|
//trigger_error(curl_error($ch));
|
|
|
|
|
throw new Exception("Image upload failed ($imageFile):\n\n"
|
|
|
|
|
. curl_error($ch));
|
|
|
|
|
}
|
2014-02-21 09:00:17 -04:00
|
|
|
curl_close($ch);
|
2014-02-26 12:22:22 -04:00
|
|
|
echo $postResult;
|
2014-02-21 09:00:17 -04:00
|
|
|
}
|
2014-02-11 17:00:26 -04:00
|
|
|
|
2014-02-25 11:15:21 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^POST I want to upload the image "([^"]*)" to user with the key "([^"]*)" stored in session array as variable "([^"]*)"\. Url "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function postIWantToUploadTheImageToUserWithTheKeyStoredInSessionArrayAsVariableUsrUidUrl($imageFile, $varName, $sessionVarName, $url)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$sessionVarName) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$sessionVarName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$usrUid = $varValue;
|
|
|
|
|
|
|
|
|
|
$this->postIWantToUploadTheImageToUser($imageFile, $usrUid, $url);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 16:02:35 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^that I want to delete the folder$/
|
|
|
|
|
*/
|
|
|
|
|
public function thatIWantToDeleteTheFolder()
|
|
|
|
|
{
|
|
|
|
|
$this->_restObjectMethod = 'delete';
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 09:40:24 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^store response count in session variable as "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function storeResponseCountInSessionVariableAs($varName)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
$currentRecordsCount=count($data);
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = new StdClass();
|
|
|
|
|
}
|
|
|
|
|
$sessionData->$varName = $currentRecordsCount;
|
|
|
|
|
file_put_contents("session.data", json_encode($sessionData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^the response has (\d+) records more than "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function theResponseHasRecordsMoreThan($records, $base)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = array();
|
|
|
|
|
}
|
|
|
|
|
if (!isset($sessionData->$base) ) {
|
|
|
|
|
$varValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
$varValue = $sessionData->$base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$totalRecords=$varValue + $records;
|
|
|
|
|
|
|
|
|
|
$this->theResponseHasRecords($totalRecords);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^POST upload an input document "([^"]*)" to "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function postUploadAnInputDocumentTo($file, $url, PyStringNode $string)
|
|
|
|
|
{
|
|
|
|
|
$postFields = json_decode($string);
|
|
|
|
|
$postFields->form ='@'.$file;
|
2014-04-01 08:52:37 -04:00
|
|
|
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
$this->_restObject = $postFields;
|
|
|
|
|
$this->iRequest($url);
|
2014-03-27 09:40:24 -04:00
|
|
|
|
2014-04-01 08:52:37 -04:00
|
|
|
|
2014-05-08 09:37:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Given /^POST upload a project file "([^"]*)" to "([^"]*)"$/
|
|
|
|
|
*/
|
2014-05-12 12:28:01 -04:00
|
|
|
public function postUploadAProjectFile($file, $url)
|
2014-05-08 09:37:44 -04:00
|
|
|
{
|
2014-05-12 12:28:01 -04:00
|
|
|
$postFields = new StdClass();
|
2014-05-08 09:37:44 -04:00
|
|
|
$postFields->project_file ='@'.$file;
|
|
|
|
|
|
|
|
|
|
$this->_restObjectMethod = 'post';
|
|
|
|
|
$this->_restObject = $postFields;
|
|
|
|
|
$this->iRequest($url);
|
|
|
|
|
|
|
|
|
|
|
2014-03-27 09:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-07 10:06:20 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^the "([^"]*)" property in object (\d+) of property "([^"]*)" equals "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function thePropertyInObjectOfPropertyEquals($propertyName, $row, $propertyParent, $propertyValue)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->_data;
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
throw new Exception("Response is empty or was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($data->$propertyParent)) {
|
|
|
|
|
throw new Exception("Response has not the property '$propertyParent'\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $data->$propertyParent;
|
|
|
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
|
if (!is_object($data)) {
|
|
|
|
|
throw new Exception("the $propertyParent in Response data is not an array!\n\n" );
|
|
|
|
|
}
|
|
|
|
|
if (is_object($data) && !isset($data->$row)) {
|
|
|
|
|
throw new Exception("the Response data is an array, but the row '$row' does not exists!\n\n" );
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data->$row->$propertyName)) {
|
|
|
|
|
throw new Exception("Property '"
|
|
|
|
|
. $propertyName . "' is not set!\n\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (is_array($data->$row->$propertyName)) {
|
|
|
|
|
throw new Exception("$propertyName is an array and we expected a value\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
if ($data->$row->$propertyName != $propertyValue) {
|
|
|
|
|
throw new \Exception('Property value mismatch! (given: '
|
|
|
|
|
. $propertyValue . ', match: '
|
|
|
|
|
. $data->$row->$propertyName . ")\n\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Response was not JSON\n\n"
|
|
|
|
|
. $this->_response->getBody(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 16:34:50 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^store "([^"]*)" in session array as variable "([^"]*)" where an object has "([^"]*)" equal to "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function storeInSessionArrayAsVariableWhereAnObjectHasEqualsTo($varName, $sessionVarName, $objectProperty, $objectValue)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$swFound=false;
|
|
|
|
|
if (file_exists("session.data")) {
|
|
|
|
|
$sessionData = json_decode(file_get_contents("session.data"));
|
|
|
|
|
} else {
|
|
|
|
|
$sessionData = new StdClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sessionData->$sessionVarName = array();
|
2014-04-07 10:06:20 -04:00
|
|
|
|
2014-04-29 16:34:50 -04:00
|
|
|
|
|
|
|
|
foreach($this->_data as $obj){
|
|
|
|
|
if((isset($obj->$objectProperty))&&($obj->$objectProperty == $objectValue)){
|
|
|
|
|
$swFound=true;
|
|
|
|
|
$varValue = $obj->$varName;
|
|
|
|
|
|
|
|
|
|
//$sessionData->$sessionVarName = $varValue;
|
|
|
|
|
$sessionData->{$sessionVarName}[] = $varValue;
|
|
|
|
|
file_put_contents("session.data", json_encode($sessionData));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!$swFound) {
|
|
|
|
|
//print_r($this->_data);
|
|
|
|
|
throw new \Exception("JSON Response does not have '$varName' property\n\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-19 10:42:08 -04:00
|
|
|
/**
|
|
|
|
|
* @Given /^save exported process to "([^"]*)"$/
|
|
|
|
|
* @Given /^save exported process to "([^"]*)" as "([^"]*)"$/
|
|
|
|
|
*/
|
|
|
|
|
public function saveExportedProcessTo($destinationFolder, $exportedProcessFileName="")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if($exportedProcessFileName == ""){//Obtain name from XML
|
|
|
|
|
$exportedProcessFileName=$this->_data->xpath('//metadata/meta[@key="name"]');
|
|
|
|
|
$exportedProcessFileName = $exportedProcessFileName[0];
|
|
|
|
|
$exportedProcessFileName = "ExpBehat ".$exportedProcessFileName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exportedProcessFileName = $destinationFolder.str_replace(" ","_",$exportedProcessFileName).".pmx";
|
|
|
|
|
|
|
|
|
|
$this->printDebug("Exporting process to: $exportedProcessFileName");
|
|
|
|
|
|
|
|
|
|
file_put_contents($exportedProcessFileName, $this->_response->getBody(true));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 16:34:50 -04:00
|
|
|
}
|