diff --git a/UPGRADE-6.4.md b/UPGRADE-6.4.md index 216c33067c0ac..425c2aecebc05 100644 --- a/UPGRADE-6.4.md +++ b/UPGRADE-6.4.md @@ -1,6 +1,11 @@ UPGRADE FROM 6.3 to 6.4 ======================= +BrowserKit +---------- + + * Add argument `$serverParameters` to `AbstractBrowser::click()` and `AbstractBrowser::clickLink()` + Cache ----- diff --git a/src/Symfony/Component/BrowserKit/AbstractBrowser.php b/src/Symfony/Component/BrowserKit/AbstractBrowser.php index 4caedaf1e9123..9193a205599c1 100644 --- a/src/Symfony/Component/BrowserKit/AbstractBrowser.php +++ b/src/Symfony/Component/BrowserKit/AbstractBrowser.php @@ -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); } /** diff --git a/src/Symfony/Component/BrowserKit/CHANGELOG.md b/src/Symfony/Component/BrowserKit/CHANGELOG.md index 2d2ea9a75c2c8..b05e3079e7a52 100644 --- a/src/Symfony/Component/BrowserKit/CHANGELOG.md +++ b/src/Symfony/Component/BrowserKit/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.4 +--- + + * Add argument `$serverParameters` to `AbstractBrowser::click()` and `AbstractBrowser::clickLink()` + 6.3 --- diff --git a/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php b/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php index dd449bea5e428..03bdc8f72bc2a 100644 --- a/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php @@ -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('foo')); + $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(); @@ -299,6 +312,18 @@ public function testClickLinkNotFound() $client->clickLink('foo'); } + public function testClickLinkPreserveHeaders() + { + $client = $this->getBrowser(); + $client->setNextResponse(new Response('foo')); + $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(); @@ -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('
')); + $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();