Passed
Push — 3.x ( 709340...a6d46d )
by San
04:41
created

RestContext::theHeaderShouldNotBeEqualTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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 = "The string '$expected' is not equal to the response of the current page";
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';
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' is equal to '$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 theHeaderShouldBeContains($name, $value)
140
    {
141
        $this->assertContains($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...
142
            "The header '$name' doesn't contain '$value'"
143
        );
144
    }
145
146
    /**
147
     * Checks, whether the header name doesn't contain the given text
148
     *
149
     * @Then the header :name should not contain :value
150
     */
151
    public function theHeaderShouldNotContain($name, $value)
152
    {
153
        $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...
154
            "The header '$name' contains '$value'"
155
        );
156
    }
157
158
    /**
159
     * Checks, whether the header not exist
160
     *
161
     * @Then the header :name should not exist
162
     */
163
    public function theHeaderShouldNotExist($name)
164
    {
165
        $this->not(function () use($name) {
166
            $this->theHeaderShouldExist($name);
167
        }, "The header '$name' exists");
168
    }
169
170
    protected function theHeaderShouldExist($name)
171
    {
172
        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...
173
    }
174
175
   /**
176
     * Checks, that the response header expire is in the future
177
     *
178
     * @Then the response should expire in the future
179
     */
180
    public function theResponseShouldExpireInTheFuture()
181
    {
182
        $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...
183
        $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...
184
185
        $this->assertSame(1, $expires->diff($date)->invert,
186
            sprintf('The response doesn\'t expire in the future (%s)', $expires->format(DATE_ATOM))
187
        );
188
    }
189
190
    /**
191
     * Add an header element in a request
192
     *
193
     * @Then I add :name header equal to :value
194
     */
195
    public function iAddHeaderEqualTo($name, $value)
196
    {
197
        $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...
198
    }
199
200
    /**
201
     * @Then the response should be encoded in :encoding
202
     */
203
    public function theResponseShouldBeEncodedIn($encoding)
204
    {
205
        $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...
206
        if (!mb_check_encoding($content, $encoding)) {
207
            throw new \Exception("The response is not encoded in $encoding");
208
        }
209
210
        $this->theHeaderShouldBeContains('Content-Type', "charset=$encoding");
211
    }
212
213
    /**
214
     * @Then print last response headers
215
     */
216
    public function printLastResponseHeaders()
217
    {
218
        $text = '';
219
        $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...
220
221
        foreach ($headers as $name => $value) {
222
            $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...
223
        }
224
        echo $text;
225
    }
226
227
228
    /**
229
     * @Then print the corresponding curl command
230
     */
231
    public function printTheCorrespondingCurlCommand()
232
    {
233
        $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...
234
        $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...
235
236
        $headers = '';
237
        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...
238
            if (substr($name, 0, 5) !== 'HTTP_' && $name !== 'HTTPS') {
239
                $headers .= " -H '$name: $value'";
240
            }
241
        }
242
243
        $data = '';
244
        $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...
245
        if (!empty($params)) {
246
            $query = http_build_query($params);
247
            $data = " --data '$query'" ;
248
        }
249
250
        echo "curl -X $method$data$headers '$url'";
251
    }
252
}
253