-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
CookieTrait.php
309 lines (270 loc) · 9.93 KB
/
CookieTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
/**
* Trait CookieTrait.
*
* Cookie-related steps.
*
* @package DrevOps\BehatSteps
*/
trait CookieTrait {
/**
* Check if a cookie exists.
*
* @Then a cookie with( the) name :name should exist
*/
public function cookieWithNameShouldExist(string $name): void {
static::cookieExists($name);
}
/**
* Check if a cookie exists with a specific value.
*
* @Then a cookie with( the) name :name and value :value should exist
*/
public function cookieWithNameValueShouldExist(string $name, string $value): void {
static::cookieExists($name, $value);
}
/**
* Check if a cookie exists with a value containing a partial value.
*
* @Then a cookie with( the) name :name and value containing :partial_value should exist
*/
public function cookieWithNamePartialValueShouldExist(string $name, string $partial_value): void {
static::cookieExists($name, $partial_value, FALSE, TRUE);
}
/**
* Check if a cookie with a partial name exists.
*
* @Then a cookie with( the) name containing :partial_name should exist
*/
public function cookieWithPartialNameShouldExist(string $partial_name): void {
static::cookieExists($partial_name, NULL, TRUE);
}
/**
* Check if a cookie with a partial name and value exists.
*
* @Then a cookie with( the) name containing :partial_name and value :value should exist
*/
public function cookieWithPartialNameValueShouldExist(string $partial_name, string $value): void {
static::cookieExists($partial_name, $value, TRUE);
}
/**
* Check if a cookie with a partial name and partial value exists.
*
* @Then a cookie with( the) name containing :partial_name and value containing :partial_value should exist
*/
public function cookieWithPartialNamePartialValueShouldExist(string $partial_name, string $partial_value): void {
static::cookieExists($partial_name, $partial_value, TRUE, TRUE);
}
/**
* Check if a cookie does not exist.
*
* @Then a cookie with( the) name :name should not exist
*/
public function cookieWithNameShouldNotExist(string $name): void {
static::cookieNotExists($name);
}
/**
* Check if a cookie with a specific value does not exist.
*
* @Then a cookie with( the) name :name and value :value should not exist
*/
public function cookieWithNameValueShouldNotExist(string $name, string $value): void {
static::cookieNotExists($name, $value);
}
/**
* Check if a cookie with a value containing a partial value does not exist.
*
* @Then a cookie with( the) name :name and value containing :partial_value should not exist
*/
public function cookieWithNamePartialValueShouldNotExist(string $name, string $partial_value): void {
static::cookieNotExists($name, $partial_value, FALSE, TRUE);
}
/**
* Check if a cookie with a partial name does not exist.
*
* @Then a cookie with( the) name containing :partial_name should not exist
*/
public function cookieWithPartialNameShouldNotExist(string $partial_name): void {
static::cookieNotExists($partial_name, NULL, TRUE);
}
/**
* Check if a cookie with a partial name and value does not exist.
*
* @Then a cookie with( the) name containing :partial_name and value :value should not exist
*/
public function cookieWithPartialNameValueShouldNotExist(string $partial_name, string $value): void {
static::cookieNotExists($partial_name, $value, TRUE);
}
/**
* Check if a cookie with a partial name and partial value does not exist.
*
* @Then a cookie with( the) name containing :partial_name and value containing :partial_value should not exist
*/
public function cookieWithPartialNamePartialValueShouldNotExist(string $partial_name, string $partial_value): void {
static::cookieNotExists($partial_name, $partial_value, TRUE, TRUE);
}
/**
* Check if a cookie exists.
*/
protected function cookieExists(string $name, ?string $value = NULL, bool $is_partial_name = FALSE, bool $is_partial_value = FALSE): void {
$cookie = $this->cookieGetByName($name, $is_partial_name);
if ($cookie === NULL) {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was not set', $name));
}
throw new \Exception(sprintf('The cookie with name "%s" was not set', $name));
}
if ($value !== NULL) {
if ($is_partial_value) {
if (!str_contains((string) $cookie['value'], $value)) {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was set with value "%s", but it should contain "%s"', $name, $cookie['value'], $value));
}
throw new \Exception(sprintf('The cookie with name "%s" was set with value "%s", but it should contain "%s"', $name, $cookie['value'], $value));
}
}
elseif ($cookie['value'] !== $value) {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was set with value "%s", but it should be "%s"', $name, $cookie['value'], $value));
}
throw new \Exception(sprintf('The cookie with name "%s" was set with value "%s", but it should be "%s"', $name, $cookie['value'], $value));
}
}
}
/**
* Check if a cookie does not exist.
*/
protected function cookieNotExists(string $name, ?string $value = NULL, bool $is_partial_name = FALSE, bool $is_partial_value = FALSE): void {
$cookie = $this->cookieGetByName($name, $is_partial_name);
if ($cookie === NULL) {
return;
}
if ($value !== NULL) {
if ($is_partial_value) {
if (str_contains((string) $cookie['value'], $value)) {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was set with value containing "%s", but it should not contain "%s"', $name, $cookie['value'], $value));
}
throw new \Exception(sprintf('The cookie with name "%s" was set with value containing "%s", but it should not contain "%s"', $name, $cookie['value'], $value));
}
}
elseif ($cookie['value'] === $value) {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was set with value "%s", but it should not be "%s"', $name, $cookie['value'], $value));
}
throw new \Exception(sprintf('The cookie with name "%s" was set with value "%s", but it should not be "%s"', $name, $cookie['value'], $value));
}
}
else {
if ($is_partial_name) {
throw new \Exception(sprintf('The cookie with name containing "%s" was set but it should not be', $name));
}
throw new \Exception(sprintf('The cookie with name "%s" was set but it should not be', $name));
}
}
/**
* Get a cookie by exact or partial name.
*
* @param string $name
* The name of the cookie.
* @param bool $is_partial
* Whether to search for a partial name.
*
* @return array<string, mixed>|null
* The cookie or NULL if not found.
*/
protected function cookieGetByName(string $name, bool $is_partial = FALSE): ?array {
$cookies = self::cookieGetAll();
foreach ($cookies as $cookie) {
if ($is_partial) {
if (str_contains((string) $cookie['name'], $name)) {
return $cookie;
}
}
elseif ($cookie['name'] === $name) {
return $cookie;
}
}
return NULL;
}
/**
* Get all cookies.
*
* @return array<int, array<string, mixed>>
* An array of cookies.
*/
protected function cookieGetAll(): array {
$driver = $this->getSession()->getDriver();
// WebDriver-based drivers like Selenium2Driver.
if (method_exists($driver, 'getWebDriverSession')) {
$cookies = $driver->getWebDriverSession()->getAllCookies();
array_walk($cookies, function (array &$cookie): void {
$cookie['value'] = rawurldecode((string) $cookie['value']);
});
return $cookies;
}
// BrowserKit-based drivers like GoutteDriver.
if (method_exists($driver, 'getClient')) {
/** @var \Symfony\Component\BrowserKit\CookieJar $cookie_jar */
$cookie_jar = $driver->getClient()->getCookieJar();
// Use filtered cookies from the Driver's cookie jar and also add more
// properties.
/** @var \Symfony\Component\BrowserKit\Cookie[] $cookies_objs */
$cookies_objs = $cookie_jar->all();
$cookies_names = array_keys($cookie_jar->allValues($driver->getCurrentUrl()));
$cookies = [];
foreach ($cookies_objs as $cookie_obj) {
if (!in_array($cookie_obj->getName(), $cookies_names)) {
continue;
}
$cookies[] = [
'name' => $cookie_obj->getName(),
'value' => $cookie_obj->getValue(),
'secure' => $cookie_obj->isSecure(),
];
}
return $cookies;
}
// Fallback to parsing headers.
$cookies = [];
$headers = $driver->getResponseHeaders();
foreach ($headers as $header_name => $header_value) {
if (strtolower((string) $header_name) !== 'set-cookie') {
continue;
}
// Only support parsed cookies from a string header.
if (is_string($header_value)) {
$cookies = self::cookieParseHeader($header_value);
}
break;
}
return $cookies;
}
/**
* Parse a cookie header string.
*
* @param string $string
* The cookie header string.
*
* @return array<int, array<string, mixed>>
* An array of cookies.
*/
protected static function cookieParseHeader(string $string): array {
$cookies = [];
$parts = explode(';', $string);
foreach ($parts as $part) {
$part = trim($part);
if (str_contains($part, '=')) {
$cookie = [];
[$name, $value] = explode('=', $part, 2);
$cookie['name'] = $name;
$cookie['value'] = rawurldecode($value);
$cookie['secure'] = FALSE;
$cookies[] = $cookie;
}
}
return $cookies;
}
}