diff --git a/src/TestSuite/Constraint/Response/HeaderContains.php b/src/TestSuite/Constraint/Response/HeaderContains.php index 7aee0cb1ed3..ce0d1939e23 100644 --- a/src/TestSuite/Constraint/Response/HeaderContains.php +++ b/src/TestSuite/Constraint/Response/HeaderContains.php @@ -13,12 +13,10 @@ */ namespace Cake\TestSuite\Constraint\Response; -use PHPUnit\Framework\AssertionFailedError; - /** * HeaderContains */ -class HeaderContains extends Header +class HeaderContains extends HeaderEquals { /** @@ -29,11 +27,8 @@ class HeaderContains extends Header */ public function matches($other) { - if (!$this->response->hasHeader($this->headerName)) { - throw new AssertionFailedError(sprintf('Header `%s` was not set.', $this->headerName)); - } - - return mb_strpos($this->response->getHeaderLine($this->headerName), $other) !== false; + $this->value = $this->response->getHeaderLine($this->headerName); + return mb_strpos($this->value, $other) !== false; } /** @@ -43,6 +38,6 @@ public function matches($other) */ public function toString() { - return sprintf('is in header `%s`', $this->headerName); + return sprintf('is in header `%s`, found "%s"', $this->headerName, $this->value); } } diff --git a/src/TestSuite/Constraint/Response/HeaderEquals.php b/src/TestSuite/Constraint/Response/HeaderEquals.php new file mode 100644 index 00000000000..2218da264b4 --- /dev/null +++ b/src/TestSuite/Constraint/Response/HeaderEquals.php @@ -0,0 +1,67 @@ +headerName = $headerName; + } + + /** + * Checks assertion + * + * @param mixed $other Expected content + * @return bool + */ + public function matches($other) + { + $this->value = $this->response->getHeaderLine($this->headerName); + return $this->value === $other; + } + + /** + * Assertion message + * + * @return string + */ + public function toString() + { + return sprintf('equals content in header `%s`, found "%s"', $this->headerName, $this->value); + } +} diff --git a/src/TestSuite/Constraint/Response/HeaderNotSet.php b/src/TestSuite/Constraint/Response/HeaderNotSet.php new file mode 100644 index 00000000000..a2159e394ad --- /dev/null +++ b/src/TestSuite/Constraint/Response/HeaderNotSet.php @@ -0,0 +1,41 @@ +headerName); + } +} diff --git a/src/TestSuite/Constraint/Response/Header.php b/src/TestSuite/Constraint/Response/HeaderSet.php similarity index 71% rename from src/TestSuite/Constraint/Response/Header.php rename to src/TestSuite/Constraint/Response/HeaderSet.php index a0c23d36e22..3cf80463043 100644 --- a/src/TestSuite/Constraint/Response/Header.php +++ b/src/TestSuite/Constraint/Response/HeaderSet.php @@ -13,12 +13,12 @@ */ namespace Cake\TestSuite\Constraint\Response; -use PHPUnit\Framework\AssertionFailedError; +use Cake\Http\Response; /** - * Header + * HeaderSet */ -class Header extends ResponseBase +class HeaderSet extends ResponseBase { /** * @var string @@ -28,10 +28,10 @@ class Header extends ResponseBase /** * Constructor. * - * @param \Cake\Http\Response $response Response + * @param Response $response Response * @param string $headerName Header name */ - public function __construct(\Cake\Http\Response $response, $headerName) + public function __construct(Response $response, $headerName) { parent::__construct($response); @@ -46,11 +46,7 @@ public function __construct(\Cake\Http\Response $response, $headerName) */ public function matches($other) { - if (!$this->response->hasHeader($this->headerName)) { - throw new AssertionFailedError(sprintf('Header `%s` was not set.', $this->headerName)); - } - - return $this->response->getHeaderLine($this->headerName) === $other; + return $this->response->hasHeader($this->headerName); } /** diff --git a/src/TestSuite/IntegrationTestTrait.php b/src/TestSuite/IntegrationTestTrait.php index f5477e3eeda..04207aa3edf 100644 --- a/src/TestSuite/IntegrationTestTrait.php +++ b/src/TestSuite/IntegrationTestTrait.php @@ -23,8 +23,10 @@ use Cake\TestSuite\Constraint\Response\CookieNotSet; use Cake\TestSuite\Constraint\Response\CookieSet; use Cake\TestSuite\Constraint\Response\CookieEquals; -use Cake\TestSuite\Constraint\Response\Header; +use Cake\TestSuite\Constraint\Response\HeaderEquals; use Cake\TestSuite\Constraint\Response\HeaderContains; +use Cake\TestSuite\Constraint\Response\HeaderNotSet; +use Cake\TestSuite\Constraint\Response\HeaderSet; use Cake\TestSuite\Constraint\Response\StatusCode; use Cake\TestSuite\Constraint\Response\StatusError; use Cake\TestSuite\Constraint\Response\StatusFailure; @@ -787,19 +789,11 @@ public function assertResponseCode($code, $message = null) */ public function assertRedirect($url = null, $message = '') { - if (!$this->_response) { - $this->fail('No response set, cannot assert location header. ' . $message); - } - $result = $this->_response->getHeaderLine('Location'); - if ($url === null) { - $this->assertNotEmpty($result, $message); + $this->assertThat(null, new HeaderSet($this->_response, 'Location'), $message); - return; - } - if (empty($result)) { - $this->fail('No location header set. ' . $message); + if ($url) { + $this->assertThat(Router::url($url, ['_full' => true]), new HeaderEquals($this->_response, 'Location'), $message); } - $this->assertEquals(Router::url($url, ['_full' => true]), $result, $message); } /** @@ -811,14 +805,8 @@ public function assertRedirect($url = null, $message = '') */ public function assertRedirectContains($url, $message = '') { - if (!$this->_response) { - $this->fail('No response set, cannot assert location header. ' . $message); - } - $result = $this->_response->getHeaderLine('Location'); - if (empty($result)) { - $this->fail('No location header set. ' . $message); - } - $this->assertContains($url, $result, $message); + $this->assertThat(null, new HeaderSet($this->_response, 'Location'), $message); + $this->assertThat($url, new HeaderContains($this->_response, 'Location'), $message); } /** @@ -829,17 +817,7 @@ public function assertRedirectContains($url, $message = '') */ public function assertNoRedirect($message = '') { - if (!$this->_response) { - $this->fail('No response set, cannot assert location header. ' . $message); - } - $result = $this->_response->getHeaderLine('Location'); - if (!$message) { - $message = 'Redirect header set'; - } - if (!empty($result)) { - $message .= ': ' . $result; - } - $this->assertEmpty($result, $message); + $this->assertThat(null, new HeaderNotSet($this->_response, 'Location'), $message); } /** @@ -852,7 +830,8 @@ public function assertNoRedirect($message = '') */ public function assertHeader($header, $content, $message = '') { - $this->assertThat($content, new Header($this->_response, $header), $message); + $this->assertThat(null, new HeaderSet($this->_response, $header), $message); + $this->assertThat($content, new HeaderEquals($this->_response, $header), $message); } /** @@ -865,6 +844,7 @@ public function assertHeader($header, $content, $message = '') */ public function assertHeaderContains($header, $content, $message = '') { + $this->assertThat(null, new HeaderSet($this->_response, $header), $message); $this->assertThat($content, new HeaderContains($this->_response, $header), $message); }