Skip to content

Commit

Permalink
Cleaned up header assertions, added assertions for redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 10, 2018
1 parent 221612d commit aad49b6
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 51 deletions.
13 changes: 4 additions & 9 deletions src/TestSuite/Constraint/Response/HeaderContains.php
Expand Up @@ -13,12 +13,10 @@
*/
namespace Cake\TestSuite\Constraint\Response;

use PHPUnit\Framework\AssertionFailedError;

/**
* HeaderContains
*/
class HeaderContains extends Header
class HeaderContains extends HeaderEquals
{

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
67 changes: 67 additions & 0 deletions src/TestSuite/Constraint/Response/HeaderEquals.php
@@ -0,0 +1,67 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.7.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Constraint\Response;

use Cake\Http\Response;

/**
* HeaderEquals
*/
class HeaderEquals extends ResponseBase
{
/**
* @var string
*/
protected $headerName;

/**
* @var string
*/
protected $value;

/**
* Constructor.
*
* @param Response $response Response
* @param string $headerName Header name
*/
public function __construct(Response $response, $headerName)
{
parent::__construct($response);

$this->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);
}
}
41 changes: 41 additions & 0 deletions src/TestSuite/Constraint/Response/HeaderNotSet.php
@@ -0,0 +1,41 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.7.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Constraint\Response;

/**
* HeaderSet
*/
class HeaderNotSet extends HeaderSet
{
/**
* Checks assertion
*
* @param mixed $other Expected content
* @return bool
*/
public function matches($other)
{
return parent::matches($other) === false;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('did not have header `%s`', $this->headerName);
}
}
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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);
}

/**
Expand Down
44 changes: 12 additions & 32 deletions src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit aad49b6

Please sign in to comment.