feature #27807 Added new methods submitForm and clickLink to Client class (nowiko)
This PR was squashed before being merged into the 4.2-dev branch (closes #27807).
Discussion
----------
Added new methods submitForm and clickLink to Client class
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| License | MIT
Added new methods to BrowserKit/Client to simplify work with links and forms.
Related to https://github.com/symfony/symfony/issues/27627
Commits
-------
e098eddc96
Added new methods submitForm and clickLink to Client class
This commit is contained in:
commit
20070b72ac
@ -290,6 +290,20 @@ abstract class Client
|
||||
return $this->request($link->getMethod(), $link->getUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds link by given text and then clicks on it.
|
||||
*
|
||||
* @param string $value The link text
|
||||
*
|
||||
* @return Crawler
|
||||
*/
|
||||
public function clickLink($value)
|
||||
{
|
||||
$link = $this->getCrawler()->selectLink($value)->link();
|
||||
|
||||
return $this->click($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits a form.
|
||||
*
|
||||
@ -307,6 +321,23 @@ abstract class Client
|
||||
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), $serverParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a form by submit button text and then submits it.
|
||||
*
|
||||
* @param string $button The button text
|
||||
* @param array $values An array of form field values
|
||||
* @param string $method The method for the form
|
||||
*
|
||||
* @return Crawler
|
||||
*/
|
||||
public function submitForm($button, $values, $method)
|
||||
{
|
||||
$buttonNode = $this->getCrawler()->selectButton($button);
|
||||
$form = $buttonNode->form($values, $method);
|
||||
|
||||
return $this->submit($form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a URI.
|
||||
*
|
||||
|
@ -322,6 +322,30 @@ class ClientTest extends TestCase
|
||||
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
|
||||
}
|
||||
|
||||
public function testClickLink()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
|
||||
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||
$client->clickLink('foo');
|
||||
|
||||
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
|
||||
}
|
||||
|
||||
public function testClickLinkNotFound()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$client->setNextResponse(new Response('<html><a href="/foo">foobar</a></html>'));
|
||||
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||
|
||||
try {
|
||||
$client->clickLink('foo');
|
||||
$this->fail('->clickLink() throws a \InvalidArgumentException if the link could not be found');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('InvalidArgumentException', $e, '->clickLink() throws a \InvalidArgumentException if the link could not be found');
|
||||
}
|
||||
}
|
||||
|
||||
public function testClickForm()
|
||||
{
|
||||
$client = new TestClient();
|
||||
@ -344,6 +368,37 @@ class ClientTest extends TestCase
|
||||
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
|
||||
}
|
||||
|
||||
public function testSubmitForm()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$client->setNextResponse(new Response('<html><form name="signup" action="/foo"><input type="text" name="username" /><input type="password" name="password" /><input type="submit" value="Register" /></form></html>'));
|
||||
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||
|
||||
$client->submitForm('Register', array(
|
||||
'username' => 'username',
|
||||
'password' => 'password',
|
||||
), 'POST');
|
||||
|
||||
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
|
||||
}
|
||||
|
||||
public function testSubmitFormNotFound()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
|
||||
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||
|
||||
try {
|
||||
$client->submitForm('Register', array(
|
||||
'username' => 'username',
|
||||
'password' => 'password',
|
||||
), 'POST');
|
||||
$this->fail('->submitForm() throws a \InvalidArgumentException if the form could not be found');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('InvalidArgumentException', $e, '->submitForm() throws a \InvalidArgumentException if the form could not be found');
|
||||
}
|
||||
}
|
||||
|
||||
public function testSubmitPreserveAuth()
|
||||
{
|
||||
$client = new TestClient(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'bar'));
|
||||
|
Reference in New Issue
Block a user