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