This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/FileFieldTest.php

171 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form;
require_once __DIR__.'/TestCase.php';
use Symfony\Component\Form\FileField;
use Symfony\Component\HttpFoundation\File\File;
class FileFieldTest extends TestCase
{
public static $tmpFiles = array();
protected static $tmpDir;
protected $field;
public static function setUpBeforeClass()
{
self::$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'symfony-test';
}
protected function setUp()
{
parent::setUp();
$this->field = $this->factory->getInstance('file', 'file');
}
protected function tearDown()
{
foreach (self::$tmpFiles as $key => $file) {
@unlink($file);
unset(self::$tmpFiles[$key]);
}
}
public function createTmpFile($path)
{
self::$tmpFiles[] = $path;
file_put_contents($path, 'foobar');
}
public function testSubmitUploadsNewFiles()
{
$tmpDir = self::$tmpDir;
$generatedToken = '';
$this->storage->expects($this->atLeastOnce())
->method('getTempDir')
->will($this->returnCallback(function ($token) use ($tmpDir, &$generatedToken) {
// A 6-digit token is generated by FileUploader and passed
// to getTempDir()
$generatedToken = $token;
return $tmpDir;
}));
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
->disableOriginalConstructor()
->getMock();
$file->expects($this->once())
->method('move')
->with($this->equalTo($tmpDir));
$file->expects($this->any())
->method('isValid')
->will($this->returnValue(true));
$file->expects($this->any())
->method('getName')
->will($this->returnValue('original_name.jpg'));
$file->expects($this->any())
->method('getPath')
->will($this->returnValue($tmpDir.'/original_name.jpg'));
2011-03-17 13:55:11 +00:00
$this->field->bind(array(
'file' => $file,
'token' => '',
'name' => '',
));
$this->assertRegExp('/^\d{6}$/', $generatedToken);
$this->assertEquals(array(
'file' => $file,
'token' => $generatedToken,
'name' => 'original_name.jpg',
), $this->field->getTransformedData());
$this->assertEquals($tmpDir.'/original_name.jpg', $this->field->getData());
}
public function testSubmitKeepsUploadedFilesOnErrors()
{
$tmpDir = self::$tmpDir;
$tmpPath = $tmpDir . DIRECTORY_SEPARATOR . 'original_name.jpg';
$this->createTmpFile($tmpPath);
$this->storage->expects($this->atLeastOnce())
->method('getTempDir')
->with($this->equalTo('123456'))
->will($this->returnValue($tmpDir));
2011-03-17 13:55:11 +00:00
$this->field->bind(array(
'file' => null,
'token' => '123456',
'name' => 'original_name.jpg',
));
$this->assertTrue(file_exists($tmpPath));
$file = new File($tmpPath);
$this->assertEquals(array(
'file' => $file,
'token' => '123456',
'name' => 'original_name.jpg',
), $this->field->getTransformedData());
$this->assertEquals($tmpPath, $this->field->getData());
}
public function testSubmitEmpty()
{
$this->storage->expects($this->never())
->method('getTempDir');
2011-03-17 13:55:11 +00:00
$this->field->bind(array(
'file' => '',
'token' => '',
'name' => '',
));
$this->assertEquals(array(
'file' => '',
'token' => '',
'name' => '',
), $this->field->getTransformedData());
$this->assertEquals(null, $this->field->getData());
}
public function testSubmitEmptyKeepsExistingFiles()
{
$tmpPath = self::$tmpDir . DIRECTORY_SEPARATOR . 'original_name.jpg';
$this->createTmpFile($tmpPath);
$file = new File($tmpPath);
$this->storage->expects($this->never())
->method('getTempDir');
$this->field->setData($tmpPath);
2011-03-17 13:55:11 +00:00
$this->field->bind(array(
'file' => '',
'token' => '',
'name' => '',
));
$this->assertEquals(array(
'file' => $file,
'token' => '',
'name' => '',
), $this->field->getTransformedData());
$this->assertEquals($tmpPath, $this->field->getData());
}
Merge branch 'event-manager' into experimental Conflicts: src/Symfony/Component/Form/BirthdayField.php src/Symfony/Component/Form/CheckboxField.php src/Symfony/Component/Form/ChoiceField.php src/Symfony/Component/Form/ChoiceList/TimeZoneChoiceList.php src/Symfony/Component/Form/CollectionField.php src/Symfony/Component/Form/DateField.php src/Symfony/Component/Form/DateTimeField.php src/Symfony/Component/Form/EntityChoiceField.php src/Symfony/Component/Form/Events.php src/Symfony/Component/Form/FieldFactory/FieldFactory.php src/Symfony/Component/Form/FieldFactory/FieldFactoryInterface.php src/Symfony/Component/Form/FileField.php src/Symfony/Component/Form/Filters.php src/Symfony/Component/Form/FormContext.php src/Symfony/Component/Form/FormContextInterface.php src/Symfony/Component/Form/FormFactoryInterface.php src/Symfony/Component/Form/HybridField.php src/Symfony/Component/Form/IntegerField.php src/Symfony/Component/Form/LanguageField.php src/Symfony/Component/Form/LocaleField.php src/Symfony/Component/Form/MoneyField.php src/Symfony/Component/Form/NumberField.php src/Symfony/Component/Form/PasswordField.php src/Symfony/Component/Form/PercentField.php src/Symfony/Component/Form/RepeatedField.php src/Symfony/Component/Form/TextField.php src/Symfony/Component/Form/TimeField.php src/Symfony/Component/Form/ToggleField.php src/Symfony/Component/Form/UrlField.php src/Symfony/Component/HttpFoundation/File/UploadedFile.php tests/Symfony/Tests/Component/Form/FileFieldTest.php tests/Symfony/Tests/Component/Form/FormContextTest.php tests/Symfony/Tests/Component/Form/HiddenFieldTest.php
2011-03-13 19:45:21 +00:00
}