diff --git a/core/tests/Drupal/Tests/Constraints/ResponseHeaderExists.php b/core/tests/Drupal/Tests/Constraints/ResponseHeaderExists.php new file mode 100644 index 0000000000..3d14f5644f --- /dev/null +++ b/core/tests/Drupal/Tests/Constraints/ResponseHeaderExists.php @@ -0,0 +1,53 @@ +header = $header; + } + + /** + * {@inheritdoc} + */ + protected function matches($other): bool { + if (is_array($other)) { + return array_key_exists($this->header, $other); + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function toString(): string { + return 'has a ' . $this->exporter()->export($this->header) . ' header'; + } + + /** + * {@inheritdoc} + */ + protected function failureDescription($other): string { + return 'the response ' . $this->toString(); + } +} diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index a3bf6b0704..3e67197681 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -10,6 +10,7 @@ use Behat\Mink\Session; use Drupal\Component\Utility\Html; use Drupal\Core\Url; +use Drupal\Tests\Constraints\ResponseHeaderExists; use PHPUnit\Framework\Assert; use PHPUnit\Framework\Constraint\ArrayHasKey; use PHPUnit\Framework\Constraint\LogicalNot; @@ -65,11 +66,8 @@ protected function cleanUrl($url) { * The name of the header entry to check existence of. */ public function responseHeaderExists(string $name, string $message = ''): void { - if ($message === '') { - $message = "Failed asserting that the response has a '$name' header."; - } $headers = $this->session->getResponseHeaders(); - $constraint = new ArrayHasKey($name); + $constraint = new ResponseHeaderExists($name); Assert::assertThat($headers, $constraint, $message); } @@ -80,12 +78,9 @@ public function responseHeaderExists(string $name, string $message = ''): void { * The name of the header entry to check existence of. */ public function responseHeaderDoesNotExist(string $name, string $message = ''): void { - if ($message === '') { - $message = "Failed asserting that the response does not have a '$name' header."; - } $headers = $this->session->getResponseHeaders(); $constraint = new LogicalNot( - new ArrayHasKey($name) + new ResponseHeaderExists($name) ); Assert::assertThat($headers, $constraint, $message); }