4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Tests\Field;
14 use Symfony\Component\DomCrawler\Field\FileFormField;
16 class FileFormFieldTest extends FormFieldTestCase
18 public function testInitialize()
20 $node = $this->createNode('input', '', array('type' => 'file'));
21 $field = new FileFormField($node);
23 $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');
25 $node = $this->createNode('textarea', '');
27 $field = new FileFormField($node);
28 $this->fail('->initialize() throws a \LogicException if the node is not an input field');
29 } catch (\LogicException $e) {
30 $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input field');
33 $node = $this->createNode('input', '', array('type' => 'text'));
35 $field = new FileFormField($node);
36 $this->fail('->initialize() throws a \LogicException if the node is not a file input field');
37 } catch (\LogicException $e) {
38 $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not a file input field');
43 * @dataProvider getSetValueMethods
45 public function testSetValue($method)
47 $node = $this->createNode('input', '', array('type' => 'file'));
48 $field = new FileFormField($node);
50 $field->$method(null);
51 $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");
53 $field->$method(__FILE__);
54 $value = $field->getValue();
56 $this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field");
57 $this->assertEquals('', $value['type'], "->$method() sets the type of the file field");
58 $this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field");
59 $this->assertFileExists($value['tmp_name'], "->$method() creates a copy of the file at the tmp_name path");
60 $this->assertEquals(0, $value['error'], "->$method() sets the error of the file field");
61 $this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field");
63 $origInfo = pathinfo(__FILE__);
64 $tmpInfo = pathinfo($value['tmp_name']);
66 $origInfo['extension'],
67 $tmpInfo['extension'],
68 "->$method() keeps the same file extension in the tmp_name copy"
71 $field->$method(__DIR__.'/../Fixtures/no-extension');
72 $value = $field->getValue();
74 $this->assertArrayNotHasKey(
76 pathinfo($value['tmp_name']),
77 "->$method() does not add a file extension in the tmp_name copy"
81 public function getSetValueMethods()
89 public function testSetErrorCode()
91 $node = $this->createNode('input', '', array('type' => 'file'));
92 $field = new FileFormField($node);
94 $field->setErrorCode(UPLOAD_ERR_FORM_SIZE);
95 $value = $field->getValue();
96 $this->assertEquals(UPLOAD_ERR_FORM_SIZE, $value['error'], '->setErrorCode() sets the file input field error code');
99 $field->setErrorCode('foobar');
100 $this->fail('->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
101 } catch (\InvalidArgumentException $e) {
102 $this->assertTrue(true, '->setErrorCode() throws a \InvalidArgumentException if the error code is not valid');
106 public function testSetRawFilePath()
108 $node = $this->createNode('input', '', array('type' => 'file'));
109 $field = new FileFormField($node);
110 $field->setFilePath(__FILE__);
112 $this->assertEquals(__FILE__, $field->getValue());