Skip to content

Commit 76e80c1

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

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPUnit\Framework\Constraint\LogicalNot;
1717
use PHPUnit\Framework\ExpectationFailedException;
1818
use Symfony\Component\BrowserKit\AbstractBrowser;
19+
use Symfony\Component\BrowserKit\History;
1920
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
2021
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\HttpFoundation\Response;
@@ -122,6 +123,38 @@ public static function assertBrowserNotHasCookie(string $name, string $path = '/
122123
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
123124
}
124125

126+
public static function assertBrowserHistoryIsOnFirstPage(string $message = ''): void
127+
{
128+
if (!method_exists(History::class, 'isFirstPage')) {
129+
throw new \LogicException('The `assertBrowserHistoryIsOnFirstPage` method requires symfony/browser-kit >= 7.4.');
130+
}
131+
self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage(), $message);
132+
}
133+
134+
public static function assertBrowserHistoryIsNotOnFirstPage(string $message = ''): void
135+
{
136+
if (!method_exists(History::class, 'isFirstPage')) {
137+
throw new \LogicException('The `assertBrowserHistoryIsNotOnFirstPage` method requires symfony/browser-kit >= 7.4.');
138+
}
139+
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage()), $message);
140+
}
141+
142+
public static function assertBrowserHistoryIsOnLastPage(string $message = ''): void
143+
{
144+
if (!method_exists(History::class, 'isLastPage')) {
145+
throw new \LogicException('The `assertBrowserHistoryIsOnLastPage` method requires symfony/browser-kit >= 7.4.');
146+
}
147+
self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnLastPage(), $message);
148+
}
149+
150+
public static function assertBrowserHistoryIsNotOnLastPage(string $message = ''): void
151+
{
152+
if (!method_exists(History::class, 'isLastPage')) {
153+
throw new \LogicException('The `assertBrowserHistoryIsNotOnLastPage` method requires symfony/browser-kit >= 7.4.');
154+
}
155+
self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnLastPage()), $message);
156+
}
157+
125158
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void
126159
{
127160
self::assertThatForClient(LogicalAnd::fromConstraints(

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

Lines changed: 59 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,50 @@ public function testAssertBrowserCookieValueSame()
190192
$this->getClientTester()->assertBrowserCookieValueSame('foo', 'babar', false, '/path');
191193
}
192194

195+
/**
196+
* @requires function \Symfony\Component\BrowserKit\History::isFirstPage
197+
*/
198+
public function testAssertBrowserHistoryIsOnFirstPage()
199+
{
200+
$this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsOnFirstPage();
201+
$this->expectException(AssertionFailedError::class);
202+
$this->expectExceptionMessage('Failed asserting that the Browser history is on the first page.');
203+
$this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsOnFirstPage();
204+
}
205+
206+
/**
207+
* @requires function \Symfony\Component\BrowserKit\History::isFirstPage
208+
*/
209+
public function testAssertBrowserHistoryIsNotOnFirstPage()
210+
{
211+
$this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsNotOnFirstPage();
212+
$this->expectException(AssertionFailedError::class);
213+
$this->expectExceptionMessage('Failed asserting that the Browser history is not on the first page.');
214+
$this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsNotOnFirstPage();
215+
}
216+
217+
/**
218+
* @requires function \Symfony\Component\BrowserKit\History::isLastPage
219+
*/
220+
public function testAssertBrowserHistoryIsOnLastPage()
221+
{
222+
$this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsOnLastPage();
223+
$this->expectException(AssertionFailedError::class);
224+
$this->expectExceptionMessage('Failed asserting that the Browser history is on the last page.');
225+
$this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsOnLastPage();
226+
}
227+
228+
/**
229+
* @requires function \Symfony\Component\BrowserKit\History::isLastPage
230+
*/
231+
public function testAssertBrowserHistoryIsNotOnLastPage()
232+
{
233+
$this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsNotOnLastPage();
234+
$this->expectException(AssertionFailedError::class);
235+
$this->expectExceptionMessage('Failed asserting that the Browser history is not on the last page.');
236+
$this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsNotOnLastPage();
237+
}
238+
193239
public function testAssertSelectorExists()
194240
{
195241
$this->getCrawlerTester(new Crawler('<html><body><h1>'))->assertSelectorExists('body > h1');
@@ -386,6 +432,19 @@ private function getRequestTester(): WebTestCase
386432
return $this->getTester($client);
387433
}
388434

435+
private function createHistoryTester(string $method, bool $returnValue): WebTestCase
436+
{
437+
/** @var KernelBrowser&MockObject $client */
438+
$client = $this->createMock(KernelBrowser::class);
439+
/** @var History&MockObject $history */
440+
$history = $this->createMock(History::class);
441+
442+
$history->method($method)->willReturn($returnValue);
443+
$client->method('getHistory')->willReturn($history);
444+
445+
return $this->getTester($client);
446+
}
447+
389448
private function getTester(KernelBrowser $client): WebTestCase
390449
{
391450
$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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
protected function matches($other): bool
25+
{
26+
if (!$other instanceof AbstractBrowser) {
27+
throw new \LogicException('Can only test on an AbstractBrowser instance.');
28+
}
29+
30+
return $other->getHistory()->isFirstPage();
31+
}
32+
33+
protected function failureDescription($other): string
34+
{
35+
return 'the Browser history '.$this->toString();
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
protected function matches($other): bool
25+
{
26+
if (!$other instanceof AbstractBrowser) {
27+
throw new \LogicException('Can only test on an AbstractBrowser instance.');
28+
}
29+
30+
return $other->getHistory()->isLastPage();
31+
}
32+
33+
protected function failureDescription($other): string
34+
{
35+
return 'the Browser history '.$this->toString();
36+
}
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy