Added types and tweaked PHPdoc of clickLink() and submitForm() methods

This commit is contained in:
Javier Eguiluz 2018-07-15 10:36:30 +02:00 committed by Fabien Potencier
parent eb112a5288
commit be9d578d4b
2 changed files with 26 additions and 22 deletions

View File

@ -291,17 +291,13 @@ abstract class Client
} }
/** /**
* Finds link by given text and then clicks on it. * Clicks the first link (or clickable image) that contains the given text.
* *
* @param string $value The link text * @param string $linkText The text of the link or the alt attribute of the clickable image
*
* @return Crawler
*/ */
public function clickLink($value) public function clickLink(string $linkText): Crawler
{ {
$link = $this->getCrawler()->selectLink($value)->link(); return $this->click($this->getCrawler()->selectLink($linkText)->link());
return $this->click($link);
} }
/** /**
@ -322,20 +318,20 @@ abstract class Client
} }
/** /**
* Finds a form by submit button text and then submits it. * Finds the first form that contains a button with the given content and
* uses it to submit the given form field values.
* *
* @param string $button The button text * @param string $button The text content, id, value or name of the form <button> or <input type="submit">
* @param array $values An array of form field values * @param array $fieldValues Use this syntax: array('my_form[name]' => '...', 'my_form[email]' => '...')
* @param string $method The method for the form * @param string $method The HTTP method used to submit the form
* * @param array $serverParameters These values override the ones stored in $_SERVER (HTTP headers must include a HTTP_ prefix as PHP does)
* @return Crawler
*/ */
public function submitForm($button, $values, $method) public function submitForm(string $button, array $fieldValues = array(), string $method = 'POST', array $serverParameters = array()): Crawler
{ {
$buttonNode = $this->getCrawler()->selectButton($button); $buttonNode = $this->getCrawler()->selectButton($button);
$form = $buttonNode->form($values, $method); $form = $buttonNode->form($fieldValues, $method);
return $this->submit($form); return $this->submit($form, array(), $serverParameters);
} }
/** /**

View File

@ -371,15 +371,23 @@ class ClientTest extends TestCase
public function testSubmitForm() public function testSubmitForm()
{ {
$client = new TestClient(); $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->setNextResponse(new Response('<html><form name="signup" action="/foo"><input type="text" name="username" value="the username" /><input type="password" name="password" value="the password" /><input type="submit" value="Register" /></form></html>'));
$client->request('GET', 'http://www.example.com/foo/foobar'); $client->request('GET', 'http://www.example.com/foo/foobar');
$client->submitForm('Register', array( $client->submitForm('Register', array(
'username' => 'username', 'username' => 'new username',
'password' => 'password', 'password' => 'new password',
), 'POST'); ), 'PUT', array(
'HTTP_USER_AGENT' => 'Symfony User Agent',
'HTTPS' => true,
));
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms'); $this->assertEquals('https://www.example.com/foo', $client->getRequest()->getUri(), '->submitForm() submit forms');
$this->assertEquals('PUT', $client->getRequest()->getMethod(), '->submitForm() allows to change the method');
$this->assertEquals('new username', $client->getRequest()->getParameters()['username'], '->submitForm() allows to override the form values');
$this->assertEquals('new password', $client->getRequest()->getParameters()['password'], '->submitForm() allows to override the form values');
$this->assertEquals('Symfony User Agent', $client->getRequest()->getServer()['HTTP_USER_AGENT'], '->submitForm() allows to change the $_SERVER parameters');
$this->assertEquals(true, $client->getRequest()->getServer()['HTTPS'], '->submitForm() allows to change the $_SERVER parameters');
} }
public function testSubmitFormNotFound() public function testSubmitFormNotFound()