2010-04-15 13:41:42 +01:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-04-15 13:41:42 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-15 13:41:42 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\DomCrawler ;
2010-04-15 13:41:42 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DomCrawler\Form ;
2010-04-15 13:41:42 +01:00
class FormTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '
< html >
< input type = " submit " />
< form >
< input type = " foo " />
</ form >
< button />
</ html >
' );
$nodes = $dom -> getElementsByTagName ( 'input' );
2010-05-07 15:09:11 +01:00
try {
2011-04-23 15:25:21 +01:00
$form = new Form ( $nodes -> item ( 0 ), 'http://example.com' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '__construct() throws a \\LogicException if the node has no form ancestor' );
2010-05-07 15:09:11 +01:00
} catch ( \LogicException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '__construct() throws a \\LogicException if the node has no form ancestor' );
}
2010-05-07 15:09:11 +01:00
try {
2011-04-23 15:25:21 +01:00
$form = new Form ( $nodes -> item ( 1 ), 'http://example.com' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '__construct() throws a \\LogicException if the input type is not submit, button, or image' );
2010-05-07 15:09:11 +01:00
} catch ( \LogicException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '__construct() throws a \\LogicException if the input type is not submit, button, or image' );
}
$nodes = $dom -> getElementsByTagName ( 'button' );
2010-05-07 15:09:11 +01:00
try {
2011-04-23 15:25:21 +01:00
$form = new Form ( $nodes -> item ( 0 ), 'http://example.com' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '__construct() throws a \\LogicException if the input type is not submit, button, or image' );
2010-05-07 15:09:11 +01:00
} catch ( \LogicException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '__construct() throws a \\LogicException if the input type is not submit, button, or image' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* @ dataProvider provideInitializeValues
*/
public function testConstructor ( $message , $form , $values )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form>' . $form . '</form>' );
2010-11-23 08:42:19 +00:00
$this -> assertEquals ( $values , array_map ( function ( $field ) { return array ( get_class ( $field ), $field -> getValue ()); }, $form -> all ()), '->getDefaultValues() ' . $message );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function provideInitializeValues ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
return array (
array (
'does not take into account input fields without a name attribute' ,
' < input type = " text " value = " foo " />
< input type = " submit " /> ' ,
array (),
),
array (
2011-08-23 10:21:48 +01:00
'takes into account disabled input fields' ,
2010-05-06 12:25:53 +01:00
' < input type = " text " name = " foo " value = " foo " disabled = " disabled " />
< input type = " submit " /> ' ,
2011-08-23 10:21:48 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , 'foo' )),
2010-05-06 12:25:53 +01:00
),
array (
'appends the submitted button value' ,
'<input type="submit" name="bar" value="bar" />' ,
2010-08-20 22:09:55 +01:00
array ( 'bar' => array ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , 'bar' )),
2010-05-06 12:25:53 +01:00
),
2012-10-08 12:13:27 +01:00
array (
'appends the submitted button value for Button element' ,
'<button type="submit" name="bar" value="bar">Bar</button>' ,
array ( 'bar' => array ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , 'bar' )),
),
2010-05-06 12:25:53 +01:00
array (
'appends the submitted button value but not other submit buttons' ,
' < input type = " submit " name = " bar " value = " bar " />
< input type = " submit " name = " foobar " value = " foobar " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foobar' => array ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , 'foobar' )),
2010-05-06 12:25:53 +01:00
),
array (
'returns textareas' ,
' < textarea name = " foo " > foo </ textarea >
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\TextareaFormField' , 'foo' )),
2010-05-06 12:25:53 +01:00
),
array (
'returns inputs' ,
' < input type = " text " name = " foo " value = " foo " />
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , 'foo' )),
2010-05-06 12:25:53 +01:00
),
array (
'returns checkboxes' ,
' < input type = " checkbox " name = " foo " value = " foo " checked = " checked " />
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField' , 'foo' )),
2010-05-06 12:25:53 +01:00
),
array (
'returns not-checked checkboxes' ,
' < input type = " checkbox " name = " foo " value = " foo " />
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField' , false )),
2010-05-06 12:25:53 +01:00
),
array (
'returns radio buttons' ,
' < input type = " radio " name = " foo " value = " foo " />
< input type = " radio " name = " foo " value = " bar " checked = " bar " />
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField' , 'bar' )),
2010-05-06 12:25:53 +01:00
),
array (
'returns file inputs' ,
' < input type = " file " name = " foo " />
< input type = " submit " /> ' ,
2010-08-20 22:09:55 +01:00
array ( 'foo' => array ( 'Symfony\\Component\\DomCrawler\\Field\\FileFormField' , array ( 'name' => '' , 'type' => '' , 'tmp_name' => '' , 'error' => 4 , 'size' => 0 ))),
2010-05-06 12:25:53 +01:00
),
);
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetFormNode ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html><form><input type="submit" /></form></html>' );
2011-04-23 15:25:21 +01:00
$form = new Form ( $dom -> getElementsByTagName ( 'input' ) -> item ( 0 ), 'http://example.com' );
2010-05-06 12:25:53 +01:00
$this -> assertSame ( $dom -> getElementsByTagName ( 'form' ) -> item ( 0 ), $form -> getFormNode (), '->getFormNode() returns the form node associated with this form' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetMethod ()
{
$form = $this -> createForm ( '<form><input type="submit" /></form>' );
2011-05-25 14:38:02 +01:00
$this -> assertEquals ( 'GET' , $form -> getMethod (), '->getMethod() returns get if no method is defined' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form method="post"><input type="submit" /></form>' );
2011-05-25 14:38:02 +01:00
$this -> assertEquals ( 'POST' , $form -> getMethod (), '->getMethod() returns the method attribute value of the form' );
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form method="post"><input type="submit" /></form>' , 'put' );
2011-05-25 14:38:02 +01:00
$this -> assertEquals ( 'PUT' , $form -> getMethod (), '->getMethod() returns the method defined in the constructor if provided' );
2010-05-06 12:25:53 +01:00
}
public function testGetSetValue ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' );
2011-03-18 16:39:08 +00:00
$this -> assertEquals ( 'foo' , $form [ 'foo' ] -> getValue (), '->offsetGet() returns the value of a form field' );
2010-05-06 12:25:53 +01:00
2010-06-25 17:39:38 +01:00
$form [ 'foo' ] = 'bar' ;
2010-05-06 12:25:53 +01:00
2011-03-18 16:39:08 +00:00
$this -> assertEquals ( 'bar' , $form [ 'foo' ] -> getValue (), '->offsetSet() changes the value of a form field' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-06-25 17:39:38 +01:00
$form [ 'foobar' ] = 'bar' ;
2011-03-18 16:39:08 +00:00
$this -> fail ( '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2011-03-18 16:39:08 +00:00
$this -> assertTrue ( true , '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-06 12:25:53 +01:00
}
2010-05-07 15:09:11 +01:00
try {
2010-06-25 17:39:38 +01:00
$form [ 'foobar' ];
2011-03-18 16:39:08 +00:00
$this -> fail ( '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2011-03-18 16:39:08 +00:00
$this -> assertTrue ( true , '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
2010-06-27 18:44:04 +01:00
public function testOffsetUnset ()
{
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' );
unset ( $form [ 'foo' ]);
2011-03-18 16:39:08 +00:00
$this -> assertFalse ( isset ( $form [ 'foo' ]), '->offsetUnset() removes a field' );
2010-06-27 18:44:04 +01:00
}
2011-03-18 16:39:08 +00:00
public function testOffsetExists ()
2010-06-27 18:44:04 +01:00
{
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' );
2011-03-18 16:39:08 +00:00
$this -> assertTrue ( isset ( $form [ 'foo' ]), '->offsetExists() return true if the field exists' );
$this -> assertFalse ( isset ( $form [ 'bar' ]), '->offsetExists() return false if the field does not exist' );
2010-06-27 18:44:04 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetValues ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'foo[bar]' => 'foo' , 'bar' => 'bar' ), $form -> getValues (), '->getValues() returns all form field values' );
$form = $this -> createForm ( '<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'bar' => 'bar' ), $form -> getValues (), '->getValues() does not include not-checked checkboxes' );
$form = $this -> createForm ( '<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'bar' => 'bar' ), $form -> getValues (), '->getValues() does not include file input fields' );
2011-08-23 10:21:48 +01:00
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'bar' => 'bar' ), $form -> getValues (), '->getValues() does not include disabled fields' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testSetValues ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$form -> setValues ( array ( 'foo' => false , 'bar' => 'foo' ));
$this -> assertEquals ( array ( 'bar' => 'foo' ), $form -> getValues (), '->setValues() sets the values of fields' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetPhpValues ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'foo' => array ( 'bar' => 'foo' ), 'bar' => 'bar' ), $form -> getPhpValues (), '->getPhpValues() converts keys with [] to arrays' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetFiles ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array (), $form -> getFiles (), '->getFiles() returns an empty array if method is get' );
$form = $this -> createForm ( '<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'foo[bar]' => array ( 'name' => '' , 'type' => '' , 'tmp_name' => '' , 'error' => 4 , 'size' => 0 )), $form -> getFiles (), '->getFiles() only returns file fields' );
2011-08-23 10:21:48 +01:00
$form = $this -> createForm ( '<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>' );
$this -> assertEquals ( array (), $form -> getFiles (), '->getFiles() does not include disabled file fields' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetPhpFiles ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$this -> assertEquals ( array ( 'foo' => array ( 'bar' => array ( 'name' => '' , 'type' => '' , 'tmp_name' => '' , 'error' => 4 , 'size' => 0 ))), $form -> getPhpFiles (), '->getPhpFiles() converts keys with [] to arrays' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* @ dataProvider provideGetUriValues
*/
public function testGetUri ( $message , $form , $values , $uri )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( $form );
$form -> setValues ( $values );
2011-04-23 15:25:21 +01:00
$this -> assertEquals ( 'http://example.com' . $uri , $form -> getUri (), '->getUri() ' . $message );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
2010-07-06 16:39:11 +01:00
public function testGetUriActionAbsolute ()
{
$formHtml = '<form id="login_form" action="https://login.foo.com/login.php?login_attempt=1" method="POST"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ;
$form = $this -> createForm ( $formHtml );
$this -> assertEquals ( 'https://login.foo.com/login.php?login_attempt=1' , $form -> getUri (), '->getUri() returns absolute URIs set in the action form' );
$form = $this -> createForm ( $formHtml , null , 'https://login.foo.com' );
$this -> assertEquals ( 'https://login.foo.com/login.php?login_attempt=1' , $form -> getUri (), '->getUri() returns absolute URIs set in the action form' );
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( $formHtml , null , 'https://login.foo.com/bar/' );
2010-07-06 16:39:11 +01:00
$this -> assertEquals ( 'https://login.foo.com/login.php?login_attempt=1' , $form -> getUri (), '->getUri() returns absolute URIs set in the action form' );
// The action URI haven't the same domain Host have an another domain as Host
$form = $this -> createForm ( $formHtml , null , 'https://www.foo.com' );
$this -> assertEquals ( 'https://login.foo.com/login.php?login_attempt=1' , $form -> getUri (), '->getUri() returns absolute URIs set in the action form' );
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( $formHtml , null , 'https://www.foo.com/bar/' );
2010-07-06 16:39:11 +01:00
$this -> assertEquals ( 'https://login.foo.com/login.php?login_attempt=1' , $form -> getUri (), '->getUri() returns absolute URIs set in the action form' );
}
2010-05-06 12:25:53 +01:00
public function testGetUriAbsolute ()
2010-04-15 13:41:42 +01:00
{
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( '<form action="foo"><input type="submit" /></form>' , null , 'http://localhost/foo/' );
$this -> assertEquals ( 'http://localhost/foo/foo' , $form -> getUri (), '->getUri() returns absolute URIs' );
2010-05-06 12:25:53 +01:00
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( '<form action="/foo"><input type="submit" /></form>' , null , 'http://localhost/foo/' );
$this -> assertEquals ( 'http://localhost/foo' , $form -> getUri (), '->getUri() returns absolute URIs' );
2010-04-15 13:41:42 +01:00
}
2010-09-02 19:47:15 +01:00
public function testGetUriWithOnlyQueryString ()
{
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( '<form action="?get=param"><input type="submit" /></form>' , null , 'http://localhost/foo/bar' );
$this -> assertEquals ( 'http://localhost/foo/bar?get=param' , $form -> getUri (), '->getUri() returns absolute URIs only if the host has been defined in the constructor' );
2010-09-02 19:47:15 +01:00
}
2010-12-24 00:35:33 +00:00
public function testGetUriWithoutAction ()
{
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( '<form><input type="submit" /></form>' , null , 'http://localhost/foo/bar' );
$this -> assertEquals ( 'http://localhost/foo/bar' , $form -> getUri (), '->getUri() returns path if no action defined' );
2010-12-24 00:35:33 +00:00
}
2010-05-06 12:25:53 +01:00
public function provideGetUriValues ()
{
return array (
array (
'returns the URI of the form' ,
'<form action="/foo"><input type="submit" /></form>' ,
array (),
'/foo'
),
array (
'appends the form values if the method is get' ,
'<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array (),
'/foo?foo=foo'
),
array (
'appends the form values and merges the submitted values' ,
'<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array ( 'foo' => 'bar' ),
'/foo?foo=bar'
),
array (
'does not append values if the method is post' ,
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array (),
'/foo'
),
array (
'appends the form values to an existing query string' ,
'<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array (),
'/foo?bar=bar&foo=foo'
),
array (
'returns an empty URI if the action is empty' ,
'<form><input type="submit" /></form>' ,
array (),
2010-12-24 00:35:33 +00:00
'/' ,
2010-05-06 12:25:53 +01:00
),
array (
'appends the form values even if the action is empty' ,
'<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array (),
'/?foo=foo' ,
),
2011-02-17 17:49:03 +00:00
array (
'chooses the path if the action attribute value is a sharp (#)' ,
'<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>' ,
array (),
2011-04-08 16:52:43 +01:00
'/#' ,
2011-02-17 17:49:03 +00:00
),
2010-05-06 12:25:53 +01:00
);
}
2010-04-15 13:41:42 +01:00
2010-11-23 08:42:19 +00:00
public function testHas ()
2010-05-06 12:25:53 +01:00
{
$form = $this -> createForm ( '<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
2010-04-15 13:41:42 +01:00
2010-11-23 08:42:19 +00:00
$this -> assertFalse ( $form -> has ( 'foo' ), '->has() returns false if a field is not in the form' );
$this -> assertTrue ( $form -> has ( 'bar' ), '->has() returns true if a field is in the form' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2011-03-18 16:39:08 +00:00
public function testRemove ()
{
$form = $this -> createForm ( '<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
$form -> remove ( 'bar' );
$this -> assertFalse ( $form -> has ( 'bar' ), '->remove() removes a field' );
}
2010-11-23 08:42:19 +00:00
public function testGet ()
2010-05-06 12:25:53 +01:00
{
$form = $this -> createForm ( '<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
2010-11-23 08:42:19 +00:00
$this -> assertEquals ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , get_class ( $form -> get ( 'bar' )), '->get() returns the field object associated with the given name' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-11-23 08:42:19 +00:00
$form -> get ( 'foo' );
$this -> fail ( '->get() throws an \InvalidArgumentException if the field does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-11-23 08:42:19 +00:00
$this -> assertTrue ( true , '->get() throws an \InvalidArgumentException if the field does not exist' );
2010-05-06 12:25:53 +01:00
}
}
2010-04-15 13:41:42 +01:00
2010-11-23 08:42:19 +00:00
public function testAll ()
2010-05-06 12:25:53 +01:00
{
$form = $this -> createForm ( '<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
2010-11-23 08:42:19 +00:00
$fields = $form -> all ();
$this -> assertEquals ( 1 , count ( $fields ), '->all() return an array of form field objects' );
$this -> assertEquals ( 'Symfony\\Component\\DomCrawler\\Field\\InputFormField' , get_class ( $fields [ 'bar' ]), '->all() return an array of form field objects' );
2010-05-06 12:25:53 +01:00
}
2011-03-18 16:39:08 +00:00
public function testBase ()
2010-05-06 12:25:53 +01:00
{
$dom = new \DOMDocument ();
2011-04-08 16:52:43 +01:00
$dom -> loadHTML ( '<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>' );
2010-05-06 12:25:53 +01:00
$nodes = $dom -> getElementsByTagName ( 'input' );
2011-04-23 15:25:21 +01:00
$form = new Form ( $nodes -> item ( $nodes -> length - 1 ), 'http://www.foo.com/' );
2011-03-18 16:39:08 +00:00
$this -> assertEquals ( 'http://www.foo.com/foo.php' , $form -> getUri ());
2010-05-06 12:25:53 +01:00
}
2011-01-13 01:05:26 +00:00
2011-04-08 16:52:43 +01:00
public function testUriWithAnchor ()
{
2011-04-23 15:25:21 +01:00
$form = $this -> createForm ( '<form action="#foo"><input type="submit" /></form>' , null , 'http://example.com/id/123' );
2011-04-08 16:52:43 +01:00
$this -> assertEquals ( 'http://example.com/id/123#foo' , $form -> getUri ());
}
2011-04-23 15:25:21 +01:00
protected function createForm ( $form , $method = null , $currentUri = null )
2011-01-13 01:05:26 +00:00
{
$dom = new \DOMDocument ();
2011-03-18 16:39:08 +00:00
$dom -> loadHTML ( '<html>' . $form . '</html>' );
2011-01-13 01:05:26 +00:00
2012-10-08 12:13:27 +01:00
$xPath = new \DOMXPath ( $dom );
$nodes = $xPath -> query ( '//input | //button' );
2011-03-18 16:39:08 +00:00
2011-04-23 15:25:21 +01:00
if ( null === $currentUri ) {
$currentUri = 'http://example.com/' ;
}
return new Form ( $nodes -> item ( $nodes -> length - 1 ), $currentUri , $method );
2011-01-13 01:05:26 +00:00
}
2010-04-15 13:41:42 +01:00
}