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
*
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* 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 {
2010-05-06 12:25:53 +01:00
$form = new Form ( $nodes -> item ( 0 ));
$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 {
2010-05-06 12:25:53 +01:00
$form = new Form ( $nodes -> item ( 1 ));
$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 {
2010-05-06 12:25:53 +01:00
$form = new Form ( $nodes -> item ( 0 ));
$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 (
'does not take into account disabled input fields' ,
' < input type = " text " name = " foo " value = " foo " disabled = " disabled " />
< input type = " submit " /> ' ,
array (),
),
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
),
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>' );
$form = new Form ( $dom -> getElementsByTagName ( 'input' ) -> item ( 0 ));
$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>' );
$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>' );
$this -> assertEquals ( 'post' , $form -> getMethod (), '->getMethod() returns the method attribute value of the form' );
$form = $this -> createForm ( '<form method="post"><input type="submit" /></form>' , 'put' );
$this -> assertEquals ( 'put' , $form -> getMethod (), '->getMethod() returns the method defined in the constructor if provided' );
}
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>' );
2010-06-25 18:13:01 +01: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
2010-06-25 18:13:01 +01: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' ;
2010-06-25 18:13:01 +01:00
$this -> pass ( '->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-06-25 18:13:01 +01: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' ];
2010-06-25 18:13:01 +01:00
$this -> pass ( '->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-06-25 18:13:01 +01: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
/**
* @ expectedException LogicException
*/
public function testOffsetUnset ()
{
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' );
unset ( $form [ 'foo' ]);
}
public function testOffsetIsset ()
{
$form = $this -> createForm ( '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>' );
$this -> assertTrue ( isset ( $form [ 'foo' ]), '->offsetIsset() return true if the field exists' );
$this -> assertFalse ( isset ( $form [ 'bar' ]), '->offsetIsset() return false if the field does not exist' );
}
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' );
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' );
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 );
$this -> assertEquals ( $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' );
$form = $this -> createForm ( $formHtml , null , 'https://login.foo.com' , '/bar/' );
$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' );
$form = $this -> createForm ( $formHtml , null , 'https://www.foo.com' , '/bar/' );
$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
{
2010-05-06 12:25:53 +01:00
$form = $this -> createForm ( '<form action="foo"><input type="submit" /></form>' , null , 'http://localhost' , '/foo/' );
$this -> assertEquals ( 'http://localhost/foo/foo' , $form -> getUri ( true ), '->getUri() returns absolute URIs' );
$form = $this -> createForm ( '<form action="/foo"><input type="submit" /></form>' , null , 'http://localhost' , '/foo/' );
$this -> assertEquals ( 'http://localhost/foo' , $form -> getUri ( true ), '->getUri() returns absolute URIs' );
$form = $this -> createForm ( '<form action="/foo"><input type="submit" /></form>' );
$this -> assertEquals ( '/foo' , $form -> getUri ( true ), '->getUri() returns absolute URIs only if the host has been defined in the constructor' );
$form = $this -> createForm ( '<form action="foo"><input type="submit" /></form>' );
$this -> assertEquals ( '/foo' , $form -> getUri ( true ), '->getUri() returns absolute URIs only if the host has been defined in the constructor' );
2010-04-15 13:41:42 +01:00
}
2010-09-02 19:47:15 +01:00
public function testGetUriWithOnlyQueryString ()
{
$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 ( true ), '->getUri() returns absolute URIs only if the host has been defined in the constructor' );
$this -> assertEquals ( '/foo/bar?get=param' , $form -> getUri ( false ), '->getUri() returns absolute URIs only if the host has been defined in the constructor' );
}
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-09-02 19:48:11 +01: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' ,
),
);
}
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
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
}
protected function createForm ( $form , $method = null , $host = null , $path = '/' )
{
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html>' . $form . '</html>' );
$nodes = $dom -> getElementsByTagName ( 'input' );
return new Form ( $nodes -> item ( $nodes -> length - 1 ), $method , $host , $path );
}
2010-04-15 13:41:42 +01:00
}