FileFormFieldTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Component\DomCrawler\Tests\Field;
  11. use Symfony\Component\DomCrawler\Field\FileFormField;
  12. class FileFormFieldTest extends FormFieldTestCase
  13. {
  14. public function testInitialize()
  15. {
  16. $node = $this->createNode('input', '', ['type' => 'file']);
  17. $field = new FileFormField($node);
  18. $this->assertEquals(['name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0], $field->getValue(), '->initialize() sets the value of the field to no file uploaded');
  19. $node = $this->createNode('textarea', '');
  20. try {
  21. $field = new FileFormField($node);
  22. $this->fail('->initialize() throws a \LogicException if the node is not an input field');
  23. } catch (\LogicException $e) {
  24. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input field');
  25. }
  26. $node = $this->createNode('input', '', ['type' => 'text']);
  27. try {
  28. $field = new FileFormField($node);
  29. $this->fail('->initialize() throws a \LogicException if the node is not a file input field');
  30. } catch (\LogicException $e) {
  31. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not a file input field');
  32. }
  33. }
  34. /**
  35. * @dataProvider getSetValueMethods
  36. */
  37. public function testSetValue($method)
  38. {
  39. $node = $this->createNode('input', '', ['type' => 'file']);
  40. $field = new FileFormField($node);
  41. $field->$method(null);
  42. $this->assertEquals(['name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0], $field->getValue(), "->$method() clears the uploaded file if the value is null");
  43. $field->$method(__FILE__);
  44. $value = $field->getValue();
  45. $this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field");
  46. $this->assertEquals('', $value['type'], "->$method() sets the type of the file field");
  47. $this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field");
  48. $this->assertFileExists($value['tmp_name'], "->$method() creates a copy of the file at the tmp_name path");
  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. $origInfo = pathinfo(__FILE__);
  52. $tmpInfo = pathinfo($value['tmp_name']);
  53. $this->assertEquals(
  54. $origInfo['extension'],
  55. $tmpInfo['extension'],
  56. "->$method() keeps the same file extension in the tmp_name copy"
  57. );
  58. $field->$method(__DIR__.'/../Fixtures/no-extension');
  59. $value = $field->getValue();
  60. $this->assertArrayNotHasKey(
  61. 'extension',
  62. pathinfo($value['tmp_name']),
  63. "->$method() does not add a file extension in the tmp_name copy"
  64. );
  65. }
  66. public function getSetValueMethods()
  67. {
  68. return [
  69. ['setValue'],
  70. ['upload'],
  71. ];
  72. }
  73. public function testSetErrorCode()
  74. {
  75. $node = $this->createNode('input', '', ['type' => 'file']);
  76. $field = new FileFormField($node);
  77. $field->setErrorCode(UPLOAD_ERR_FORM_SIZE);
  78. $value = $field->getValue();
  79. $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code');
  80. try {
  81. $field->setErrorCode('foobar');
  82. $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  83. } catch (\InvalidArgumentException $e) {
  84. $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
  85. }
  86. }
  87. public function testSetRawFilePath()
  88. {
  89. $node = $this->createNode('input', '', ['type' => 'file']);
  90. $field = new FileFormField($node);
  91. $field->setFilePath(__FILE__);
  92. $this->assertEquals(__FILE__, $field->getValue());
  93. }
  94. }