You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/vendor/symfony/http-foundation/Tests/FileBagTest.php

174 lines
4.7 KiB

10 months ago
<?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\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\FileBag;
/**
* FileBagTest.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class FileBagTest extends TestCase
{
public function testFileMustBeAnArrayOrUploadedFile()
{
8 months ago
$this->expectException('InvalidArgumentException');
new FileBag(['file' => 'foo']);
10 months ago
}
public function testShouldConvertsUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
8 months ago
$bag = new FileBag(['file' => [
10 months ago
'name' => basename($tmpFile),
'type' => 'text/plain',
'tmp_name' => $tmpFile,
'error' => 0,
'size' => 100,
8 months ago
]]);
10 months ago
$this->assertEquals($file, $bag->get('file'));
}
public function testShouldSetEmptyUploadedFilesToNull()
{
8 months ago
$bag = new FileBag(['file' => [
10 months ago
'name' => '',
'type' => '',
'tmp_name' => '',
8 months ago
'error' => \UPLOAD_ERR_NO_FILE,
10 months ago
'size' => 0,
8 months ago
]]);
10 months ago
$this->assertNull($bag->get('file'));
}
public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
{
8 months ago
$bag = new FileBag(['files' => [
'name' => [''],
'type' => [''],
'tmp_name' => [''],
'error' => [\UPLOAD_ERR_NO_FILE],
'size' => [0],
]]);
$this->assertSame([], $bag->get('files'));
10 months ago
}
public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
{
8 months ago
$bag = new FileBag(['files' => [
'name' => ['file1' => ''],
'type' => ['file1' => ''],
'tmp_name' => ['file1' => ''],
'error' => ['file1' => \UPLOAD_ERR_NO_FILE],
'size' => ['file1' => 0],
]]);
$this->assertSame(['file1' => null], $bag->get('files'));
10 months ago
}
public function testShouldConvertUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
8 months ago
$bag = new FileBag([
'child' => [
'name' => [
10 months ago
'file' => basename($tmpFile),
8 months ago
],
'type' => [
10 months ago
'file' => 'text/plain',
8 months ago
],
'tmp_name' => [
10 months ago
'file' => $tmpFile,
8 months ago
],
'error' => [
10 months ago
'file' => 0,
8 months ago
],
'size' => [
10 months ago
'file' => 100,
8 months ago
],
],
]);
10 months ago
$files = $bag->all();
$this->assertEquals($file, $files['child']['file']);
}
public function testShouldConvertNestedUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
8 months ago
$bag = new FileBag([
'child' => [
'name' => [
'sub' => ['file' => basename($tmpFile)],
],
'type' => [
'sub' => ['file' => 'text/plain'],
],
'tmp_name' => [
'sub' => ['file' => $tmpFile],
],
'error' => [
'sub' => ['file' => 0],
],
'size' => [
'sub' => ['file' => 100],
],
],
]);
10 months ago
$files = $bag->all();
$this->assertEquals($file, $files['child']['sub']['file']);
}
public function testShouldNotConvertNestedUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
8 months ago
$bag = new FileBag(['image' => ['file' => $file]]);
10 months ago
$files = $bag->all();
$this->assertEquals($file, $files['image']['file']);
}
protected function createTempFile()
{
return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
}
protected function setUp()
{
mkdir(sys_get_temp_dir().'/form_test', 0777, true);
}
protected function tearDown()
{
foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
8 months ago
@unlink($file);
10 months ago
}
8 months ago
@rmdir(sys_get_temp_dir().'/form_test');
10 months ago
}
}