Skip to content

Commit

Permalink
Added constraints for header, content type
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 4, 2018
1 parent 028800a commit e1bde25
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 25 deletions.
47 changes: 47 additions & 0 deletions src/TestSuite/Constraint/Response/ContentType.php
@@ -0,0 +1,47 @@
<?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;

/**
* ContentType
*/
class ContentType extends ResponseBase
{

/**
* Checks assertion
*
* @param mixed $other Expected type
* @return bool
*/
public function matches($other)
{
$alias = $this->response->getMimeType($other);
if ($alias !== false) {
$other = $alias;
}

return $other === $this->response->getType();
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'was set as the Content-Type';
}
}
65 changes: 65 additions & 0 deletions src/TestSuite/Constraint/Response/Header.php
@@ -0,0 +1,65 @@
<?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 PHPUnit\Framework\AssertionFailedError;

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

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

$this->headerName = $headerName;
}

/**
* Checks assertion
*
* @param mixed $other Expected content
* @return bool
*/
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;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('has header `%s`', $this->headerName);
}
}
48 changes: 48 additions & 0 deletions src/TestSuite/Constraint/Response/HeaderContains.php
@@ -0,0 +1,48 @@
<?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 PHPUnit\Framework\AssertionFailedError;

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

/**
* Checks assertion
*
* @param mixed $other Expected content
* @return bool
*/
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;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('is in header `%s`', $this->headerName);
}
}
5 changes: 5 additions & 0 deletions src/TestSuite/Constraint/Response/ResponseBase.php
Expand Up @@ -13,6 +13,7 @@
*/
namespace Cake\TestSuite\Constraint\Response;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\Constraint;

/**
Expand All @@ -35,6 +36,10 @@ public function __construct($response)
{
parent::__construct();

if (!$response) {
throw new AssertionFailedError('No response set, cannot assert content.');
}

$this->response = $response;
}
}
31 changes: 6 additions & 25 deletions src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -18,6 +18,9 @@
use Cake\Http\ServerRequest;
use Cake\Http\Session;
use Cake\Routing\Router;
use Cake\TestSuite\Constraint\Response\ContentType;
use Cake\TestSuite\Constraint\Response\Header;
use Cake\TestSuite\Constraint\Response\HeaderContains;
use Cake\TestSuite\Constraint\Response\StatusCode;
use Cake\TestSuite\Constraint\Response\StatusError;
use Cake\TestSuite\Constraint\Response\StatusFailure;
Expand Down Expand Up @@ -833,14 +836,7 @@ public function assertNoRedirect($message = '')
*/
public function assertHeader($header, $content, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert headers. ' . $message);
}
if (!$this->_response->hasHeader($header)) {
$this->fail("The '$header' header is not set. " . $message);
}
$actual = $this->_response->getHeaderLine($header);
$this->assertEquals($content, $actual, $message);
$this->assertThat($content, new Header($this->_response, $header), $message);
}

/**
Expand All @@ -853,14 +849,7 @@ public function assertHeader($header, $content, $message = '')
*/
public function assertHeaderContains($header, $content, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert headers. ' . $message);
}
if (!$this->_response->hasHeader($header)) {
$this->fail("The '$header' header is not set. " . $message);
}
$actual = $this->_response->getHeaderLine($header);
$this->assertContains($content, $actual, $message);
$this->assertThat($content, new HeaderContains($this->_response, $header), $message);
}

/**
Expand All @@ -872,15 +861,7 @@ public function assertHeaderContains($header, $content, $message = '')
*/
public function assertContentType($type, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content-type. ' . $message);
}
$alias = $this->_response->getMimeType($type);
if ($alias !== false) {
$type = $alias;
}
$result = $this->_response->getType();
$this->assertEquals($type, $result, $message);
$this->assertThat($type, new ContentType($this->_response), $message);
}

/**
Expand Down

0 comments on commit e1bde25

Please sign in to comment.