Skip to content

Commit

Permalink
[BrowserKit] add serverParameters to click and clickLink method
Browse files Browse the repository at this point in the history
  • Loading branch information
syl20b authored and nicolas-grekas committed Jul 31, 2023
1 parent a747429 commit 3f5752f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
19 changes: 13 additions & 6 deletions AbstractBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,33 @@ public function getRequest(): object

/**
* Clicks on a given link.
*
* @param array $serverParameters An array of server parameters
*/
public function click(Link $link): Crawler
public function click(Link $link/* , array $serverParameters = [] */): Crawler
{
$serverParameters = 1 < \func_num_args() ? func_get_arg(1) : [];

if ($link instanceof Form) {
return $this->submit($link);
return $this->submit($link, [], $serverParameters);
}

return $this->request($link->getMethod(), $link->getUri());
return $this->request($link->getMethod(), $link->getUri(), [], [], $serverParameters);
}

/**
* Clicks the first link (or clickable image) that contains the given text.
*
* @param string $linkText The text of the link or the alt attribute of the clickable image
* @param string $linkText The text of the link or the alt attribute of the clickable image
* @param array $serverParameters An array of server parameters
*/
public function clickLink(string $linkText): Crawler
public function clickLink(string $linkText/* , array $serverParameters = [] */): Crawler
{
$serverParameters = 1 < \func_num_args() ? func_get_arg(1) : [];

$crawler = $this->crawler ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__));

return $this->click($crawler->selectLink($linkText)->link());
return $this->click($crawler->selectLink($linkText)->link(), $serverParameters);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add argument `$serverParameters` to `AbstractBrowser::click()` and `AbstractBrowser::clickLink()`

6.3
---

Expand Down
38 changes: 38 additions & 0 deletions Tests/AbstractBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ public function testClick()
$this->assertSame('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
}

public function testClickPreserveHeaders()
{
$client = $this->getBrowser();
$client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
$crawler = $client->request('GET', 'http://www.example.com/foo/foobar');

$client->click($crawler->filter('a')->link(), ['X-Special-Header' => 'Special Header Value']);

$server = $client->getRequest()->getServer();
$this->assertArrayHasKey('X-Special-Header', $server);
$this->assertSame('Special Header Value', $server['X-Special-Header']);
}

public function testClickLink()
{
$client = $this->getBrowser();
Expand All @@ -299,6 +312,18 @@ public function testClickLinkNotFound()
$client->clickLink('foo');
}

public function testClickLinkPreserveHeaders()
{
$client = $this->getBrowser();
$client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
$client->request('GET', 'http://www.example.com/foo/foobar');
$client->clickLink('foo', ['X-Special-Header' => 'Special Header Value']);

$server = $client->getRequest()->getServer();
$this->assertArrayHasKey('X-Special-Header', $server);
$this->assertSame('Special Header Value', $server['X-Special-Header']);
}

public function testClickForm()
{
$client = $this->getBrowser();
Expand All @@ -310,6 +335,19 @@ public function testClickForm()
$this->assertSame('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() Form submit forms');
}

public function testClickFormPreserveHeaders()
{
$client = $this->getBrowser();
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
$crawler = $client->request('GET', 'http://www.example.com/foo/foobar');

$client->click($crawler->filter('input')->form(), ['X-Special-Header' => 'Special Header Value']);

$server = $client->getRequest()->getServer();
$this->assertArrayHasKey('X-Special-Header', $server);
$this->assertSame('Special Header Value', $server['X-Special-Header']);
}

public function testSubmit()
{
$client = $this->getBrowser();
Expand Down

0 comments on commit 3f5752f

Please sign in to comment.