Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add `isFirstPage()` and `isLastPage()` methods to the History class for checking navigation boundaries

6.4
---

Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/BrowserKit/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,30 @@ public function isEmpty(): bool
return 0 === \count($this->stack);
}

/**
* Returns true if the stack is on the first page.
*/
public function isFirstPage(): bool
{
return $this->position < 1;
}

/**
* Returns true if the stack is on the last page.
*/
public function isLastPage(): bool
{
return $this->position > \count($this->stack) - 2;
}

/**
* Goes back in the history.
*
* @throws LogicException if the stack is already on the first page
*/
public function back(): Request
{
if ($this->position < 1) {
if ($this->isFirstPage()) {
throw new LogicException('You are already on the first page.');
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ public function testBack()
$this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->back() keeps files');
$this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->back() keeps $_SERVER');
$this->assertSame($content, $client->getRequest()->getContent(), '->back() keeps content');
$this->assertTrue($client->getHistory()->isFirstPage());
$this->assertFalse($client->getHistory()->isLastPage());
}

public function testForward()
Expand Down Expand Up @@ -741,6 +743,8 @@ public function testBackAndFrowardWithRedirects()
$client->forward();

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

public function testReload()
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/HistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,34 @@ public function testForward()

$this->assertSame('http://www.example1.com/', $history->current()->getUri(), '->forward() returns the next request in the history');
}

public function testIsFirstPage()
{
$history = new History();
$history->add(new Request('http://www.example.com/', 'get'));
$history->add(new Request('http://www.example1.com/', 'get'));
$history->back();

$this->assertFalse($history->isLastPage());
$this->assertTrue($history->isFirstPage());
}

public function testIsLastPage()
{
$history = new History();
$history->add(new Request('http://www.example.com/', 'get'));
$history->add(new Request('http://www.example1.com/', 'get'));

$this->assertTrue($history->isLastPage());
$this->assertFalse($history->isFirstPage());
}

public function testIsFirstAndLastPage()
{
$history = new History();
$history->add(new Request('http://www.example.com/', 'get'));

$this->assertTrue($history->isLastPage());
$this->assertTrue($history->isFirstPage());
}
}
Loading