Skip to content

Commit 4539661

Browse files
feature symfony#60895 [BrowserKit] Add isFirstPage() and isLastPage() methods to History (santysisi)
This PR was merged into the 7.4 branch. Discussion ---------- [BrowserKit] Add `isFirstPage()` and `isLastPage()` methods to History | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | No | License | MIT This PR introduces two new utility methods - `isFirstPage()`: Returns `true` if the current position is at the start of the history stack. - `isLastPage()`: Returns `true` if the current position is at the end of the history stack. These methods are particularly useful in a testing context. For example, they enable clearer and more targeted assertions, such as: ```php $this->assertTrue($client->getHistory()->isLastPage()); $this->assertTrue($client->getHistory()->isFirstPage()); $this->assertFalse($client->getHistory()->isFirstPage()); $this->assertFalse($client->getHistory()->isLastPage()); ``` These new methods also allow developers to proactively check the navigation state before calling `back()` or `forward()`, which can be useful in certain scenarios to avoid unnecessary exceptions and reduce the need for additional error handling logic. Commits ------- 8b2045e [BrowserKit] Add `isFirstPage()` and `isLastPage()` methods to History
2 parents 5838424 + 8b2045e commit 4539661

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)