Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objects with __toString() can be asserted. #760

Merged
merged 3 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function responseHeaderContains($name, $value)
$actual = $this->session->getResponseHeader($name);
$message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);

$this->assert(false !== stripos($actual, $value), $message);
$this->assert(false !== stripos($actual, (string) $value), $message);
}

/**
Expand All @@ -206,7 +206,7 @@ public function responseHeaderNotContains($name, $value)
$actual = $this->session->getResponseHeader($name);
$message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name);

$this->assert(false === stripos($actual, $value), $message);
$this->assert(false === stripos($actual, (string) $value), $message);
}

/**
Expand Down Expand Up @@ -321,7 +321,7 @@ public function responseContains($text)
$actual = $this->session->getPage()->getContent();
$message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text);

$this->assert(stripos($actual, $text) !== false, $message);
$this->assert(stripos($actual, (string) $text) !== false, $message);
}

/**
Expand All @@ -336,7 +336,7 @@ public function responseNotContains($text)
$actual = $this->session->getPage()->getContent();
$message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text);

$this->assert(stripos($actual, $text) === false, $message);
$this->assert(stripos($actual, (string) $text) === false, $message);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Helper/Stringer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Behat\Mink\Tests\Helper;

/**
* A container class to hold a string.
*/
class Stringer
{

/**
* Internal storage.
*
* @var string
*/
private $content;

/**
* Stringer constructor.
*
* @param string $content
*/
public function __construct($content)
{
$this->content = $content;
}

/**
* Returns the wrapped string.
*
* @return string
*/
public function __toString()
{
return $this->content;
}

}
101 changes: 101 additions & 0 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Behat\Mink\Tests;

use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Tests\Helper\Stringer;
use Behat\Mink\WebAssert;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -285,6 +286,50 @@ public function testResponseHeaderNotContains()
);
}

public function testResponseHeaderContainsObjectWithToString()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will($this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
));

$this->assertCorrectAssertion('responseHeaderContains', array('foo', new Stringer('ba')));
$this->assertWrongAssertion(
'responseHeaderContains',
array('bar', 'bz'),
'Behat\\Mink\\Exception\\ExpectationException',
'The text "bz" was not found anywhere in the "bar" response header.'
);
}

public function testResponseHeaderNotContainsObjectWithToString()
{
$this->session
->expects($this->any())
->method('getResponseHeader')
->will(
$this->returnValueMap(
array(
array('foo', 'bar'),
array('bar', 'baz'),
)
)
);

$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', new Stringer('bz')));
$this->assertWrongAssertion(
'responseHeaderNotContains',
array('bar', 'ba'),
'Behat\\Mink\\Exception\\ExpectationException',
'The text "ba" was found in the "bar" response header, but it should not.'
);
}

public function testResponseHeaderMatches()
{
$this->session
Expand Down Expand Up @@ -495,6 +540,62 @@ public function testResponseNotContains()
);
}

public function testResponseContainsObjectWithToString()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
->disableOriginalConstructor()
->getMock()
;

$this->session
->expects($this->exactly(2))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(2))
->method('getContent')
->will($this->returnValue('Some page text'))
;

$this->assertCorrectAssertion('responseContains', array(new Stringer('PAGE text')));
$this->assertWrongAssertion(
'responseContains',
array('html text'),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "html text" was not found anywhere in the HTML response of the current page.'
);
}

public function testResponseNotContainsObjectWithToString()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
->disableOriginalConstructor()
->getMock()
;

$this->session
->expects($this->exactly(2))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(2))
->method('getContent')
->will($this->returnValue('Some html text'))
;

$this->assertCorrectAssertion('responseNotContains', array(new Stringer('PAGE text')));
$this->assertWrongAssertion(
'responseNotContains',
array('HTML text'),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "HTML text" appears in the HTML response of this page, but it should not.'
);
}

public function testResponseMatches()
{
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
Expand Down