Skip to content

Commit f321c80

Browse files
committed
[BrowserKit] Emulate back/forward browser navigation
1 parent 549af73 commit f321c80

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Symfony/Component/BrowserKit/Client.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ abstract class Client
3838
protected $crawler;
3939
protected $insulated = false;
4040
protected $redirect;
41+
protected $redirects = array();
4142
protected $followRedirects = true;
43+
protected $browserNavigation = false;
4244

4345
private $maxRedirects = -1;
4446
private $redirectCount = 0;
@@ -78,6 +80,16 @@ public function isFollowingRedirects()
7880
return $this->followRedirects;
7981
}
8082

83+
/**
84+
* Sets whether to emulate back/forward browser navigation (skip redirects).
85+
*
86+
* @param bool $browserNavigation Whether to emulate browser navigation
87+
*/
88+
public function browserNavigation($browserNavigation = true)
89+
{
90+
$this->browserNavigation = (bool) $browserNavigation;
91+
}
92+
8193
/**
8294
* Sets the maximum number of requests that crawler can follow.
8395
*
@@ -328,6 +340,8 @@ public function request($method, $uri, array $parameters = array(), array $files
328340
}
329341

330342
if ($this->followRedirects && $this->redirect) {
343+
$this->redirects[spl_object_hash($this->history->current())] = true;
344+
331345
return $this->crawler = $this->followRedirect();
332346
}
333347

@@ -430,7 +444,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
430444
*/
431445
public function back()
432446
{
433-
return $this->requestFromRequest($this->history->back(), false);
447+
do {
448+
$request = $this->history->back();
449+
} while ($this->browserNavigation && array_key_exists(spl_object_hash($request), $this->redirects));
450+
451+
return $this->requestFromRequest($request, false);
434452
}
435453

436454
/**
@@ -440,7 +458,11 @@ public function back()
440458
*/
441459
public function forward()
442460
{
443-
return $this->requestFromRequest($this->history->forward(), false);
461+
do {
462+
$request = $this->history->forward();
463+
} while ($this->browserNavigation && array_key_exists(spl_object_hash($request), $this->redirects));
464+
465+
return $this->requestFromRequest($request, false);
444466
}
445467

446468
/**

0 commit comments

Comments
 (0)