FileFormFieldTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\DomCrawler\Field;
  11. require_once __DIR__.'/FormFieldTestCase.php';
  12. use Symfony\Component\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. $field = new FileFormField($node);
  23. $this->fail('->initialize() throws a \LogicException if the node is not an input field');
  24. } catch (\LogicException $e) {
  25. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input field');
  26. }
  27. $node = $this->createNode('input', '', array('type' => 'text'));
  28. try {
  29. $field = new FileFormField($node);
  30. $this->fail('->initialize() throws a \LogicException if the node is not a file input field');
  31. } catch (\LogicException $e) {
  32. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not a file input field');
  33. }
  34. }
  35. public function testSetValue()
  36. {
  37. $node = $this->createNode('input', '', array('type' => 'file'));
  38. $field = new FileFormField($node);
  39. $field->setValue(null);
  40. $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');
  41. $field->setValue(__FILE__);
  42. $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');
  43. }
  44. public function testUpload()
  45. {
  46. $node = $this->createNode('input', '', array('type' => 'file'));
  47. $field = new FileFormField($node);
  48. $field->upload(null);
  49. $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), '->upload() clears the uploaded file if the value is null');
  50. $field->upload(__FILE__);
  51. $this->assertEquals(array('name' => 'FileFormFieldTest.php', 'type' => '', 'tmp_name' => __FILE__, 'error' => 0, 'size' => filesize(__FILE__)), $field->getValue(), '->upload() sets the value to the given file');
  52. }
  53. public function testSetErrorCode()
  54. {
  55. $node = $this->createNode('input', '', array('type' => 'file'));
  56. $field = new FileFormField($node);
  57. $field->setErrorCode(UPLOAD_ERR_FORM_SIZE);
  58. $value = $field->getValue();
  59. $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code');
  60. try {
  61. $field->setErrorCode('foobar');
  62. $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  63. } catch (\InvalidArgumentException $e) {
  64. $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  65. }
  66. }
  67. }