Added new methods submitForm and clickLink to Client class

This commit is contained in:
Viktor Novikov 2018-07-02 21:21:39 +03:00 committed by Fabien Potencier
parent 2b9c142f07
commit e098eddc96
2 changed files with 86 additions and 0 deletions

View File

@ -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.
*

View File

@ -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'));