Skip to content

Commit

Permalink
[DomCrawler] updated upload logic to better emulate a real upload by …
Browse files Browse the repository at this point in the history
…copying the source file to the temp directory
  • Loading branch information
kriswallsmith committed Mar 17, 2011
1 parent 60c1159 commit 6960925
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Component/DomCrawler/Field/FileFormField.php
Expand Up @@ -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);
}

/**
Expand Down
Expand Up @@ -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()
Expand Down

0 comments on commit 6960925

Please sign in to comment.