Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added constraints for response status code assertions
- Loading branch information
1 parent
8822119
commit 028800a
Showing
8 changed files
with
270 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters