Passed
Pull Request — master (#218)
by
unknown
02:42
created

RestContext   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 5
dl 0
loc 244
rs 9.8
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iSendARequestTo() 0 10 2
B iSendARequestToWithParameters() 0 27 6
A iSendARequestToWithBody() 0 4 1
A theResponseShouldBeEqualTo() 0 7 1
A theResponseShouldBeEmpty() 0 6 2
A theHeaderShouldBeEqualTo() 0 7 1
A theHeaderShouldNotBeEqualTo() 0 9 2
A theHeaderShouldContain() 0 7 1
A theHeaderShouldNotContain() 0 6 1
A theHeaderShouldNotExist() 0 6 1
A theHeaderShouldExist() 0 4 1
A theResponseShouldExpireInTheFuture() 0 9 1
A iAddHeaderEqualTo() 0 4 1
A theResponseShouldBeEncodedIn() 0 9 2
A printLastResponseHeaders() 0 10 2
B printTheCorrespondingCurlCommand() 0 21 5
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use Behatch\HttpCall\Request;
7
use Behat\Gherkin\Node\TableNode;
8
use Behat\Gherkin\Node\PyStringNode;
9
10
class RestContext extends BaseContext
11
{
12
    /**
13
     * @var Request
14
     */
15
    protected $request;
16
17
    public function __construct(Request $request)
18
    {
19
        $this->request = $request;
20
    }
21
22
    /**
23
     * Sends a HTTP request
24
     *
25
     * @Given I send a :method request to :url
26
     */
27
    public function iSendARequestTo($method, $url, PyStringNode $body = null, $files = [])
28
    {
29
        return $this->request->send(
0 ignored issues
show
Documentation Bug introduced by
The method send does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
30
            $method,
31
            $this->locatePath($url),
32
            [],
33
            $files,
34
            $body !== null ? $body->getRaw() : null
35
        );
36
    }
37
38
    /**
39
     * Sends a HTTP request with a some parameters
40
     *
41
     * @Given I send a :method request to :url with parameters:
42
     */
43
    public function iSendARequestToWithParameters($method, $url, TableNode $data)
44
    {
45
        $files = [];
46
        $parameters = [];
47
48
        foreach ($data->getHash() as $row) {
49
            if (!isset($row['key']) || !isset($row['value'])) {
50
                throw new \Exception("You must provide a 'key' and 'value' column in your table node.");
51
            }
52
53
            if (is_string($row['value']) && substr($row['value'], 0, 1) == '@') {
54
                $files[$row['key']] = rtrim($this->getMinkParameter('files_path'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.substr($row['value'],1);
55
            }
56
            else {
57
                $parameters[] = sprintf('%s=%s', $row['key'], $row['value']);
58
            }
59
        }
60
61
        parse_str(implode('&', $parameters), $parameters);
62
63
        return $this->request->send(
0 ignored issues
show
Documentation Bug introduced by
The method send does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64
            $method,
65
            $this->locatePath($url),
66
            $parameters,
67
            $files
68
        );
69
    }
70
71
    /**
72
     * Sends a HTTP request with a body
73
     *
74
     * @Given I send a :method request to :url with body:
75
     */
76
    public function iSendARequestToWithBody($method, $url, PyStringNode $body)
77
    {
78
        return $this->iSendARequestTo($method, $url, $body);
79
    }
80
81
    /**
82
     * Checks, whether the response content is equal to given text
83
     *
84
     * @Then the response should be equal to
85
     */
86
    public function theResponseShouldBeEqualTo(PyStringNode $expected)
87
    {
88
        $expected = str_replace('\\"', '"', $expected);
89
        $actual   = $this->request->getContent();
0 ignored issues
show
Documentation Bug introduced by
The method getContent does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
        $message = "Actual response is '$actual', but expected '$expected'";
91
        $this->assertEquals($expected, $actual, $message);
92
    }
93
94
    /**
95
     * Checks, whether the response content is null or empty string
96
     *
97
     * @Then the response should be empty
98
     */
99
    public function theResponseShouldBeEmpty()
100
    {
101
        $actual = $this->request->getContent();
0 ignored issues
show
Documentation Bug introduced by
The method getContent does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
102
        $message = "The response of the current page is not empty, it is: $actual";
103
        $this->assertTrue(null === $actual || "" === $actual, $message);
104
    }
105
106
    /**
107
     * Checks, whether the header name is equal to given text
108
     *
109
     * @Then the header :name should be equal to :value
110
     */
111
    public function theHeaderShouldBeEqualTo($name, $value)
112
    {
113
        $actual = $this->request->getHttpHeader($name);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
114
        $this->assertEquals(strtolower($value), strtolower($actual),
115
            "The header '$name' doesn't contain '$value', it is: '$actual'"
116
        );
117
    }
118
119
    /**
120
    * Checks, whether the header name is not equal to given text
121
    *
122
    * @Then the header :name should not be equal to :value
123
    */
124
    public function theHeaderShouldNotBeEqualTo($name, $value) {
125
        $actual = $this->getSession()->getResponseHeader($name);
126
        if (strtolower($value) == strtolower($actual)) {
127
            throw new ExpectationException(
128
                "The header '$name' is equal to '$actual'",
129
                $this->getSession()->getDriver()
130
            );
131
        }
132
    }
133
134
    /**
135
     * Checks, whether the header name contains the given text
136
     *
137
     * @Then the header :name should contain :value
138
     */
139
    public function theHeaderShouldContain($name, $value)
140
    {
141
        $actual = $this->request->getHttpHeader($name);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
142
        $this->assertContains($value, $actual,
143
            "The header '$name' value is '$actual'"
144
        );
145
    }
146
147
    /**
148
     * Checks, whether the header name doesn't contain the given text
149
     *
150
     * @Then the header :name should not contain :value
151
     */
152
    public function theHeaderShouldNotContain($name, $value)
153
    {
154
        $this->assertNotContains($value, $this->request->getHttpHeader($name),
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
155
            "The header '$name' contains '$value'"
156
        );
157
    }
158
159
    /**
160
     * Checks, whether the header not exist
161
     *
162
     * @Then the header :name should not exist
163
     */
164
    public function theHeaderShouldNotExist($name)
165
    {
166
        $this->not(function () use($name) {
167
            $this->theHeaderShouldExist($name);
168
        }, "The header '$name' exists");
169
    }
170
171
    protected function theHeaderShouldExist($name)
172
    {
173
        return $this->request->getHttpHeader($name);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
174
    }
175
176
   /**
177
     * Checks, that the response header expire is in the future
178
     *
179
     * @Then the response should expire in the future
180
     */
181
    public function theResponseShouldExpireInTheFuture()
182
    {
183
        $date = new \DateTime($this->request->getHttpRawHeader('Date')[0]);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpRawHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
184
        $expires = new \DateTime($this->request->getHttpRawHeader('Expires')[0]);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpRawHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
185
186
        $this->assertSame(1, $expires->diff($date)->invert,
187
            sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM))
188
        );
189
    }
190
191
    /**
192
     * Add an header element in a request
193
     *
194
     * @Then I add :name header equal to :value
195
     */
196
    public function iAddHeaderEqualTo($name, $value)
197
    {
198
        $this->request->setHttpHeader($name, $value);
0 ignored issues
show
Documentation Bug introduced by
The method setHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
199
    }
200
201
    /**
202
     * @Then the response should be encoded in :encoding
203
     */
204
    public function theResponseShouldBeEncodedIn($encoding)
205
    {
206
        $content = $this->request->getContent();
0 ignored issues
show
Documentation Bug introduced by
The method getContent does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
207
        if (!mb_check_encoding($content, $encoding)) {
208
            throw new \Exception("The response is not encoded in $encoding");
209
        }
210
211
        $this->theHeaderShouldContain('Content-Type', "charset=$encoding");
212
    }
213
214
    /**
215
     * @Then print last response headers
216
     */
217
    public function printLastResponseHeaders()
218
    {
219
        $text = '';
220
        $headers = $this->request->getHttpHeaders();
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeaders does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
221
222
        foreach ($headers as $name => $value) {
223
            $text .= $name . ': '. $this->request->getHttpHeader($name) . "\n";
0 ignored issues
show
Documentation Bug introduced by
The method getHttpHeader does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
224
        }
225
        echo $text;
226
    }
227
228
229
    /**
230
     * @Then print the corresponding curl command
231
     */
232
    public function printTheCorrespondingCurlCommand()
233
    {
234
        $method = $this->request->getMethod();
0 ignored issues
show
Documentation Bug introduced by
The method getMethod does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
235
        $url = $this->request->getUri();
0 ignored issues
show
Documentation Bug introduced by
The method getUri does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
236
237
        $headers = '';
238
        foreach ($this->request->getServer() as $name => $value) {
0 ignored issues
show
Documentation Bug introduced by
The method getServer does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
239
            if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') {
240
                $headers .= " -H '$name: $value'";
241
            }
242
        }
243
244
        $data = '';
245
        $params = $this->request->getParameters();
0 ignored issues
show
Documentation Bug introduced by
The method getParameters does not exist on object<Behatch\HttpCall\Request>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
246
        if (!empty($params)) {
247
            $query = http_build_query($params);
248
            $data = " --data '$query'" ;
249
        }
250
251
        echo "curl -X $method$data$headers '$url'";
252
    }
253
}
254