Skip to content

Commit

Permalink
Objects with __toString() can be asserted.
Browse files Browse the repository at this point in the history
In a recent change, some regex matches changed to stripos() calls.
The problem is that stripos() converts the needle parameter to int
if it is not a string. While objects having __toString() can be
converted into string by most of the internal PHP functions, they
cannot be converted to int, resulting in an error.
  • Loading branch information
tamasd committed Mar 27, 2018
1 parent 04ab7af commit 34f39d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
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
9 changes: 5 additions & 4 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\helpers\Stringer;
use Behat\Mink\WebAssert;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -255,7 +256,7 @@ public function testResponseHeaderContains()
)
));

$this->assertCorrectAssertion('responseHeaderContains', array('foo', 'ba'));
$this->assertCorrectAssertion('responseHeaderContains', array('foo', new Stringer('ba')));
$this->assertWrongAssertion(
'responseHeaderContains',
array('bar', 'bz'),
Expand All @@ -276,7 +277,7 @@ public function testResponseHeaderNotContains()
)
));

$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', 'bz'));
$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', new Stringer('bz')));
$this->assertWrongAssertion(
'responseHeaderNotContains',
array('bar', 'ba'),
Expand Down Expand Up @@ -458,7 +459,7 @@ public function testResponseContains()
->will($this->returnValue('Some page text'))
;

$this->assertCorrectAssertion('responseContains', array('PAGE text'));
$this->assertCorrectAssertion('responseContains', array(new Stringer('PAGE text')));
$this->assertWrongAssertion(
'responseContains',
array('html text'),
Expand Down Expand Up @@ -486,7 +487,7 @@ public function testResponseNotContains()
->will($this->returnValue('Some html text'))
;

$this->assertCorrectAssertion('responseNotContains', array('PAGE text'));
$this->assertCorrectAssertion('responseNotContains', array(new Stringer('PAGE text')));
$this->assertWrongAssertion(
'responseNotContains',
array('HTML text'),
Expand Down
42 changes: 42 additions & 0 deletions tests/helpers/Stringer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Behat\Mink\Tests\helpers;

/**
* Class Stringer
*
* A container class to hold a string.
*
* @package Behat\Mink\Tests\helpers
*/
class Stringer
{

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

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

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

}

0 comments on commit 34f39d8

Please sign in to comment.