From 69609257acbc13ffcaa3c359e17114ea46a19530 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Thu, 17 Mar 2011 16:06:47 -0700 Subject: [PATCH] [DomCrawler] updated upload logic to better emulate a real upload by copying the source file to the temp directory --- .../DomCrawler/Field/FileFormField.php | 10 +++++- .../DomCrawler/Field/FileFormFieldTest.php | 33 +++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Field/FileFormField.php b/src/Symfony/Component/DomCrawler/Field/FileFormField.php index 7d8a29a8ac72..7f1eac189177 100644 --- a/src/Symfony/Component/DomCrawler/Field/FileFormField.php +++ b/src/Symfony/Component/DomCrawler/Field/FileFormField.php @@ -55,13 +55,21 @@ public function setValue($value) if (null !== $value && is_readable($value)) { $error = UPLOAD_ERR_OK; $size = filesize($value); + $name = basename($value); + + // copy to a tmp location + $tmp = tempnam(sys_get_temp_dir(), 'upload'); + unlink($tmp); + copy($value, $tmp); + $value = $tmp; } else { $error = UPLOAD_ERR_NO_FILE; $size = 0; + $name = ''; $value = ''; } - $this->value = array('name' => basename($value), 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size); + $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size); } /** diff --git a/tests/Symfony/Tests/Component/DomCrawler/Field/FileFormFieldTest.php b/tests/Symfony/Tests/Component/DomCrawler/Field/FileFormFieldTest.php index 3cb4414e1b5b..0c50fc226325 100644 --- a/tests/Symfony/Tests/Component/DomCrawler/Field/FileFormFieldTest.php +++ b/tests/Symfony/Tests/Component/DomCrawler/Field/FileFormFieldTest.php @@ -41,28 +41,33 @@ public function testInitialize() } } - public function testSetValue() + /** + * @dataProvider getSetValueMethods + */ + public function testSetValue($method) { $node = $this->createNode('input', '', array('type' => 'file')); $field = new FileFormField($node); - $field->setValue(null); - $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'); + $field->$method(null); + $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"); - $field->setValue(__FILE__); - $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'); + $field->$method(__FILE__); + $value = $field->getValue(); + + $this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field"); + $this->assertEquals('', $value['type'], "->$method() sets the type of the file field"); + $this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field"); + $this->assertEquals(0, $value['error'], "->$method() sets the error of the file field"); + $this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field"); } - public function testUpload() + public function getSetValueMethods() { - $node = $this->createNode('input', '', array('type' => 'file')); - $field = new FileFormField($node); - - $field->upload(null); - $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'); - - $field->upload(__FILE__); - $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'); + return array( + array('setValue'), + array('upload'), + ); } public function testSetErrorCode()