Skip to content

Commit d07a653

Browse files
committed
[BrowserKit] Add PHPUnit constraints: BrowserHistoryIsOnFirstPage and BrowserHistoryIsOnLastPage
1 parent c50f247 commit d07a653

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public static function assertBrowserNotHasCookie(string $name, string $path = '/
122122
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
123123
}
124124

125+
public static function assertBrowserHistoryIsOnFirstPage(string $message = ''): void
126+
{
127+
self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage(), $message);
128+
}
129+
130+
public static function assertBrowserHistoryIsNotOnFirstPage(string $message = ''): void
131+
{
132+
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage()), $message);
133+
}
134+
135+
public static function assertBrowserHistoryIsOnLastPage(string $message = ''): void
136+
{
137+
self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnLastPage(), $message);
138+
}
139+
140+
public static function assertBrowserHistoryIsNotOnLastPage(string $message = ''): void
141+
{
142+
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnLastPage()), $message);
143+
}
144+
125145
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void
126146
{
127147
self::assertThatForClient(LogicalAnd::fromConstraints(

src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
use PHPUnit\Framework\AssertionFailedError;
1515
use PHPUnit\Framework\ExpectationFailedException;
16+
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
1718
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1819
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
1920
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
2021
use Symfony\Component\BrowserKit\Cookie;
2122
use Symfony\Component\BrowserKit\CookieJar;
23+
use Symfony\Component\BrowserKit\History;
2224
use Symfony\Component\DomCrawler\Crawler;
2325
use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie;
2426
use Symfony\Component\HttpFoundation\Request;
@@ -190,6 +192,38 @@ public function testAssertBrowserCookieValueSame()
190192
$this->getClientTester()->assertBrowserCookieValueSame('foo', 'babar', false, '/path');
191193
}
192194

195+
public function testAssertBrowserHistoryIsOnFirstPage()
196+
{
197+
$this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsOnFirstPage();
198+
$this->expectException(AssertionFailedError::class);
199+
$this->expectExceptionMessage('Failed asserting that the Browser history is on the first page.');
200+
$this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsOnFirstPage();
201+
}
202+
203+
public function testAssertBrowserHistoryIsNotOnFirstPage()
204+
{
205+
$this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsNotOnFirstPage();
206+
$this->expectException(AssertionFailedError::class);
207+
$this->expectExceptionMessage('Failed asserting that the Browser history is not on the first page.');
208+
$this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsNotOnFirstPage();
209+
}
210+
211+
public function testAssertBrowserHistoryIsOnLastPage()
212+
{
213+
$this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsOnLastPage();
214+
$this->expectException(AssertionFailedError::class);
215+
$this->expectExceptionMessage('Failed asserting that the Browser history is on the last page.');
216+
$this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsOnLastPage();
217+
}
218+
219+
public function testAssertBrowserHistoryIsNotOnLastPage()
220+
{
221+
$this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsNotOnLastPage();
222+
$this->expectException(AssertionFailedError::class);
223+
$this->expectExceptionMessage('Failed asserting that the Browser history is not on the last page.');
224+
$this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsNotOnLastPage();
225+
}
226+
193227
public function testAssertSelectorExists()
194228
{
195229
$this->getCrawlerTester(new Crawler('<html><body><h1>'))->assertSelectorExists('body > h1');
@@ -386,6 +420,19 @@ private function getRequestTester(): WebTestCase
386420
return $this->getTester($client);
387421
}
388422

423+
private function createHistoryTester(string $method, bool $returnValue): WebTestCase
424+
{
425+
/** @var KernelBrowser&MockObject $client */
426+
$client = $this->createMock(KernelBrowser::class);
427+
/** @var History&MockObject $history */
428+
$history = $this->createMock(History::class);
429+
430+
$history->method($method)->willReturn($returnValue);
431+
$client->method('getHistory')->willReturn($history);
432+
433+
return $this->getTester($client);
434+
}
435+
389436
private function getTester(KernelBrowser $client): WebTestCase
390437
{
391438
$tester = new class(method_exists($this, 'name') ? $this->name() : $this->getName()) extends WebTestCase {

src/Symfony/Component/BrowserKit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `isFirstPage()` and `isLastPage()` methods to the History class for checking navigation boundaries
8+
* Add PHPUnit constraints: `BrowserHistoryIsOnFirstPage` and `BrowserHistoryIsOnLastPage`
89

910
6.4
1011
---
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\BrowserKit\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\BrowserKit\AbstractBrowser;
16+
17+
final class BrowserHistoryIsOnFirstPage extends Constraint
18+
{
19+
public function toString(): string
20+
{
21+
return 'is on the first page';
22+
}
23+
24+
/**
25+
* @param AbstractBrowser $browser
26+
*/
27+
protected function matches($browser): bool
28+
{
29+
return $browser->getHistory()->isFirstPage();
30+
}
31+
32+
protected function failureDescription($browser): string
33+
{
34+
return 'the Browser history '.$this->toString();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\BrowserKit\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\BrowserKit\AbstractBrowser;
16+
17+
final class BrowserHistoryIsOnLastPage extends Constraint
18+
{
19+
public function toString(): string
20+
{
21+
return 'is on the last page';
22+
}
23+
24+
/**
25+
* @param AbstractBrowser $browser
26+
*/
27+
protected function matches($browser): bool
28+
{
29+
return $browser->getHistory()->isLastPage();
30+
}
31+
32+
protected function failureDescription($browser): string
33+
{
34+
return 'the Browser history '.$this->toString();
35+
}
36+
}

0 commit comments

Comments
 (0)