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

RestContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behatch\HttpCall\Request;
6
use Behat\Gherkin\Node\TableNode;
7
use Behat\Gherkin\Node\PyStringNode;
8
9
class RestContext extends BaseContext
10
{
11
    protected $request;
12
13
    public function __construct(Request $request)
14
    {
15
        $this->request = $request;
16
    }
17
18
    /**
19
     * Sends a HTTP request
20
     *
21
     * @Given I send a :method request to :url
22
     */
23
    public function iSendARequestTo($method, $url, PyStringNode $body = null, $files = [])
24
    {
25
        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...
26
            $method,
27
            $this->locatePath($url),
28
            [],
29
            $files,
30
            $body !== null ? $body->getRaw() : null
31
        );
32
    }
33
34
    /**
35
     * Sends a HTTP request with a some parameters
36
     *
37
     * @Given I send a :method request to :url with parameters:
38
     */
39
    public function iSendARequestToWithParameters($method, $url, TableNode $datas)
40
    {
41
        $files = [];
42
        $parameters = [];
43
44
        foreach ($datas->getHash() as $row) {
45
            if (!isset($row['key']) || !isset($row['value'])) {
46
                throw new \Exception("You must provide a 'key' and 'value' column in your table node.");
47
            }
48
49
            if (is_string($row['value']) && substr($row['value'], 0, 1) == '@') {
50
                $files[$row['key']] = rtrim($this->getMinkParameter('files_path'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.substr($row['value'],1);
51
            }
52
            else {
53
                $parameters[] = sprintf('%s=%s', $row['key'], $row['value']);
54
            }
55
        }
56
57
        parse_str(implode('&', $parameters), $parameters);
58
59
        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...
60
            $method,
61
            $this->locatePath($url),
62
            $parameters,
63
            $files
64
        );
65
    }
66
67
    /**
68
     * Sends a HTTP request with a body
69
     *
70
     * @Given I send a :method request to :url with body:
71
     */
72
    public function iSendARequestToWithBody($method, $url, PyStringNode $body)
73
    {
74
        return $this->iSendARequestTo($method, $url, $body);
75
    }
76
77
    /**
78
     * Checks, whether the response content is equal to given text
79
     *
80
     * @Then the response should be equal to
81
     */
82
    public function theResponseShouldBeEqualTo(PyStringNode $expected)
83
    {
84
        $expected = str_replace('\\"', '"', $expected);
85
        $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...
86
        $message = "Response is: $actual";
87
        $this->assertEquals($expected, $actual, $message);
88
    }
89
90
    /**
91
     * Checks, whether the response content is null or empty string
92
     *
93
     * @Then the response should be empty
94
     */
95
    public function theResponseShouldBeEmpty()
96
    {
97
        $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...
98
        $message = "The response of the current page is not empty, it is: $actual";
99
        $this->assertTrue(null === $actual || "" === $actual, $message);
100
    }
101
102
    /**
103
     * Checks, whether the header name is equal to given text
104
     *
105
     * @Then the header :name should be equal to :value
106
     */
107
    public function theHeaderShouldBeEqualTo($name, $value)
108
    {
109
        $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...
110
        $this->assertEquals(strtolower($value), strtolower($actual),
111
            "The header '$name' is equal to '$actual'"
112
        );
113
    }
114
115
    /**
116
    * Checks, whether the header name is not equal to given text
117
    *
118
    * @Then the header :name should not be equal to :value
119
    */
120
    public function theHeaderShouldNotBeEqualTo($name, $value) {
121
        $actual = $this->getSession()->getResponseHeader($name);
122
        if (strtolower($value) == strtolower($actual)) {
123
            throw new ExpectationException(
124
                "The header '$name' is equal to '$actual'",
125
                $this->getSession()->getDriver()
126
            );
127
        }
128
    }
129
130
    /**
131
     * Checks, whether the header name contains the given text
132
     *
133
     * @Then the header :name should contain :value
134
     */
135
    public function theHeaderShouldContain($name, $value)
136
    {
137
        $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...
138
        $this->assertContains($value, $actual,
139
            "The header '$name' value is '$actual'"
140
        );
141
    }
142
143
    /**
144
     * Checks, whether the header name doesn't contain the given text
145
     *
146
     * @Then the header :name should not contain :value
147
     */
148
    public function theHeaderShouldNotContain($name, $value)
149
    {
150
        $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...
151
            "The header '$name' contains '$value'"
152
        );
153
    }
154
155
    /**
156
     * Checks, whether the header not exist
157
     *
158
     * @Then the header :name should not exist
159
     */
160
    public function theHeaderShouldNotExist($name)
161
    {
162
        $this->not(function () use($name) {
163
            $this->theHeaderShouldExist($name);
164
        }, "The header '$name' exists");
165
    }
166
167
    protected function theHeaderShouldExist($name)
168
    {
169
        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...
170
    }
171
172
   /**
173
     * Checks, that the response header expire is in the future
174
     *
175
     * @Then the response should expire in the future
176
     */
177
    public function theResponseShouldExpireInTheFuture()
178
    {
179
        $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...
180
        $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...
181
182
        $this->assertSame(1, $expires->diff($date)->invert,
183
            sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM))
184
        );
185
    }
186
187
    /**
188
     * Add an header element in a request
189
     *
190
     * @Then I add :name header equal to :value
191
     */
192
    public function iAddHeaderEqualTo($name, $value)
193
    {
194
        $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...
195
    }
196
197
    /**
198
     * @Then the response should be encoded in :encoding
199
     */
200
    public function theResponseShouldBeEncodedIn($encoding)
201
    {
202
        $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...
203
        if (!mb_check_encoding($content, $encoding)) {
204
            throw new \Exception("The response is not encoded in $encoding");
205
        }
206
207
        $this->theHeaderShouldContain('Content-Type', "charset=$encoding");
208
    }
209
210
    /**
211
     * @Then print last response headers
212
     */
213
    public function printLastResponseHeaders()
214
    {
215
        $text = '';
216
        $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...
217
218
        foreach ($headers as $name => $value) {
219
            $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...
220
        }
221
        echo $text;
222
    }
223
224
225
    /**
226
     * @Then print the corresponding curl command
227
     */
228
    public function printTheCorrespondingCurlCommand()
229
    {
230
        $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...
231
        $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...
232
233
        $headers = '';
234
        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...
235
            if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') {
236
                $headers .= " -H '$name: $value'";
237
            }
238
        }
239
240
        $data = '';
241
        $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...
242
        if (!empty($params)) {
243
            $query = http_build_query($params);
244
            $data = " --data '$query'" ;
245
        }
246
247
        echo "curl -X $method$data$headers '$url'";
248
    }
249
}
250