FormTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Components\DomCrawler;
  11. use Symfony\Components\DomCrawler\Form;
  12. class FormTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor()
  15. {
  16. $dom = new \DOMDocument();
  17. $dom->loadHTML('
  18. <html>
  19. <input type="submit" />
  20. <form>
  21. <input type="foo" />
  22. </form>
  23. <button />
  24. </html>
  25. ');
  26. $nodes = $dom->getElementsByTagName('input');
  27. try {
  28. $form = new Form($nodes->item(0));
  29. $this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
  30. } catch (\LogicException $e) {
  31. $this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
  32. }
  33. try {
  34. $form = new Form($nodes->item(1));
  35. $this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
  36. } catch (\LogicException $e) {
  37. $this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
  38. }
  39. $nodes = $dom->getElementsByTagName('button');
  40. try {
  41. $form = new Form($nodes->item(0));
  42. $this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
  43. } catch (\LogicException $e) {
  44. $this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
  45. }
  46. }
  47. /**
  48. * @dataProvider provideInitializeValues
  49. */
  50. public function testConstructor($message, $form, $values)
  51. {
  52. $form = $this->createForm('<form>'.$form.'</form>');
  53. $this->assertEquals($values, array_map(function ($field) { return array(get_class($field), $field->getValue()); }, $form->getFields()), '->getDefaultValues() '.$message);
  54. }
  55. public function provideInitializeValues()
  56. {
  57. return array(
  58. array(
  59. 'does not take into account input fields without a name attribute',
  60. '<input type="text" value="foo" />
  61. <input type="submit" />',
  62. array(),
  63. ),
  64. array(
  65. 'does not take into account disabled input fields',
  66. '<input type="text" name="foo" value="foo" disabled="disabled" />
  67. <input type="submit" />',
  68. array(),
  69. ),
  70. array(
  71. 'appends the submitted button value',
  72. '<input type="submit" name="bar" value="bar" />',
  73. array('bar' => array('Symfony\\Components\\DomCrawler\\Field\\InputFormField', 'bar')),
  74. ),
  75. array(
  76. 'appends the submitted button value but not other submit buttons',
  77. '<input type="submit" name="bar" value="bar" />
  78. <input type="submit" name="foobar" value="foobar" />',
  79. array('foobar' => array('Symfony\\Components\\DomCrawler\\Field\\InputFormField', 'foobar')),
  80. ),
  81. array(
  82. 'returns textareas',
  83. '<textarea name="foo">foo</textarea>
  84. <input type="submit" />',
  85. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\TextareaFormField', 'foo')),
  86. ),
  87. array(
  88. 'returns inputs',
  89. '<input type="text" name="foo" value="foo" />
  90. <input type="submit" />',
  91. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\InputFormField', 'foo')),
  92. ),
  93. array(
  94. 'returns checkboxes',
  95. '<input type="checkbox" name="foo" value="foo" checked="checked" />
  96. <input type="submit" />',
  97. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\ChoiceFormField', 'foo')),
  98. ),
  99. array(
  100. 'returns not-checked checkboxes',
  101. '<input type="checkbox" name="foo" value="foo" />
  102. <input type="submit" />',
  103. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\ChoiceFormField', false)),
  104. ),
  105. array(
  106. 'returns radio buttons',
  107. '<input type="radio" name="foo" value="foo" />
  108. <input type="radio" name="foo" value="bar" checked="bar" />
  109. <input type="submit" />',
  110. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\ChoiceFormField', 'bar')),
  111. ),
  112. array(
  113. 'returns file inputs',
  114. '<input type="file" name="foo" />
  115. <input type="submit" />',
  116. array('foo' => array('Symfony\\Components\\DomCrawler\\Field\\FileFormField', array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))),
  117. ),
  118. );
  119. }
  120. public function testGetFormNode()
  121. {
  122. $dom = new \DOMDocument();
  123. $dom->loadHTML('<html><form><input type="submit" /></form></html>');
  124. $form = new Form($dom->getElementsByTagName('input')->item(0));
  125. $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
  126. }
  127. public function testGetMethod()
  128. {
  129. $form = $this->createForm('<form><input type="submit" /></form>');
  130. $this->assertEquals('get', $form->getMethod(), '->getMethod() returns get if no method is defined');
  131. $form = $this->createForm('<form method="post"><input type="submit" /></form>');
  132. $this->assertEquals('post', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
  133. $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
  134. $this->assertEquals('put', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
  135. }
  136. public function testGetSetValue()
  137. {
  138. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  139. $this->assertEquals('foo', $form->getValue('foo'), '->getValue() returns the value of a form field');
  140. $ret = $form->setValue('foo', 'bar');
  141. $this->assertEquals($form, $ret, '->setValue() implements a fluent interface');
  142. $this->assertEquals('bar', $form->getValue('foo'), '->setValue() changes the value of a form field');
  143. try {
  144. $form->setValue('foobar', 'bar');
  145. $this->pass('->setValue() throws an \InvalidArgumentException exception if the field does not exist');
  146. } catch (\InvalidArgumentException $e) {
  147. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException exception if the field does not exist');
  148. }
  149. try {
  150. $form->getValue('foobar');
  151. $this->pass('->getValue() throws an \InvalidArgumentException exception if the field does not exist');
  152. } catch (\InvalidArgumentException $e) {
  153. $this->assertTrue(true, '->getValue() throws an \InvalidArgumentException exception if the field does not exist');
  154. }
  155. }
  156. public function testGetValues()
  157. {
  158. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  159. $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar'), $form->getValues(), '->getValues() returns all form field values');
  160. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  161. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
  162. $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  163. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
  164. }
  165. public function testSetValues()
  166. {
  167. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  168. $form->setValues(array('foo' => false, 'bar' => 'foo'));
  169. $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
  170. }
  171. public function testGetPhpValues()
  172. {
  173. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  174. $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
  175. }
  176. public function testGetFiles()
  177. {
  178. $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  179. $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
  180. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  181. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
  182. }
  183. public function testGetPhpFiles()
  184. {
  185. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  186. $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
  187. }
  188. /**
  189. * @dataProvider provideGetUriValues
  190. */
  191. public function testGetUri($message, $form, $values, $uri)
  192. {
  193. $form = $this->createForm($form);
  194. $form->setValues($values);
  195. $this->assertEquals($uri, $form->getUri(), '->getUri() '.$message);
  196. }
  197. public function testGetUriAbsolute()
  198. {
  199. $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost', '/foo/');
  200. $this->assertEquals('http://localhost/foo/foo', $form->getUri(true), '->getUri() returns absolute URIs');
  201. $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost', '/foo/');
  202. $this->assertEquals('http://localhost/foo', $form->getUri(true), '->getUri() returns absolute URIs');
  203. $form = $this->createForm('<form action="/foo"><input type="submit" /></form>');
  204. $this->assertEquals('/foo', $form->getUri(true), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
  205. $form = $this->createForm('<form action="foo"><input type="submit" /></form>');
  206. $this->assertEquals('/foo', $form->getUri(true), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
  207. }
  208. public function provideGetUriValues()
  209. {
  210. return array(
  211. array(
  212. 'returns the URI of the form',
  213. '<form action="/foo"><input type="submit" /></form>',
  214. array(),
  215. '/foo'
  216. ),
  217. array(
  218. 'appends the form values if the method is get',
  219. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  220. array(),
  221. '/foo?foo=foo'
  222. ),
  223. array(
  224. 'appends the form values and merges the submitted values',
  225. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  226. array('foo' => 'bar'),
  227. '/foo?foo=bar'
  228. ),
  229. array(
  230. 'does not append values if the method is post',
  231. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  232. array(),
  233. '/foo'
  234. ),
  235. array(
  236. 'appends the form values to an existing query string',
  237. '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  238. array(),
  239. '/foo?bar=bar&foo=foo'
  240. ),
  241. array(
  242. 'returns an empty URI if the action is empty',
  243. '<form><input type="submit" /></form>',
  244. array(),
  245. '',
  246. ),
  247. array(
  248. 'appends the form values even if the action is empty',
  249. '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  250. array(),
  251. '/?foo=foo',
  252. ),
  253. );
  254. }
  255. public function testHasField()
  256. {
  257. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  258. $this->assertFalse($form->hasField('foo'), '->hasField() returns false if a field is not in the form');
  259. $this->assertTrue($form->hasField('bar'), '->hasField() returns true if a field is in the form');
  260. }
  261. public function testGetField()
  262. {
  263. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  264. $this->assertEquals('Symfony\\Components\\DomCrawler\\Field\\InputFormField', get_class($form->getField('bar')), '->getField() returns the field object associated with the given name');
  265. try {
  266. $form->getField('foo');
  267. $this->fail('->getField() throws an \InvalidArgumentException if the field does not exist');
  268. } catch (\InvalidArgumentException $e) {
  269. $this->assertTrue(true, '->getField() throws an \InvalidArgumentException if the field does not exist');
  270. }
  271. }
  272. public function testGetFields()
  273. {
  274. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  275. $fields = $form->getFields();
  276. $this->assertEquals(1, count($fields), '->getFields() return an array of form field objects');
  277. $this->assertEquals('Symfony\\Components\\DomCrawler\\Field\\InputFormField', get_class($fields['bar']), '->getFields() return an array of form field objects');
  278. }
  279. protected function createForm($form, $method = null, $host = null, $path = '/')
  280. {
  281. $dom = new \DOMDocument();
  282. $dom->loadHTML('<html>'.$form.'</html>');
  283. $nodes = $dom->getElementsByTagName('input');
  284. return new Form($nodes->item($nodes->length - 1), $method, $host, $path);
  285. }
  286. }