forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowserAssertionsTrait.php
300 lines (267 loc) · 11.4 KB
/
BrowserAssertionsTrait.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
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\LogicalAnd;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame;
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie;
use Symfony\Component\HttpFoundation\Test\Constraint\RequestAttributeValueSame;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasHeader;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderLocationSame;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderSame;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsRedirected;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
use function sprintf;
trait BrowserAssertionsTrait
{
/**
* Asserts the given cookie in the test Client is set to the expected value.
*/
public function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForClient(LogicalAnd::fromConstraints(
new BrowserHasCookie($name, $path, $domain),
new BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain)
), $message);
}
/**
* Asserts that the test Client does have the given cookie set (meaning, the cookie was set by any response in the test).
*/
public function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForClient(new BrowserHasCookie($name, $path, $domain), $message);
}
/**
* Asserts that the test Client does not have the given cookie set (meaning, the cookie was set by any response in the test).
*/
public function assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForClient(new LogicalNot(new BrowserHasCookie($name, $path, $domain)), $message);
}
/**
* Asserts the given request attribute is set to the expected value.
*/
public function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
{
$this->assertThat($this->getClient()->getRequest(), new RequestAttributeValueSame($name, $expectedValue), $message);
}
/**
* Asserts the given cookie is present and set to the expected value.
*/
public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForResponse(LogicalAnd::fromConstraints(
new ResponseHasCookie($name, $path, $domain),
new ResponseCookieValueSame($name, $expectedValue, $path, $domain)
), $message);
}
/**
* Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value.
*/
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
{
$this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message);
}
/**
* Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain).
*/
public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message);
}
/**
* Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');.
*/
public function assertResponseHasHeader(string $headerName, string $message = ''): void
{
$this->assertThatForResponse(new ResponseHasHeader($headerName), $message);
}
/**
* Asserts the given header does not contain the expected value on the response,
* e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');.
*/
public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
{
$this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message);
}
/**
* Asserts the given header does contain the expected value on the response,
* e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');.
*/
public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
{
$this->assertThatForResponse(new ResponseHeaderSame($headerName, $expectedValue), $message);
}
/**
* Asserts that the response was successful (HTTP status is 2xx).
*/
public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
{
$this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message);
}
/**
* Asserts the response is unprocessable (HTTP status is 422)
*/
public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void
{
$this->assertThatForResponse(new ResponseIsUnprocessable($verbose), $message);
}
/**
* Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain).
*/
public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
{
$this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message);
}
/**
* Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');.
*/
public function assertResponseNotHasHeader(string $headerName, string $message = ''): void
{
$this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message);
}
/**
* Asserts the response is a redirect response (optionally, you can check the target location and status code).
* The excepted location can be either an absolute or a relative path.
*/
public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
{
$constraint = new ResponseIsRedirected($verbose);
if ($expectedLocation) {
if (class_exists(ResponseHeaderLocationSame::class)) {
$locationConstraint = new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation);
} else {
$locationConstraint = new ResponseHeaderSame('Location', $expectedLocation);
}
$constraint = LogicalAnd::fromConstraints($constraint, $locationConstraint);
}
if ($expectedCode) {
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode));
}
$this->assertThatForResponse($constraint, $message);
}
/**
* Asserts a specific HTTP status code.
*/
public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
{
$this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode, $verbose), $message);
}
/**
* Asserts the request matches the given route and optionally route parameters.
*/
public function assertRouteSame(string $expectedRoute, array $parameters = [], string $message = ''): void
{
$constraint = new RequestAttributeValueSame('_route', $expectedRoute);
$constraints = [];
foreach ($parameters as $key => $value) {
$constraints[] = new RequestAttributeValueSame($key, $value);
}
if ($constraints) {
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
}
$this->assertThat($this->getClient()->getRequest(), $constraint, $message);
}
/**
* Reboot client's kernel.
* Can be used to manually reboot kernel when 'rebootable_client' => false
*
* ```php
* <?php
*
* // Perform some requests
*
* $I->rebootClientKernel();
*
* // Perform other requests
*
* ```
*/
public function rebootClientKernel(): void
{
$this->getClient()->rebootKernel();
}
/**
* Verifies that a page is available.
* By default, it checks the current page, specify the `$url` parameter to change it.
*
* ```php
* <?php
* $I->amOnPage('/dashboard');
* $I->seePageIsAvailable();
*
* $I->seePageIsAvailable('/dashboard'); // Same as above
* ```
*
* @param string|null $url
*/
public function seePageIsAvailable(?string $url = null): void
{
if ($url !== null) {
$this->amOnPage($url);
$this->seeInCurrentUrl($url);
}
$this->assertResponseIsSuccessful();
}
/**
* Goes to a page and check that it redirects to another.
*
* ```php
* <?php
* $I->seePageRedirectsTo('/admin', '/login');
* ```
*/
public function seePageRedirectsTo(string $page, string $redirectsTo): void
{
$this->getClient()->followRedirects(false);
$this->amOnPage($page);
$response = $this->getClient()->getResponse();
$this->assertTrue(
$response->isRedirection()
);
$this->getClient()->followRedirect();
$this->seeInCurrentUrl($redirectsTo);
}
/**
* Submit a form specifying the form name only once.
*
* Use this function instead of [`$I->submitForm()`](#submitForm) to avoid repeating the form name in the field selectors.
* If you customized the names of the field selectors use `$I->submitForm()` for full control.
*
* ```php
* <?php
* $I->submitSymfonyForm('login_form', [
* '[email]' => '[email protected]',
* '[password]' => 'secretForest'
* ]);
* ```
*
* @param string $name The `name` attribute of the `<form>` (you cannot use an array as selector here)
* @param string[] $fields
*/
public function submitSymfonyForm(string $name, array $fields): void
{
$selector = sprintf('form[name=%s]', $name);
$params = [];
foreach ($fields as $key => $value) {
$fixedKey = sprintf('%s%s', $name, $key);
$params[$fixedKey] = $value;
}
$button = sprintf('%s_submit', $name);
$this->submitForm($selector, $params, $button);
}
protected function assertThatForClient(Constraint $constraint, string $message = ''): void
{
$this->assertThat($this->getClient(), $constraint, $message);
}
protected function assertThatForResponse(Constraint $constraint, string $message = ''): void
{
$this->assertThat($this->getClient()->getResponse(), $constraint, $message);
}
}