Skip to content

Commit

Permalink
Added body related constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 4, 2018
1 parent e1bde25 commit 4975dfc
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/TestSuite/IntegrationTestTrait.php
Expand Up @@ -35,6 +35,11 @@
use Exception;
use LogicException;
use PHPUnit\Exception as PhpunitException;
use PHPUnit\Framework\Constraint\IsEmpty;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\Constraint\RegularExpression;
use PHPUnit\Framework\Constraint\StringContains;

/**
* A trait intended to make integration tests of your controllers easier.
Expand Down Expand Up @@ -681,6 +686,10 @@ protected function _url($url)
*/
protected function _getBodyAsString()
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content.');
}

return (string)$this->_response->getBody();
}

Expand Down Expand Up @@ -873,41 +882,36 @@ public function assertContentType($type, $message = '')
*/
public function assertResponseEquals($content, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertEquals($content, $this->_getBodyAsString(), $message);
$this->assertThat($this->_getBodyAsString(), new IsEqual($content), $message);
}

/**
* Asserts content exists in the response body.
*
* @param string $content The content to check for.
* @param string $message The failure message that will be appended to the generated message.
* @param bool $ignoreCase A flag to check whether we should ignore case or not.
* @param bool $ignoreCase A flag to check whether we should ignore case or not.
* @return void
*/
public function assertResponseContains($content, $message = '', $ignoreCase = false)
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertContains($content, $this->_getBodyAsString(), $message, $ignoreCase);
$this->assertThat($this->_getBodyAsString(), new StringContains($content, $ignoreCase), $message);
}

/**
* Asserts content does not exist in the response body.
*
* @param string $content The content to check for.
* @param string $message The failure message that will be appended to the generated message.
* @param bool $ignoreCase A flag to check whether we should ignore case or not.
* @return void
*/
public function assertResponseNotContains($content, $message = '')
public function assertResponseNotContains($content, $message = '', $ignoreCase = false)
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertNotContains($content, $this->_getBodyAsString(), $message);
$constraint = new LogicalNot(
new StringContains($content, $ignoreCase)
);
$this->assertThat($this->_getBodyAsString(), $constraint, $message);
}

/**
Expand All @@ -919,10 +923,8 @@ public function assertResponseNotContains($content, $message = '')
*/
public function assertResponseRegExp($pattern, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertRegExp($pattern, $this->_getBodyAsString(), $message);
$constraint = new RegularExpression($pattern);
$this->assertThat($this->_getBodyAsString(), $constraint, $message);
}

/**
Expand All @@ -934,10 +936,10 @@ public function assertResponseRegExp($pattern, $message = '')
*/
public function assertResponseNotRegExp($pattern, $message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertNotRegExp($pattern, $this->_getBodyAsString(), $message);
$constraint = new LogicalNot(
new RegularExpression($pattern)
);
$this->assertThat($this->_getBodyAsString(), $constraint, $message);
}

/**
Expand All @@ -948,10 +950,10 @@ public function assertResponseNotRegExp($pattern, $message = '')
*/
public function assertResponseNotEmpty($message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertNotEmpty($this->_getBodyAsString(), $message);
$constraint = new LogicalNot(
new IsEmpty()
);
$this->assertThat($this->_getBodyAsString(), $constraint, $message);
}
/**
* Assert response content is empty.
Expand All @@ -961,10 +963,7 @@ public function assertResponseNotEmpty($message = '')
*/
public function assertResponseEmpty($message = '')
{
if (!$this->_response) {
$this->fail('No response set, cannot assert content. ' . $message);
}
$this->assertEmpty($this->_getBodyAsString(), $message);
$this->assertThat($this->_getBodyAsString(), new IsEmpty(), $message);
}

/**
Expand Down

0 comments on commit 4975dfc

Please sign in to comment.