Completed
Push — master ( ded910...421c50 )
by Sébastien
02:28
created

RestApiContext   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 15 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 6
dl 24
loc 160
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A iSendARequest() 0 4 1
A iSendARequestWithBody() 0 4 1
A iSendAPostRequestToWithFormData() 0 4 1
A theResponseCodeShouldBe() 0 6 1
A getResponse() 0 4 1
A iSetHeaderEqualTo() 0 4 1
A iAddHeaderEqualTo() 0 4 1
A iSetBasicAuthenticationWithAnd() 0 5 1
A printRequestAndResponse() 0 7 1
A printRequest() 11 11 1
A getRequest() 0 4 1
A getRawHeaders() 0 10 3
A printResponse() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * Sends HTTP request to specific URL with POST parameters.
53
     *
54
     * @When I send a :method request to :url with form data:
55
     */
56
    public function iSendAPostRequestToWithFormData($method, $url, TableNode $formData)
57
    {
58
        $this->restApiBrowser->sendRequest($method, $url, $formData->getRowsHash());
59
    }
60
61
    /**
62
     * @param string $code status code
63
     *
64
     * @Then /^(?:the )?response status code should be (\d+)$/
65
     */
66
    public function theResponseCodeShouldBe($code)
67
    {
68
        $expected = intval($code);
69
        $actual = intval($this->getResponse()->getStatusCode());
70
        $this->asserter->variable($actual)->isEqualTo($expected);
71
    }
72
73
    /**
74
     * @return ResponseInterface
75
     */
76
    private function getResponse()
77
    {
78
        return $this->restApiBrowser->getResponse();
79
    }
80
81
    /**
82
     * @Given /^I set "([^"]*)" header equal to "([^"]*)"$/
83
     */
84
    public function iSetHeaderEqualTo($headerName, $headerValue)
85
    {
86
        $this->restApiBrowser->setRequestHeader($headerName, $headerValue);
87
    }
88
89
    /**
90
     * @Given /^I add "([^"]*)" header equal to "([^"]*)"$/
91
     */
92
    public function iAddHeaderEqualTo($headerName, $headerValue)
93
    {
94
        $this->restApiBrowser->addRequestHeader($headerName, $headerValue);
95
    }
96
97
    /**
98
     * Set login / password for next HTTP authentication
99
     *
100
     * @When /^I set basic authentication with "(?P<username>[^"]*)" and "(?P<password>[^"]*)"$/
101
     */
102
    public function iSetBasicAuthenticationWithAnd($username, $password)
103
    {
104
        $authorization = base64_encode($username . ':' . $password);
105
        $this->restApiBrowser->setRequestHeader('Authorization', 'Basic ' . $authorization);
106
    }
107
108
    /**
109
     * @Then print request and response
110
     */
111
    public function printRequestAndResponse()
112
    {
113
        echo "REQUEST:\n";
114
        $this->printRequest();
115
        echo "\nRESPONSE:\n";
116
        $this->printResponse();
117
    }
118
119
    /**
120
     * @Then print request
121
     */
122 View Code Duplication
    public function printRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $request = $this->getRequest();
125
        echo sprintf(
126
            "%s %s :\n%s%s\n",
127
            $request->getMethod(),
128
            $request->getUri(),
129
            $this->getRawHeaders($request->getHeaders()),
130
            $request->getBody()
131
        );
132
    }
133
134
    /**
135
     * @return RequestInterface
136
     */
137
    private function getRequest()
138
    {
139
        return $this->restApiBrowser->getRequest();
140
    }
141
142
    /**
143
     * @param array $headers
144
     * @return string
145
     */
146
    private function getRawHeaders(array $headers)
147
    {
148
        $rawHeaders = '';
149
        foreach ($headers as $key => $value) {
150
            $rawHeaders .= sprintf("%s: %s\n", $key, is_array($value) ? implode(", ", $value) : $value);
151
152
        }
153
        $rawHeaders .= "\n";
154
        return $rawHeaders;
155
    }
156
157
    /**
158
     * @Then print response
159
     */
160 View Code Duplication
    public function printResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $request = $this->getRequest();
163
        $response = $this->getResponse();
164
165
        echo sprintf(
166
            "%s %s :\n%s%s\n",
167
            $request->getMethod(),
168
            $request->getUri()->__toString(),
169
            $this->getRawHeaders($response->getHeaders()),
170
            $response->getBody()
171
        );
172
    }
173
}
174