FileFormFieldTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /**
  36. * @dataProvider getSetValueMethods
  37. */
  38. public function testSetValue($method)
  39. {
  40. $node = $this->createNode('input', '', array('type' => 'file'));
  41. $field = new FileFormField($node);
  42. $field->$method(null);
  43. $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), "->$method() clears the uploaded file if the value is null");
  44. $field->$method(__FILE__);
  45. $value = $field->getValue();
  46. $this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field");
  47. $this->assertEquals('', $value['type'], "->$method() sets the type of the file field");
  48. $this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field");
  49. $this->assertEquals(0, $value['error'], "->$method() sets the error of the file field");
  50. $this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field");
  51. }
  52. public function getSetValueMethods()
  53. {
  54. return array(
  55. array('setValue'),
  56. array('upload'),
  57. );
  58. }
  59. public function testSetErrorCode()
  60. {
  61. $node = $this->createNode('input', '', array('type' => 'file'));
  62. $field = new FileFormField($node);
  63. $field->setErrorCode(UPLOAD_ERR_FORM_SIZE);
  64. $value = $field->getValue();
  65. $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code');
  66. try {
  67. $field->setErrorCode('foobar');
  68. $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  69. } catch (\InvalidArgumentException $e) {
  70. $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  71. }
  72. }
  73. }