FormTest.php 14 KB

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