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
*
* @return Crawler
* @param string $linkText The text of the link or the alt attribute of the clickable image
*/
public function clickLink($value)
public function clickLink(string $linkText): Crawler
{
$link = $this->getCrawler()->selectLink($value)->link();
return $this->click($link);
return $this->click($this->getCrawler()->selectLink($linkText)->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 array $values An array of form field values
* @param string $method The method for the form
*
* @return Crawler
* @param string $button The text content, id, value or name of the form <button> or <input type="submit">
* @param array $fieldValues Use this syntax: array('my_form[name]' => '...', 'my_form[email]' => '...')
* @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)
*/
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);
$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()
{
$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->submitForm('Register', array(
'username' => 'username',
'password' => 'password',
), 'POST');
'username' => 'new username',
'password' => 'new password',
), '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()