Skip to content

Commit 67ed554

Browse files
committed
[BrowserKit] Add isFirstPage() and isLastPage() methods to History
1 parent 0d3fedf commit 67ed554

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/Symfony/Component/BrowserKit/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Add `isFirstPage()` and `isLastPage()` methods to the History class for checking navigation boundaries
8+
49
6.4
510
---
611

src/Symfony/Component/BrowserKit/History.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,30 @@ public function isEmpty(): bool
5050
return 0 === \count($this->stack);
5151
}
5252

53+
/**
54+
* Returns true if the stack is on the first page
55+
*/
56+
public function isFirstPage(): bool
57+
{
58+
return $this->position < 1;
59+
}
60+
61+
/**
62+
* Returns true if the stack is on the last page
63+
*/
64+
public function isLastPage(): bool
65+
{
66+
return $this->position > \count($this->stack) - 2;
67+
}
68+
5369
/**
5470
* Goes back in the history.
5571
*
5672
* @throws LogicException if the stack is already on the first page
5773
*/
5874
public function back(): Request
5975
{
60-
if ($this->position < 1) {
76+
if ($this->isFirstPage()) {
6177
throw new LogicException('You are already on the first page.');
6278
}
6379

@@ -71,7 +87,7 @@ public function back(): Request
7187
*/
7288
public function forward(): Request
7389
{
74-
if ($this->position > \count($this->stack) - 2) {
90+
if ($this->isLastPage()) {
7591
throw new LogicException('You are already on the last page.');
7692
}
7793

src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ public function testBack()
701701
$this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->back() keeps files');
702702
$this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->back() keeps $_SERVER');
703703
$this->assertSame($content, $client->getRequest()->getContent(), '->back() keeps content');
704+
$this->assertTrue($client->getHistory()->isFirstPage());
705+
$this->assertFalse($client->getHistory()->isLastPage());
704706
}
705707

706708
public function testForward()
@@ -741,6 +743,8 @@ public function testBackAndFrowardWithRedirects()
741743
$client->forward();
742744

743745
$this->assertSame('http://www.example.com/redirected', $client->getRequest()->getUri(), '->forward() goes forward in the history skipping redirects');
746+
$this->assertTrue($client->getHistory()->isLastPage());
747+
$this->assertFalse($client->getHistory()->isFirstPage());
744748
}
745749

746750
public function testReload()

src/Symfony/Component/BrowserKit/Tests/HistoryTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,34 @@ public function testForward()
9999

100100
$this->assertSame('http://www.example1.com/', $history->current()->getUri(), '->forward() returns the next request in the history');
101101
}
102+
103+
public function testIsFirstPage()
104+
{
105+
$history = new History();
106+
$history->add(new Request('http://www.example.com/', 'get'));
107+
$history->add(new Request('http://www.example1.com/', 'get'));
108+
$history->back();
109+
110+
$this->assertFalse($history->isLastPage());
111+
$this->assertTrue($history->isFirstPage());
112+
}
113+
114+
public function testIsLastPage()
115+
{
116+
$history = new History();
117+
$history->add(new Request('http://www.example.com/', 'get'));
118+
$history->add(new Request('http://www.example1.com/', 'get'));
119+
120+
$this->assertTrue($history->isLastPage());
121+
$this->assertFalse($history->isFirstPage());
122+
}
123+
124+
public function testIsFirstAndLastPage()
125+
{
126+
$history = new History();
127+
$history->add(new Request('http://www.example.com/', 'get'));
128+
129+
$this->assertTrue($history->isLastPage());
130+
$this->assertTrue($history->isFirstPage());
131+
}
102132
}

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