Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Chris Cornutt
file 209 lines (182 sloc) 6.249 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
<?php
use Behat\Behat\Context\BehatContext;
use Symfony\Component\Yaml\Yaml;

/**
* Features context.
*/
//class FeatureContextRest extends Behat\Mink\Behat\Context\MinkContext
class FeatureContextRest extends BehatContext
{

    private $_restObject = null;
    private $_restObjectType = null;
    private $_restObjectMethod = 'get';
    private $_client = null;
    private $_response = null;
    private $_requestUrl = null;

    private $_config = null;
    private $_configFile = 'behat.yml';

    /**
* 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)
    {
        // Initialize your context here

        $this->_restObject = new stdClass();
        $this->_client = new Guzzle\Service\Client();

        // pull in the config file
     $this->_config = Yaml::parse($this->_configFile);
    }

    public function getParameter($name)
    {
     if ($this->_config !== null) {

     $parameters = $this->_config['default']['context']['parameters'];
     return (isset($parameters[$name])) ? $parameters[$name] : null;

     } else {
     throw new \Exception('Configuration not loaded!');
     }
    }

     /**
* @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 find a "([^"]*)"$/
*/
    public function thatIWantToFindA($objectType)
    {
        $this->_restObjectType = ucwords(strtolower($objectType));
        $this->_restObjectMethod = 'get';
    }

    /**
* @Given /^that I want to delete a "([^"]*)"$/
*/
    public function thatIWantToDeleteA($objectType)
    {
        $this->_restObjectType = ucwords(strtolower($objectType));
        $this->_restObjectMethod = 'delete';
    }

    /**
* @Given /^that its "([^"]*)" is "([^"]*)"$/
*/
    public function thatTheItsIs($propertyName, $propertyValue)
    {
        $this->_restObject->$propertyName = $propertyValue;
    }

    /**
* @When /^I request "([^"]*)"$/
*/
    public function iRequest($pageUrl)
    {
        $baseUrl = $this->getParameter('base_url');
        $this->_requestUrl = $baseUrl.$pageUrl;

        switch (strtoupper($this->_restObjectMethod)) {
            case 'GET':
                $response = $this->_client
                    ->get($this->_requestUrl.'?'.http_build_str((array)$this->_restObject))
                    ->send();
                break;
            case 'POST':
                $postFields = (array)$this->_restObject;
                $response = $this->_client
                    ->post($this->_requestUrl,null,$postFields)
                    ->send();
                break;
            case 'DELETE':
             $response = $this->_client
                    ->delete($this->_requestUrl.'?'.http_build_str((array)$this->_restObject))
                    ->send();
             break;
        }
        $this->_response = $response;
    }

    /**
* @Then /^the response is JSON$/
*/
    public function theResponseIsJson()
    {
        $data = json_decode($this->_response->getBody(true));

        if (empty($data)) {
            throw new Exception("Response was not JSON\n" . $this->_response);
        }
    }

    /**
* @Given /^the response has a "([^"]*)" property$/
*/
    public function theResponseHasAProperty($propertyName)
    {
        $data = json_decode($this->_response->getBody(true));

        if (!empty($data)) {
            if (!isset($data->$propertyName)) {
                throw new Exception("Property '".$propertyName."' is not set!\n");
            }
        } else {
            throw new Exception("Response was not JSON\n" . $this->_response->getBody(true));
        }
    }

    /**
* @Then /^the "([^"]*)" property equals "([^"]*)"$/
*/
    public function thePropertyEquals($propertyName, $propertyValue)
    {
        $data = json_decode($this->_response->getBody(true));

        if (!empty($data)) {
         if (!isset($data->$propertyName)) {
                throw new Exception("Property '".$propertyName."' is not set!\n");
            }
            if ($data->$propertyName !== $propertyValue) {
             throw new \Exception('Property value mismatch! (given: '.$propertyValue.', match: '.$data->$propertyName.')');
            }
        } else {
            throw new Exception("Response was not JSON\n" . $this->_response->getBody(true));
        }
    }

    /**
* @Given /^the type of the "([^"]*)" property is ([^"]*)$/
*/
    public function theTypeOfThePropertyIsNumeric($propertyName,$typeString)
    {
        $data = json_decode($this->_response->getBody(true));

        if (!empty($data)) {
            if (!isset($data->$propertyName)) {
                throw new Exception("Property '".$propertyName."' is not set!\n");
            }
            // check our type
            switch (strtolower($typeString)) {
                case 'numeric':
                    if (!is_numeric($data->$propertyName)) {
                        throw new Exception("Property '".$propertyName."' is not of the correct type: ".$theTypeOfThePropertyIsNumeric."!\n");
                    }
                    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)
    {
        if ((string)$this->_response->getStatusCode() !== $httpStatus) {
         throw new \Exception('HTTP code does not match '.$httpStatus.
         ' (actual: '.$this->_response->getStatusCode().')');
        }
    }

     /**
* @Then /^echo last response$/
*/
    public function echoLastResponse()
    {
        $this->printDebug(
            $this->_requestUrl."\n\n".
            $this->_response
        );
    }
}
Something went wrong with that request. Please try again.