Skip to content

Commit

Permalink
feature symfony#48841 [BrowserKit] Add argument $serverParameters to …
Browse files Browse the repository at this point in the history
…click() and clickLink() (syl20b)

This PR was merged into the 6.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 6.4 <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix symfony#48796 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT
| Doc PR        |  <!-- required for new features -->

Commits
-------

5148155 [BrowserKit] add serverParameters to click and clickLink method
  • Loading branch information
fabpot committed Aug 1, 2023
2 parents 15e082d + 5148155 commit 11b538c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
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

0 comments on commit 11b538c

Please sign in to comment.