1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Behapi\Http; |
3
|
|
|
|
4
|
|
|
use RuntimeException; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
|
8
|
|
|
// use Assert\Assert; // to be used when https://github.com/beberlei/assert/pull/264 is merged and released |
9
|
|
|
|
10
|
|
|
use Behapi\Assert\Assert; |
11
|
|
|
use Behapi\HttpHistory\History as HttpHistory; |
12
|
|
|
|
13
|
|
|
final class ResponseContext implements Context |
14
|
|
|
{ |
15
|
|
|
/** @var HttpHistory */ |
16
|
|
|
private $history; |
17
|
|
|
|
18
|
|
|
public function __construct(HttpHistory $history) |
19
|
|
|
{ |
20
|
|
|
$this->history = $history; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @Then the status code should be :expected */ |
24
|
|
|
public function status_code_should_be(int $expected): void |
25
|
|
|
{ |
26
|
|
|
$response = $this->history->getLastResponse(); |
27
|
|
|
|
28
|
|
|
Assert::that($response->getStatusCode()) |
29
|
|
|
->same($expected) |
30
|
|
|
; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @Then the status code should not be :expected */ |
34
|
|
|
public function status_code_should_not_be(int $expected): void |
35
|
|
|
{ |
36
|
|
|
$response = $this->history->getLastResponse(); |
37
|
|
|
|
38
|
|
|
Assert::that($response->getStatusCode()) |
39
|
|
|
->same($expected) |
40
|
|
|
; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @Then the content-type should be equal to :expected */ |
44
|
|
|
public function content_type_should_be(string $expected): void |
45
|
|
|
{ |
46
|
|
|
$response = $this->history->getLastResponse(); |
47
|
|
|
|
48
|
|
|
Assert::that($response->getHeaderLine('Content-type')) |
49
|
|
|
->same($expected) |
50
|
|
|
; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @Then the response header :header should be equal to :expected */ |
54
|
|
|
public function header_should_be(string $header, string $expected): void |
55
|
|
|
{ |
56
|
|
|
$response = $this->history->getLastResponse(); |
57
|
|
|
|
58
|
|
|
Assert::that($response->getHeaderLine($header)) |
59
|
|
|
->same($expected) |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @Then the response header :header should contain :expected */ |
64
|
|
|
public function header_should_contain(string $header, string $expected): void |
65
|
|
|
{ |
66
|
|
|
$response = $this->history->getLastResponse(); |
67
|
|
|
|
68
|
|
|
Assert::that($response->getHeaderLine($header)) |
69
|
|
|
->same($expected) |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @Then the response should have a header :header */ |
74
|
|
|
public function response_should_have_header(string $header): void |
75
|
|
|
{ |
76
|
|
|
$response = $this->history->getLastResponse(); |
77
|
|
|
|
78
|
|
|
Assert::that($response->hasHeader($header)) |
79
|
|
|
->true() |
80
|
|
|
; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @Then the response should have sent some data */ |
84
|
|
|
public function response_should_have_sent_some_data(): void |
85
|
|
|
{ |
86
|
|
|
$body = $this->history->getLastResponse()->getBody(); |
87
|
|
|
|
88
|
|
|
Assert::that($body->getSize()) |
89
|
|
|
->notNull() |
90
|
|
|
->greaterThan(0) |
91
|
|
|
; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @Then the response should not have sent any data */ |
95
|
|
|
public function response_should_not_have_any_data(): void |
96
|
|
|
{ |
97
|
|
|
$body = $this->history->getLastResponse()->getBody(); |
98
|
|
|
|
99
|
|
|
Assert::that($body->getSize()) |
100
|
|
|
->nullOr() |
101
|
|
|
->same(0) |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @Then the response should contain :data */ |
106
|
|
|
public function response_should_contain(string $data): void |
107
|
|
|
{ |
108
|
|
|
$response = $this->history->getLastResponse(); |
109
|
|
|
|
110
|
|
|
Assert::that((string) $response->getBody()) |
111
|
|
|
->contains($data) |
112
|
|
|
; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** @Then the response should not contain :data */ |
116
|
|
|
public function response_should_not_contain(string $data): void |
117
|
|
|
{ |
118
|
|
|
$response = $this->history->getLastResponse(); |
119
|
|
|
|
120
|
|
|
Assert::that((string) $response->getBody()) |
121
|
|
|
->notContains($data) |
122
|
|
|
; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @Then the response should be :data */ |
126
|
|
|
public function response_should_be(string $data): void |
127
|
|
|
{ |
128
|
|
|
$response = $this->history->getLastResponse(); |
129
|
|
|
|
130
|
|
|
Assert::that((string) $response->getBody()) |
131
|
|
|
->eq($data) |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @Then the response should not be :data */ |
136
|
|
|
public function response_should_not_be(string $data): void |
137
|
|
|
{ |
138
|
|
|
$response = $this->history->getLastResponse(); |
139
|
|
|
|
140
|
|
|
Assert::that((string) $response->getBody()) |
141
|
|
|
->notEq($data) |
142
|
|
|
; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|