Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20070b7

Browse files
committed
feature #27807 Added new methods submitForm and clickLink to Client class (nowiko)
This PR was squashed before being merged into the 4.2-dev branch (closes #27807). Discussion ---------- Added new methods submitForm and clickLink to Client class | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Added new methods to BrowserKit/Client to simplify work with links and forms. Related to #27627 Commits ------- e098edd Added new methods submitForm and clickLink to Client class
2 parents 3f663fd + e098edd commit 20070b7

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ public function click(Link $link)
290290
return $this->request($link->getMethod(), $link->getUri());
291291
}
292292

293+
/**
294+
* Finds link by given text and then clicks on it.
295+
*
296+
* @param string $value The link text
297+
*
298+
* @return Crawler
299+
*/
300+
public function clickLink($value)
301+
{
302+
$link = $this->getCrawler()->selectLink($value)->link();
303+
304+
return $this->click($link);
305+
}
306+
293307
/**
294308
* Submits a form.
295309
*
@@ -307,6 +321,23 @@ public function submit(Form $form, array $values = array()/*, array $serverParam
307321
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), $serverParameters);
308322
}
309323

324+
/**
325+
* Finds a form by submit button text and then submits it.
326+
*
327+
* @param string $button The button text
328+
* @param array $values An array of form field values
329+
* @param string $method The method for the form
330+
*
331+
* @return Crawler
332+
*/
333+
public function submitForm($button, $values, $method)
334+
{
335+
$buttonNode = $this->getCrawler()->selectButton($button);
336+
$form = $buttonNode->form($values, $method);
337+
338+
return $this->submit($form);
339+
}
340+
310341
/**
311342
* Calls a URI.
312343
*

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,30 @@ public function testClick()
322322
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
323323
}
324324

325+
public function testClickLink()
326+
{
327+
$client = new TestClient();
328+
$client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
329+
$client->request('GET', 'http://www.example.com/foo/foobar');
330+
$client->clickLink('foo');
331+
332+
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
333+
}
334+
335+
public function testClickLinkNotFound()
336+
{
337+
$client = new TestClient();
338+
$client->setNextResponse(new Response('<html><a href="/foo">foobar</a></html>'));
339+
$client->request('GET', 'http://www.example.com/foo/foobar');
340+
341+
try {
342+
$client->clickLink('foo');
343+
$this->fail('->clickLink() throws a \InvalidArgumentException if the link could not be found');
344+
} catch (\Exception $e) {
345+
$this->assertInstanceOf('InvalidArgumentException', $e, '->clickLink() throws a \InvalidArgumentException if the link could not be found');
346+
}
347+
}
348+
325349
public function testClickForm()
326350
{
327351
$client = new TestClient();
@@ -344,6 +368,37 @@ public function testSubmit()
344368
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
345369
}
346370

371+
public function testSubmitForm()
372+
{
373+
$client = new TestClient();
374+
$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>'));
375+
$client->request('GET', 'http://www.example.com/foo/foobar');
376+
377+
$client->submitForm('Register', array(
378+
'username' => 'username',
379+
'password' => 'password',
380+
), 'POST');
381+
382+
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
383+
}
384+
385+
public function testSubmitFormNotFound()
386+
{
387+
$client = new TestClient();
388+
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
389+
$client->request('GET', 'http://www.example.com/foo/foobar');
390+
391+
try {
392+
$client->submitForm('Register', array(
393+
'username' => 'username',
394+
'password' => 'password',
395+
), 'POST');
396+
$this->fail('->submitForm() throws a \InvalidArgumentException if the form could not be found');
397+
} catch (\Exception $e) {
398+
$this->assertInstanceOf('InvalidArgumentException', $e, '->submitForm() throws a \InvalidArgumentException if the form could not be found');
399+
}
400+
}
401+
347402
public function testSubmitPreserveAuth()
348403
{
349404
$client = new TestClient(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'bar'));

0 commit comments

Comments
 (0)