diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 16a5b4e3b6..08324eecb3 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -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. * diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 020fa07526..6f6f2d5aba 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -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('foo')); + $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('foobar')); + $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('
')); + $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('
')); + $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'));