FileFormFieldTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Field;
  11. require_once __DIR__.'/FormFieldTestCase.php';
  12. use Symfony\Components\DomCrawler\Field\FileFormField;
  13. class FileFormFieldTest extends FormFieldTestCase
  14. {
  15. public function testInitialize()
  16. {
  17. $node = $this->createNode('input', '', array('type' => 'file'));
  18. $field = new FileFormField($node);
  19. $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), '->initialize() sets the value of the field to no file uploaded');
  20. $node = $this->createNode('textarea', '');
  21. try
  22. {
  23. $field = new FileFormField($node);
  24. $this->fail('->initialize() throws a \LogicException if the node is not an input field');
  25. }
  26. catch (\LogicException $e)
  27. {
  28. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input field');
  29. }
  30. $node = $this->createNode('input', '', array('type' => 'text'));
  31. try
  32. {
  33. $field = new FileFormField($node);
  34. $this->fail('->initialize() throws a \LogicException if the node is not a file input field');
  35. }
  36. catch (\LogicException $e)
  37. {
  38. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not a file input field');
  39. }
  40. }
  41. public function testSetValue()
  42. {
  43. $node = $this->createNode('input', '', array('type' => 'file'));
  44. $field = new FileFormField($node);
  45. $field->setValue(null);
  46. $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), '->setValue() clears the uploaded file if the value is null');
  47. $field->setValue(__FILE__);
  48. $this->assertEquals(array('name' => 'FileFormFieldTest.php', 'type' => '', 'tmp_name' => __FILE__, 'error' => 0, 'size' => filesize(__FILE__)), $field->getValue(), '->setValue() sets the value to the given file');
  49. }
  50. public function testSetErrorCode()
  51. {
  52. $node = $this->createNode('input', '', array('type' => 'file'));
  53. $field = new FileFormField($node);
  54. $field->setErrorCode(UPLOAD_ERR_FORM_SIZE);
  55. $value = $field->getValue();
  56. $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code');
  57. try
  58. {
  59. $field->setErrorCode('foobar');
  60. $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  61. }
  62. catch (\InvalidArgumentException $e)
  63. {
  64. $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  65. }
  66. }
  67. }