|
4 | 4 |
|
5 | 5 | namespace Codeception\Module\Symfony;
|
6 | 6 |
|
| 7 | +use PHPUnit\Framework\Constraint\Constraint; |
| 8 | +use PHPUnit\Framework\Constraint\LogicalAnd; |
| 9 | +use PHPUnit\Framework\Constraint\LogicalNot; |
| 10 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame; |
| 11 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie; |
| 12 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasHeader; |
| 13 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderLocationSame; |
| 14 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderSame; |
| 15 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsRedirected; |
7 | 16 | use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
|
| 17 | +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame; |
8 | 18 | use function sprintf;
|
9 | 19 |
|
10 | 20 | trait BrowserAssertionsTrait
|
11 | 21 | {
|
| 22 | + /** |
| 23 | + * Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value. |
| 24 | + */ |
| 25 | + public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void |
| 26 | + { |
| 27 | + $this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Asserts that the response was successful (HTTP status is 2xx). |
| 32 | + */ |
| 33 | + public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void |
| 34 | + { |
| 35 | + $this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Asserts a specific HTTP status code. |
| 40 | + */ |
| 41 | + public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void |
| 42 | + { |
| 43 | + $this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode, $verbose), $message); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Asserts the response is a redirect response (optionally, you can check the target location and status code). |
| 48 | + * The excepted location can be either an absolute or a relative path. |
| 49 | + */ |
| 50 | + public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void |
| 51 | + { |
| 52 | + $constraint = new ResponseIsRedirected($verbose); |
| 53 | + if ($expectedLocation) { |
| 54 | + if (class_exists(ResponseHeaderLocationSame::class)) { |
| 55 | + $locationConstraint = new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation); |
| 56 | + } else { |
| 57 | + $locationConstraint = new ResponseHeaderSame('Location', $expectedLocation); |
| 58 | + } |
| 59 | + |
| 60 | + $constraint = LogicalAnd::fromConstraints($constraint, $locationConstraint); |
| 61 | + } |
| 62 | + if ($expectedCode) { |
| 63 | + $constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode)); |
| 64 | + } |
| 65 | + |
| 66 | + $this->assertThatForResponse($constraint, $message); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');. |
| 71 | + */ |
| 72 | + public function assertResponseHasHeader(string $headerName, string $message = ''): void |
| 73 | + { |
| 74 | + $this->assertThatForResponse(new ResponseHasHeader($headerName), $message); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');. |
| 79 | + */ |
| 80 | + public function assertResponseNotHasHeader(string $headerName, string $message = ''): void |
| 81 | + { |
| 82 | + $this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Asserts the given header does contain the expected value on the response, |
| 87 | + * e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');. |
| 88 | + */ |
| 89 | + public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void |
| 90 | + { |
| 91 | + $this->assertThatForResponse(new ResponseHeaderSame($headerName, $expectedValue), $message); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Asserts the given header does not contain the expected value on the response, |
| 96 | + * e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');. |
| 97 | + */ |
| 98 | + public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void |
| 99 | + { |
| 100 | + $this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain). |
| 105 | + */ |
| 106 | + public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void |
| 107 | + { |
| 108 | + $this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain). |
| 113 | + */ |
| 114 | + public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void |
| 115 | + { |
| 116 | + $this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message); |
| 117 | + } |
| 118 | + |
12 | 119 | /**
|
13 | 120 | * Reboot client's kernel.
|
14 | 121 | * Can be used to manually reboot kernel when 'rebootable_client' => false
|
@@ -50,7 +157,7 @@ public function seePageIsAvailable(?string $url = null): void
|
50 | 157 | $this->seeInCurrentUrl($url);
|
51 | 158 | }
|
52 | 159 |
|
53 |
| - $this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful()); |
| 160 | + $this->assertResponseIsSuccessful(); |
54 | 161 | }
|
55 | 162 |
|
56 | 163 | /**
|
@@ -104,4 +211,9 @@ public function submitSymfonyForm(string $name, array $fields): void
|
104 | 211 |
|
105 | 212 | $this->submitForm($selector, $params, $button);
|
106 | 213 | }
|
| 214 | + |
| 215 | + protected function assertThatForResponse(Constraint $constraint, string $message = ''): void |
| 216 | + { |
| 217 | + $this->assertThat($this->getClient()->getResponse(), $constraint, $message); |
| 218 | + } |
107 | 219 | }
|
0 commit comments