Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BrowserKit] Add argument $serverParameters to click() and clickLink() #48841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 UPGRADE-6.4.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
UPGRADE FROM 6.3 to 6.4
=======================

BrowserKit
----------

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

Cache
-----

Expand Down
19 changes: 13 additions & 6 deletions src/Symfony/Component/BrowserKit/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 src/Symfony/Component/BrowserKit/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 src/Symfony/Component/BrowserKit/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