Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added constraints for response status code assertions
  • Loading branch information
jeremyharris committed Apr 23, 2018
1 parent 8822119 commit 028800a
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 46 deletions.
40 changes: 40 additions & 0 deletions src/TestSuite/Constraint/Response/ResponseBase.php
@@ -0,0 +1,40 @@
<?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\Constraint\Constraint;

/**
* Base constraint for response constraints
*/
abstract class ResponseBase extends Constraint
{

/**
* @var \Cake\Http\Response
*/
protected $response;

/**
* Constructor
*
* @param \Cake\Http\Response $response Response
*/
public function __construct($response)
{
parent::__construct();

$this->response = $response;
}
}
31 changes: 31 additions & 0 deletions src/TestSuite/Constraint/Response/StatusCode.php
@@ -0,0 +1,31 @@
<?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;

/**
* StatusCode
*/
class StatusCode extends StatusCodeBase
{

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return sprintf('matches status code `%d`', $this->response->getStatusCode());
}
}
57 changes: 57 additions & 0 deletions src/TestSuite/Constraint/Response/StatusCodeBase.php
@@ -0,0 +1,57 @@
<?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;

/**
* StatusCodeBase
*/
abstract class StatusCodeBase extends ResponseBase
{

/**
* @var int|array
*/
protected $code;

/**
* Check assertion
*
* @param int|array $other Array of min/max status codes, or a single code
* @return bool
*/
public function matches($other)
{
if (!$other) {
$other = $this->code;
}

if (is_array($other)) {
return $this->statusCodeBetween($other[0], $other[1]);
}

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

/**
* Helper for checking status codes
*
* @param int $min Min status code (inclusive)
* @param int $max Max status code (inclusive)
* @return bool
*/
protected function statusCodeBetween($min, $max)
{
return $this->response->getStatusCode() >= $min && $this->response->getStatusCode() <= $max;
}
}
33 changes: 33 additions & 0 deletions src/TestSuite/Constraint/Response/StatusError.php
@@ -0,0 +1,33 @@
<?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;

/**
* StatusError
*/
class StatusError extends StatusCodeBase
{

protected $code = [400, 429];

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is not between 200 and 204';
}
}
33 changes: 33 additions & 0 deletions src/TestSuite/Constraint/Response/StatusFailure.php
@@ -0,0 +1,33 @@
<?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;

/**
* StatusFailure
*/
class StatusFailure extends StatusCodeBase
{

protected $code = [500, 505];

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is not between 200 and 204';
}
}
33 changes: 33 additions & 0 deletions src/TestSuite/Constraint/Response/StatusOk.php
@@ -0,0 +1,33 @@
<?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;

/**
* StatusOk
*/
class StatusOk extends StatusCodeBase
{

protected $code = [200, 204];

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is not between 200 and 204';
}
}
33 changes: 33 additions & 0 deletions src/TestSuite/Constraint/Response/StatusSuccess.php
@@ -0,0 +1,33 @@
<?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;

/**
* StatusSuccess
*/
class StatusSuccess extends StatusCodeBase
{

protected $code = [200, 308];

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is not between 200 and 308';
}
}
56 changes: 10 additions & 46 deletions src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -18,6 +18,11 @@
use Cake\Http\ServerRequest;
use Cake\Http\Session;
use Cake\Routing\Router;
use Cake\TestSuite\Constraint\Response\StatusCode;
use Cake\TestSuite\Constraint\Response\StatusError;
use Cake\TestSuite\Constraint\Response\StatusFailure;
use Cake\TestSuite\Constraint\Response\StatusOk;
use Cake\TestSuite\Constraint\Response\StatusSuccess;
use Cake\TestSuite\Stub\TestExceptionRenderer;
use Cake\Utility\CookieCryptTrait;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -704,10 +709,7 @@ public function viewVariable($name)
*/
public function assertResponseOk($message = null)
{
if (empty($message)) {
$message = 'Status code is not between 200 and 204';
}
$this->_assertStatus(200, 204, $message);
$this->assertThat(null, new StatusOk($this->_response), $message);
}

/**
Expand All @@ -718,10 +720,7 @@ public function assertResponseOk($message = null)
*/
public function assertResponseSuccess($message = null)
{
if (empty($message)) {
$message = 'Status code is not between 200 and 308';
}
$this->_assertStatus(200, 308, $message);
$this->assertThat(null, new StatusSuccess($this->_response), $message);
}

/**
Expand All @@ -732,10 +731,7 @@ public function assertResponseSuccess($message = null)
*/
public function assertResponseError($message = null)
{
if (empty($message)) {
$message = 'Status code is not between 400 and 429';
}
$this->_assertStatus(400, 429, $message);
$this->assertThat(null, new StatusError($this->_response), $message);
}

/**
Expand All @@ -746,10 +742,7 @@ public function assertResponseError($message = null)
*/
public function assertResponseFailure($message = null)
{
if (empty($message)) {
$message = 'Status code is not between 500 and 505';
}
$this->_assertStatus(500, 505, $message);
$this->assertThat(null, new StatusFailure($this->_response), $message);
}

/**
Expand All @@ -761,36 +754,7 @@ public function assertResponseFailure($message = null)
*/
public function assertResponseCode($code, $message = null)
{
$actual = $this->_response->getStatusCode();

if (empty($message)) {
$message = 'Status code is not ' . $code . ' but ' . $actual;
}

$this->_assertStatus($code, $code, $message);
}

/**
* Helper method for status assertions.
*
* @param int $min Min status code.
* @param int $max Max status code.
* @param string $message The error message.
* @return void
*/
protected function _assertStatus($min, $max, $message)
{
if (!$this->_response) {
$this->fail('No response set, cannot assert status code.');
}
$status = $this->_response->getStatusCode();

if ($this->_exception && ($status < $min || $status > $max)) {
$this->fail($this->_exception->getMessage());
}

$this->assertGreaterThanOrEqual($min, $status, $message);
$this->assertLessThanOrEqual($max, $status, $message);
$this->assertThat($code, new StatusCode($this->_response), $message);
}

/**
Expand Down

0 comments on commit 028800a

Please sign in to comment.