Skip to content

Commit 153e4bb

Browse files
tamasdaik099
authored andcommitted
Objects with __toString() can be asserted. (#760)
Fixed assertion, when object with "__toString" method is passed instead of a string
1 parent d5ee350 commit 153e4bb

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

src/WebAssert.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function responseHeaderContains($name, $value)
190190
$actual = $this->session->getResponseHeader($name);
191191
$message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);
192192

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

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

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

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

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

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

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

342342
/**

tests/Helper/Stringer.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Behat\Mink\Tests\Helper;
4+
5+
/**
6+
* A container class to hold a string.
7+
*/
8+
class Stringer
9+
{
10+
11+
/**
12+
* Internal storage.
13+
*
14+
* @var string
15+
*/
16+
private $content;
17+
18+
/**
19+
* Stringer constructor.
20+
*
21+
* @param string $content
22+
*/
23+
public function __construct($content)
24+
{
25+
$this->content = $content;
26+
}
27+
28+
/**
29+
* Returns the wrapped string.
30+
*
31+
* @return string
32+
*/
33+
public function __toString()
34+
{
35+
return $this->content;
36+
}
37+
38+
}

tests/WebAssertTest.php

+101
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Behat\Mink\Tests;
44

55
use Behat\Mink\Exception\ExpectationException;
6+
use Behat\Mink\Tests\Helper\Stringer;
67
use Behat\Mink\WebAssert;
78
use PHPUnit\Framework\TestCase;
89

@@ -285,6 +286,50 @@ public function testResponseHeaderNotContains()
285286
);
286287
}
287288

289+
public function testResponseHeaderContainsObjectWithToString()
290+
{
291+
$this->session
292+
->expects($this->any())
293+
->method('getResponseHeader')
294+
->will($this->returnValueMap(
295+
array(
296+
array('foo', 'bar'),
297+
array('bar', 'baz'),
298+
)
299+
));
300+
301+
$this->assertCorrectAssertion('responseHeaderContains', array('foo', new Stringer('ba')));
302+
$this->assertWrongAssertion(
303+
'responseHeaderContains',
304+
array('bar', 'bz'),
305+
'Behat\\Mink\\Exception\\ExpectationException',
306+
'The text "bz" was not found anywhere in the "bar" response header.'
307+
);
308+
}
309+
310+
public function testResponseHeaderNotContainsObjectWithToString()
311+
{
312+
$this->session
313+
->expects($this->any())
314+
->method('getResponseHeader')
315+
->will(
316+
$this->returnValueMap(
317+
array(
318+
array('foo', 'bar'),
319+
array('bar', 'baz'),
320+
)
321+
)
322+
);
323+
324+
$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', new Stringer('bz')));
325+
$this->assertWrongAssertion(
326+
'responseHeaderNotContains',
327+
array('bar', 'ba'),
328+
'Behat\\Mink\\Exception\\ExpectationException',
329+
'The text "ba" was found in the "bar" response header, but it should not.'
330+
);
331+
}
332+
288333
public function testResponseHeaderMatches()
289334
{
290335
$this->session
@@ -495,6 +540,62 @@ public function testResponseNotContains()
495540
);
496541
}
497542

543+
public function testResponseContainsObjectWithToString()
544+
{
545+
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
546+
->disableOriginalConstructor()
547+
->getMock()
548+
;
549+
550+
$this->session
551+
->expects($this->exactly(2))
552+
->method('getPage')
553+
->will($this->returnValue($page))
554+
;
555+
556+
$page
557+
->expects($this->exactly(2))
558+
->method('getContent')
559+
->will($this->returnValue('Some page text'))
560+
;
561+
562+
$this->assertCorrectAssertion('responseContains', array(new Stringer('PAGE text')));
563+
$this->assertWrongAssertion(
564+
'responseContains',
565+
array('html text'),
566+
'Behat\\Mink\\Exception\\ExpectationException',
567+
'The string "html text" was not found anywhere in the HTML response of the current page.'
568+
);
569+
}
570+
571+
public function testResponseNotContainsObjectWithToString()
572+
{
573+
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')
574+
->disableOriginalConstructor()
575+
->getMock()
576+
;
577+
578+
$this->session
579+
->expects($this->exactly(2))
580+
->method('getPage')
581+
->will($this->returnValue($page))
582+
;
583+
584+
$page
585+
->expects($this->exactly(2))
586+
->method('getContent')
587+
->will($this->returnValue('Some html text'))
588+
;
589+
590+
$this->assertCorrectAssertion('responseNotContains', array(new Stringer('PAGE text')));
591+
$this->assertWrongAssertion(
592+
'responseNotContains',
593+
array('HTML text'),
594+
'Behat\\Mink\\Exception\\ExpectationException',
595+
'The string "HTML text" appears in the HTML response of this page, but it should not.'
596+
);
597+
}
598+
498599
public function testResponseMatches()
499600
{
500601
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')

0 commit comments

Comments
 (0)