Skip to content
Newer
Older
100644 209 lines (182 sloc) 6.1 KB
d7d5f83 @enygma initial commit of behat tests and FuelPHP model/controller
authored Mar 27, 2012
1 <?php
2 use Behat\Behat\Context\BehatContext;
3 use Symfony\Component\Yaml\Yaml;
4
5 /**
6 * Features context.
7 */
8 //class FeatureContextRest extends Behat\Mink\Behat\Context\MinkContext
9 class FeatureContextRest extends BehatContext
10 {
11
12 private $_restObject = null;
13 private $_restObjectType = null;
14 private $_restObjectMethod = 'get';
15 private $_client = null;
16 private $_response = null;
17 private $_requestUrl = null;
18
19 private $_config = null;
20 private $_configFile = 'behat.yml';
21
22 /**
23 * Initializes context.
24 * Every scenario gets it's own context object.
25 *
26 * @param array $parameters context parameters (set them up through behat.yml)
27 */
28 public function __construct(array $parameters)
29 {
30 // Initialize your context here
31
32 $this->_restObject = new stdClass();
33 $this->_client = new Guzzle\Service\Client();
34
35 // pull in the config file
36 $this->_config = Yaml::parse($this->_configFile);
37 }
38
39 public function getParameter($name)
40 {
41 if ($this->_config !== null) {
42
43 $parameters = $this->_config['default']['context']['parameters'];
44 return (isset($parameters[$name])) ? $parameters[$name] : null;
45
46 } else {
47 throw new \Exception('Configuration not loaded!');
48 }
49 }
50
51 /**
52 * @Given /^that I want to make a new "([^"]*)"$/
53 */
54 public function thatIWantToMakeANew($objectType)
55 {
56 $this->_restObjectType = ucwords(strtolower($objectType));
57 $this->_restObjectMethod = 'post';
58 }
59
60 /**
61 * @Given /^that I want to find a "([^"]*)"$/
62 */
63 public function thatIWantToFindA($objectType)
64 {
65 $this->_restObjectType = ucwords(strtolower($objectType));
66 $this->_restObjectMethod = 'get';
67 }
68
69 /**
70 * @Given /^that I want to delete a "([^"]*)"$/
71 */
72 public function thatIWantToDeleteA($objectType)
73 {
74 $this->_restObjectType = ucwords(strtolower($objectType));
75 $this->_restObjectMethod = 'delete';
76 }
77
78 /**
79 * @Given /^that its "([^"]*)" is "([^"]*)"$/
80 */
81 public function thatTheItsIs($propertyName, $propertyValue)
82 {
83 $this->_restObject->$propertyName = $propertyValue;
84 }
85
86 /**
87 * @When /^I request "([^"]*)"$/
88 */
89 public function iRequest($pageUrl)
90 {
91 $baseUrl = $this->getParameter('base_url');
92 $this->_requestUrl = $baseUrl.$pageUrl;
93
94 switch (strtoupper($this->_restObjectMethod)) {
95 case 'GET':
96 $response = $this->_client
97 ->get($this->_requestUrl.'?'.http_build_str((array)$this->_restObject))
98 ->send();
99 break;
100 case 'POST':
101 $postFields = (array)$this->_restObject;
102 $response = $this->_client
103 ->post($this->_requestUrl,null,$postFields)
104 ->send();
105 break;
106 case 'DELETE':
107 $response = $this->_client
108 ->delete($this->_requestUrl.'?'.http_build_str((array)$this->_restObject))
109 ->send();
110 break;
111 }
112 $this->_response = $response;
113 }
114
115 /**
116 * @Then /^the response is JSON$/
117 */
118 public function theResponseIsJson()
119 {
120 $data = json_decode($this->_response->getBody(true));
121
122 if (empty($data)) {
123 throw new Exception("Response was not JSON\n" . $this->_response);
124 }
125 }
126
127 /**
128 * @Given /^the response has a "([^"]*)" property$/
129 */
130 public function theResponseHasAProperty($propertyName)
131 {
132 $data = json_decode($this->_response->getBody(true));
133
134 if (!empty($data)) {
135 if (!isset($data->$propertyName)) {
136 throw new Exception("Property '".$propertyName."' is not set!\n");
137 }
138 } else {
139 throw new Exception("Response was not JSON\n" . $this->_response->getBody(true));
140 }
141 }
142
143 /**
144 * @Then /^the "([^"]*)" property equals "([^"]*)"$/
145 */
146 public function thePropertyEquals($propertyName, $propertyValue)
147 {
148 $data = json_decode($this->_response->getBody(true));
149
150 if (!empty($data)) {
151 if (!isset($data->$propertyName)) {
152 throw new Exception("Property '".$propertyName."' is not set!\n");
153 }
154 if ($data->$propertyName !== $propertyValue) {
155 throw new \Exception('Property value mismatch! (given: '.$propertyValue.', match: '.$data->$propertyName.')');
156 }
157 } else {
158 throw new Exception("Response was not JSON\n" . $this->_response->getBody(true));
159 }
160 }
161
162 /**
163 * @Given /^the type of the "([^"]*)" property is ([^"]*)$/
164 */
165 public function theTypeOfThePropertyIsNumeric($propertyName,$typeString)
166 {
167 $data = json_decode($this->_response->getBody(true));
168
169 if (!empty($data)) {
170 if (!isset($data->$propertyName)) {
171 throw new Exception("Property '".$propertyName."' is not set!\n");
172 }
173 // check our type
174 switch (strtolower($typeString)) {
175 case 'numeric':
176 if (!is_numeric($data->$propertyName)) {
177 throw new Exception("Property '".$propertyName."' is not of the correct type: ".$theTypeOfThePropertyIsNumeric."!\n");
178 }
179 break;
180 }
181
182 } else {
183 throw new Exception("Response was not JSON\n" . $this->_response->getBody(true));
184 }
185 }
186
187 /**
188 * @Then /^the response status code should be (\d+)$/
189 */
190 public function theResponseStatusCodeShouldBe($httpStatus)
191 {
192 if ((string)$this->_response->getStatusCode() !== $httpStatus) {
193 throw new \Exception('HTTP code does not match '.$httpStatus.
194 ' (actual: '.$this->_response->getStatusCode().')');
195 }
196 }
197
198 /**
199 * @Then /^echo last response$/
200 */
201 public function echoLastResponse()
202 {
203 $this->printDebug(
204 $this->_requestUrl."\n\n".
205 $this->_response
206 );
207 }
208 }
Something went wrong with that request. Please try again.