Completed
Pull Request — master (#65)
by Timothée
01:57
created

RestApiContext::theResponseCodeShouldBe()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 7
nop 1
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
0 ignored issues
show
Deprecated Code introduced by
The interface Behat\Behat\Context\SnippetAcceptingContext has been deprecated with message: will be removed in 4.0. Use --snippets-for CLI option instead

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.

Loading history...
15
{
16
    private $asserter;
17
18
    private $restApiBrowser;
19
20
    private $requestHeaders = [];
21
22
    public function __construct(RestApiBrowser $restApiBrowser)
23
    {
24
        $this->restApiBrowser = $restApiBrowser;
25
        $this->asserter = new asserter\generator;
26
    }
27
28
    /**
29
     * @param string $method request method
30
     * @param string $url    relative url
31
     *
32
     * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/
33
     */
34
    public function iSendARequest($method, $url)
35
    {
36
        $this->restApiBrowser->sendRequest($method, $url, null, $this->requestHeaders);
37
    }
38
39
    /**
40
     * Sends HTTP request to specific URL with raw body from PyString.
41
     *
42
     * @param string       $method request method
43
     * @param string       $url relative url
44
     * @param PyStringNode $body
45
     *
46
     * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/
47
     */
48
    public function iSendARequestWithBody($method, $url, PyStringNode $body)
49
    {
50
        $this->restApiBrowser->sendRequest($method, $url, (string) $body, $this->requestHeaders);
51
    }
52
53
    /**
54
     * @param string $code status code
55
     *
56
     * @Then /^(?:the )?response status code should be (\d+)$/
57
     */
58
    public function theResponseCodeShouldBe($code)
59
    {
60
        $expected = intval($code);
61
        $actual = intval($this->getResponse()->getStatusCode());
62
        try {
63
            $this->asserter->variable($actual)->isEqualTo($expected);
64
        } catch (\Exception $e) {
65
            throw new Rest\WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e);
66
        }
67
    }
68
69
    /**
70
     * @return ResponseInterface
71
     */
72
    private function getResponse()
73
    {
74
        return $this->restApiBrowser->getResponse();
75
    }
76
77
    /**
78
     * @Given /^I set "([^"]*)" header equal to "([^"]*)"$/
79
     */
80
    public function iSetHeaderEqualTo($headerName, $headerValue)
81
    {
82
        $this->removeRequestHeader($headerName);
83
        $this->addRequestHeader($headerName, $headerValue);
84
    }
85
86
    /**
87
     * @Given /^I add "([^"]*)" header equal to "([^"]*)"$/
88
     */
89
    public function iAddHeaderEqualTo($headerName, $headerValue)
90
    {
91
        $this->addRequestHeader($headerName, $headerValue);
92
    }
93
94
    /**
95
     * Set login / password for next HTTP authentication
96
     *
97
     * @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/
98
     */
99
    public function iSetBasicAuthenticationWithAnd($username, $password)
100
    {
101
        $authorization = base64_encode($username . ':' . $password);
102
        $this->setRequestHeader('Authorization', 'Basic ' . $authorization);
0 ignored issues
show
Bug introduced by
The method setRequestHeader() does not seem to exist on object<Rezzza\RestApiBeh...tension\RestApiContext>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
    }
104
105
    /**
106
     * @Then print request and response
107
     */
108
    public function printRequestAndResponse()
109
    {
110
        $formatter = $this->buildHttpExchangeFormatter();
111
        echo "REQUEST:\n";
112
        echo $formatter->formatRequest();
113
        echo "\nRESPONSE:\n";
114
        echo $formatter->formatFullExchange();
115
    }
116
117
    /**
118
     * @Then print request
119
     */
120
    public function printRequest()
121
    {
122
        echo $this->buildHttpExchangeFormatter()->formatRequest();
123
    }
124
125
    /**
126
     * @Then print response
127
     */
128
    public function printResponse()
129
    {
130
        echo $this->buildHttpExchangeFormatter()->formatFullExchange();
131
    }
132
133
    private function buildHttpExchangeFormatter()
134
    {
135
        return new Rest\HttpExchangeFormatter($this->restApiBrowser->getRequest(), $this->getResponse());
136
    }
137
138
    /**
139
     * @param string $name
140
     * @param string $value
141
     */
142
    private function addRequestHeader($name, $value)
143
    {
144
        if (isset($this->requestHeaders[$name])) {
145
            $this->requestHeaders[$name] .= ', '.$value;
146
        } else {
147
            $this->requestHeaders[$name] = $value;
148
        }
149
    }
150
151
    /**
152
     * @param string $headerName
153
     */
154
    private function removeRequestHeader($headerName)
155
    {
156
        if (array_key_exists($headerName, $this->requestHeaders)) {
157
            unset($this->requestHeaders[$headerName]);
158
        }
159
    }
160
}
161