source: sandbox/webservice/api/library/tonic/features/bootstrap/FeatureContext.php @ 6019

Revision 6019, 8.1 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Modificada implementação para alinhamento com projeto de camada REST.

  • Property svn:executable set to *
Line 
1<?php
2
3use Behat\Behat\Context\ClosuredContextInterface,
4    Behat\Behat\Context\TranslatedContextInterface,
5    Behat\Behat\Context\BehatContext,
6    Behat\Behat\Exception\PendingException;
7use Behat\Gherkin\Node\PyStringNode,
8    Behat\Gherkin\Node\TableNode;
9
10/**
11 * Features context.
12 */
13class FeatureContext extends BehatContext
14{
15    /**
16     * Initializes context.
17     * Every scenario gets it's own context object.
18     *
19     * @param   array   $parameters     context parameters (set them up through behat.yml)
20     */
21    public function __construct(array $parameters)
22    {
23        // Initialize your context here
24                $this->config = array();
25    }
26
27    /**
28     * @Given /^the request URI of "([^"]*)"$/
29     */
30    public function theRequestUriOf($uri)
31    {
32                $this->config['uri'] = $uri;
33    }
34
35    /**
36     * @When /^I create a request object$/
37     */
38    public function iCreateARequestObject()
39    {
40                $this->request = new Request($this->config);
41    }
42
43    /**
44     * @Then /^I should see a request URI of "([^"]*)"$/
45     */
46    public function iShouldSeeARequestUriOf($uri)
47    {
48                if ($this->request->uri != $uri)
49                        throw new Exception;
50    }
51
52    /**
53     * @Given /^the request method of "([^"]*)"$/
54     */
55    public function theRequestMethodOf($method)
56    {
57                $this->config['method'] = $method;
58    }
59
60    /**
61     * @Then /^I should see a request method of "([^"]*)"$/
62     */
63    public function iShouldSeeARequestMethodOf($method)
64    {
65                if ($this->request->method != $method)
66                        throw new Exception;
67    }
68
69    /**
70     * @Given /^the request data of "([^"]*)"$/
71     */
72    public function theRequestDataOf($data)
73    {
74                $this->config['data'] = $data;
75    }
76
77    /**
78     * @Given /^I should see the request data "([^"]*)"$/
79     */
80    public function iShouldSeeTheRequestData($data)
81    {
82                if($this->request->data != $data)
83                        throw new Exception;
84    }
85
86    /**
87     * @Then /^I should see a negotiated URI of "([^"]*)"$/
88     */
89    public function iShouldSeeANegotiatedUriOf($uri)
90    {
91                if($this->request->negotiatedUris != explode(',', $uri))
92                        throw new Exception;
93    }
94
95    /**
96     * @Then /^I should see a format negotiated URI of "([^"]*)"$/
97     */
98    public function iShouldSeeAFormatNegotiatedUriOf($uri)
99    {
100                if($this->request->formatNegotiatedUris != explode(',', $uri))
101                        throw new Exception;
102    }
103
104    /**
105     * @Then /^I should see a language negotiated URI of "([^"]*)"$/
106     */
107    public function iShouldSeeALanguageNegotiatedUriOf($uri)
108    {
109                if($this->request->languageNegotiatedUris != explode(',', $uri))
110                        throw new Exception;
111    }
112
113    /**
114     * @Given /^the accept header of "([^"]*)"$/
115     */
116    public function theAcceptHeaderOf($header)
117    {
118                $this->config['accept'] = $header;
119    }
120
121    /**
122     * @Given /^the language accept header of "([^"]*)"$/
123     */
124    public function theLanguageAcceptHeaderOf($header)
125    {
126                $this->config['acceptLang'] = $header;
127    }
128
129    /**
130     * @Given /^an if match header of '([^']*)'$/
131     */
132    public function anIfMatchHeaderOf($header)
133    {
134                $this->config['ifMatch'] = $header;
135    }
136
137    /**
138     * @Then /^I should see an if match header of "([^"]*)"$/
139     */
140    public function iShouldSeeAnIfMatchHeaderOf($header)
141    {
142                if($this->request->ifMatch != explode(',', $header))
143                        throw new Exception;
144    }
145
146    /**
147     * @Then /^if match should match "([^"]*)"$/
148     */
149    public function ifMatchShouldMatch($match)
150    {
151                if(!$this->request->ifMatch($match))
152                        throw new Exception;
153    }
154
155    /**
156     * @Given /^an if none match header of '([^']*)'$/
157     */
158    public function anIfNoneMatchHeaderOf($header)
159    {
160                $this->config['ifNoneMatch'] = $header;
161    }
162
163    /**
164     * @Then /^I should see an if none match header of "([^"]*)"$/
165     */
166    public function iShouldSeeAnIfNoneMatchHeaderOf($header)
167    {
168                if($this->request->ifNoneMatch != explode(',', $header))
169                        throw new Exception;
170    }
171
172    /**
173     * @Then /^if none match should match "([^"]*)"$/
174     */
175    public function ifNoneMatchShouldMatch($match)
176    {
177                if(!$this->request->ifNoneMatch($match))
178                        throw new Exception;
179    }
180
181    /**
182     * @Then /^if none match should not match "([^"]*)"$/
183     */
184    public function ifNoneMatchShouldNotMatch($match)
185    {
186                if($this->request->ifNoneMatch($match))
187                        throw new Exception;
188    }
189
190    /**
191     * @Given /^I load the resource$/
192     */
193    public function iLoadTheResource()
194    {
195                $this->resource = $this->request->loadResource();
196    }
197
198    /**
199     * @Then /^I should fail to load the resource$/
200     */
201    public function iShouldFailToLoadTheResource()
202    {
203                try {
204                        $this->request->loadResource();
205                } catch(ResponseException $e) {
206                        if($e->getCode() != Response::NOTFOUND)
207                                throw new Exception;
208                }
209    }
210
211    /**
212     * @Then /^I should have a response of type "([^"]*)"$/
213     */
214    public function iShouldHaveAResponseOfType($type)
215    {
216                if(get_class($this->resource != $type))
217                        throw new Exception;
218    }
219
220    /**
221     * @Then /^I should see resource "([^"]*)" metadata of "([^"]*)"$/
222     */
223    public function iShouldSeeResourceMetadataOf($argument1, $argument2)
224    {
225                if($this->request->resources[$this->request->uri][$argument1] != $argument2)
226                        throw new Exception("metadata $argument1: want $argument2, got {$this->request->resources[$this->request->uri][$argument1]}");
227    }
228
229    /**
230     * @Given /^a mounting of "([^"]*)" to "([^"]*)"$/
231     */
232    public function aMountingOfTo($argument1, $argument2)
233    {
234                $this->config['mount'][$argument1] = $argument2;
235    }
236
237    /**
238     * @Given /^execute the request$/
239     */
240    public function executeTheRequest()
241    {
242        $this->response = $this->resource->exec($this->request);
243    }
244
245    /**
246     * @Given /^an accept encoding of "([^"]*)"$/
247     */
248    public function anAcceptEncodingOf($encoding)
249    {
250                $this->config['acceptEncoding'] = $encoding;
251    }
252
253#    /**
254#     * @Given /^I process content encoding$/
255#     */
256#    public function iProcessContentEncoding()
257#    {
258#               $this->response->doContentEncoding();
259#    }
260
261    /**
262     * @Then /^the response header "([^"]*)" should contain '([^']*)'$/
263     */
264    public function theResponseHeaderShouldContain($header, $contents)
265    {
266                if($this->response->headers[$header] != $contents)
267                        throw new Exception;
268    }
269
270#    /**
271#     * @Then /^the response body should be ([^ ]*) and be "([^"]*)"$/
272#     */
273#    public function theResponseBodyShouldBeTransformedAndBe($transform, $contents)
274#    {
275#       switch ($transform) {
276#       case 'gzipped':
277##          var_dump($this->response->body, $contents, gzencode($contents));
278#           if ($this->response->body != gzencode($contents)) throw new Exception;
279#           break;
280#       case 'deflated':
281#           if ($this->response->body != gzdeflate($contents)) throw new Exception;
282#           break;
283#       case 'compressed':
284#           if ($this->response->body != gzcompress($contents)) throw new Exception;
285#           break;
286#       }
287#    }
288
289    /**
290     * @Given /^I add a cache header of "([^"]*)"$/
291     */
292    public function iAddACacheHeaderOf($header)
293        {
294                if($header == '') {
295                        $this->response->addCacheHeader();
296                } else {
297                        $this->response->addCacheHeader($header);
298                }
299        }
300
301    /**
302     * @Given /^I add an etag header of "([^"]*)"$/
303     */
304    public function iAddAnEtagHeaderOf($etag)
305    {
306                $this->response->addEtag($etag);
307    }
308
309    /**
310     * @Then /^I should fail to load the resource with response code "([^"]*)" and body '([^']*)'$/
311     */
312    public function iShouldFailToLoadTheResourceWithResponseCodeAndBody($code, $body)
313    {
314                try {
315                        $this->request->loadResource();
316                } catch(Exception $e) {
317                        if($e->getCode() != $code)
318                                throw new Exception('bad code ' . $e->getCode() . " (want $code)");
319                        if($e->getMessage() != $body)
320                                throw new Exception('bad error message ' . $e->getMessage() . " (want $body)");
321                }
322    }
323
324    /**
325     * @Then /^the response code should be "([^"]*)"$/
326     */
327    public function theResponseCodeShouldBe($code)
328    {
329                if($this->response->code != $code)
330                        throw new Exception;
331    }
332
333    /**
334     * @Given /^the response body should be '([^']*)'$/
335     */
336    public function theResponseBodyShouldBe($body)
337    {
338                if($this->response->body != $body)
339                        throw new Exception;
340    }
341
342}
Note: See TracBrowser for help on using the repository browser.