8000 Added new methods submitForm and clickLink to Client class by nowiko · Pull Request #27807 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Added new methods submitForm and clickLink to Client class #27807

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ public function click(Link $link)
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.
*
Expand All @@ -307,6 +321,23 @@ public function submit(Form $form, array $values = array()/*, array $serverParam
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.
*
Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,30 @@ public function testClick()
$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');
B134 } catch (\Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->clickLink() throws a \InvalidArgumentException if the link could not be found');
}
}

public function testClickForm()
{
$client = new TestClient();
Expand All @@ -344,6 +368,37 @@ public function testSubmit()
$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'));
Expand Down
0