1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rezzza\RestApiBehatExtension; |
4
|
|
|
|
5
|
|
|
use mageekguy\atoum\asserter; |
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
9
|
|
|
use Behat\Gherkin\Node\TableNode; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser; |
13
|
|
|
|
14
|
|
|
class RestApiContext implements Context, SnippetAcceptingContext |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
private $asserter; |
17
|
|
|
|
18
|
|
|
private $restApiBrowser; |
19
|
|
|
|
20
|
|
|
public function __construct(RestApiBrowser $restApiBrowser) |
21
|
|
|
{ |
22
|
|
|
$this->restApiBrowser = $restApiBrowser; |
23
|
|
|
$this->asserter = new asserter\generator; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $method request method |
28
|
|
|
* @param string $url relative url |
29
|
|
|
* |
30
|
|
|
* @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/ |
31
|
|
|
*/ |
32
|
|
|
public function iSendARequest($method, $url) |
33
|
|
|
{ |
34
|
|
|
$this->restApiBrowser->sendRequest($method, $url); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sends HTTP request to specific URL with raw body from PyString. |
39
|
|
|
* |
40
|
|
|
* @param string $method request method |
41
|
|
|
* @param string $url relative url |
42
|
|
|
* @param PyStringNode $body |
43
|
|
|
* |
44
|
|
|
* @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/ |
45
|
|
|
*/ |
46
|
|
|
public function iSendARequestWithBody($method, $url, PyStringNode $body) |
47
|
|
|
{ |
48
|
|
|
$this->restApiBrowser->sendRequest($method, $url, (string) $body); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $code status code |
53
|
|
|
* |
54
|
|
|
* @Then /^(?:the )?response status code should be (\d+)$/ |
55
|
|
|
*/ |
56
|
|
|
public function theResponseCodeShouldBe($code) |
57
|
|
|
{ |
58
|
|
|
$expected = intval($code); |
59
|
|
|
$actual = intval($this->getResponse()->getStatusCode()); |
60
|
|
|
try { |
61
|
|
|
$this->asserter->variable($actual)->isEqualTo($expected); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
throw new WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return ResponseInterface |
69
|
|
|
*/ |
70
|
|
|
private function getResponse() |
71
|
|
|
{ |
72
|
|
|
return $this->restApiBrowser->getResponse(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Given /^I set "([^"]*)" header equal to "([^"]*)"$/ |
77
|
|
|
*/ |
78
|
|
|
public function iSetHeaderEqualTo($headerName, $headerValue) |
79
|
|
|
{ |
80
|
|
|
$this->restApiBrowser->setRequestHeader($headerName, $headerValue); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Given /^I add "([^"]*)" header equal to "([^"]*)"$/ |
85
|
|
|
*/ |
86
|
|
|
public function iAddHeaderEqualTo($headerName, $headerValue) |
87
|
|
|
{ |
88
|
|
|
$this->restApiBrowser->addRequestHeader($headerName, $headerValue); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set login / password for next HTTP authentication |
93
|
|
|
* |
94
|
|
|
* @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/ |
95
|
|
|
*/ |
96
|
|
|
public function iSetBasicAuthenticationWithAnd($username, $password) |
97
|
|
|
{ |
98
|
|
|
$authorization = base64_encode($username . ':' . $password); |
99
|
|
|
$this->restApiBrowser->setRequestHeader('Authorization', 'Basic ' . $authorization); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @Then print request and response |
104
|
|
|
*/ |
105
|
|
|
public function printRequestAndResponse() |
106
|
|
|
{ |
107
|
|
|
$formatter = $this->buildHttpExchangeFormatter(); |
108
|
|
|
echo "REQUEST:\n"; |
109
|
|
|
echo $formatter->formatRequest(); |
110
|
|
|
echo "\nRESPONSE:\n"; |
111
|
|
|
echo $formatter->formatFullExchange(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @Then print request |
116
|
|
|
*/ |
117
|
|
|
public function printRequest() |
118
|
|
|
{ |
119
|
|
|
echo $this->buildHttpExchangeFormatter()->formatRequest(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @Then print response |
124
|
|
|
*/ |
125
|
|
|
public function printResponse() |
126
|
|
|
{ |
127
|
|
|
echo $this->buildHttpExchangeFormatter()->formatFullExchange(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function buildHttpExchangeFormatter() |
131
|
|
|
{ |
132
|
|
|
return new Rest\HttpExchangeFormatter($this->restApiBrowser->getRequest(), $this->getResponse()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.