Skip to content

Commit

Permalink
Gave body assertions proper constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 10, 2018
1 parent 0738fe0 commit e2f581b
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 26 deletions.
69 changes: 69 additions & 0 deletions src/TestSuite/Constraint/Response/BodyContains.php
@@ -0,0 +1,69 @@
<?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;

/**
* BodyContains
*
* @internal
*/
class BodyContains extends ResponseBase
{

/**
* @var bool
*/
protected $ignoreCase;

/**
* Constructor.
*
* @param Response $response Response
* @param bool $ignoreCase Ignore case
*/
public function __construct(Response $response, $ignoreCase = false)
{
parent::__construct($response);

$this->ignoreCase = $ignoreCase;
}

/**
* Checks assertion
*
* @param mixed $other Expected type
* @return bool
*/
public function matches($other)
{
$method = 'mb_strpos';
if ($this->ignoreCase) {
$method = 'mb_stripos';
}

return $method($this->_getBodyAsString(), $other) !== false;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is in response body';
}
}
55 changes: 55 additions & 0 deletions src/TestSuite/Constraint/Response/BodyEmpty.php
@@ -0,0 +1,55 @@
<?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;

/**
* BodyEmpty
*
* @internal
*/
class BodyEmpty extends ResponseBase
{

/**
* Checks assertion
*
* @param mixed $other Expected type
* @return bool
*/
public function matches($other)
{
return empty($this->_getBodyAsString());
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'response body is empty';
}

/**
* Overwrites the descriptions so we can remove the automatic "expected" message
*
* @param mixed $other Value
* @return string
*/
protected function failureDescription($other)
{
return $this->toString();
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyEquals.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyEquals
*
* @internal
*/
class BodyEquals extends ResponseBase
{

/**
* Checks assertion
*
* @param mixed $other Expected type
* @return bool
*/
public function matches($other)
{
return $this->_getBodyAsString() === $other;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'matches response body';
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyNotContains.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyContains
*
* @internal
*/
class BodyNotContains extends BodyContains
{

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

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'is not in response body';
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyNotEmpty.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyNotEmpty
*
* @internal
*/
class BodyNotEmpty extends BodyEmpty
{

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

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'response body is not empty';
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyNotEquals.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyNotEquals
*
* @internal
*/
class BodyNotEquals extends BodyEquals
{

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

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'does not match response body';
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyNotRegExp.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyNotRegExp
*
* @internal
*/
class BodyNotRegExp extends ResponseBase
{

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

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'PCRE pattern not found in response body';
}
}
44 changes: 44 additions & 0 deletions src/TestSuite/Constraint/Response/BodyRegExp.php
@@ -0,0 +1,44 @@
<?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;

/**
* BodyRegExp
*
* @internal
*/
class BodyRegExp extends ResponseBase
{

/**
* Checks assertion
*
* @param mixed $other Expected pattern
* @return bool
*/
public function matches($other)
{
return preg_match($other, $this->_getBodyAsString()) > 0;
}

/**
* Assertion message
*
* @return string
*/
public function toString()
{
return 'PCRE pattern found in response body';
}
}

0 comments on commit e2f581b

Please sign in to comment.