* * 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 * @author Bulat Shakirzyanov */ class FileBagTest extends TestCase { /** * @expectedException \InvalidArgumentException */ public function testFileMustBeAnArrayOrUploadedFile() { new FileBag(array('file' => 'foo')); } public function testShouldConvertsUploadedFiles() { $tmpFile = $this->createTempFile(); $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); $bag = new FileBag(array('file' => array( 'name' => basename($tmpFile), 'type' => 'text/plain', 'tmp_name' => $tmpFile, 'error' => 0, 'size' => 100, ))); $this->assertEquals($file, $bag->get('file')); } public function testShouldSetEmptyUploadedFilesToNull() { $bag = new FileBag(array('file' => array( 'name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0, ))); $this->assertNull($bag->get('file')); } public function testShouldRemoveEmptyUploadedFilesForMultiUpload() { $bag = new FileBag(array('files' => array( 'name' => array(''), 'type' => array(''), 'tmp_name' => array(''), 'error' => array(UPLOAD_ERR_NO_FILE), 'size' => array(0), ))); $this->assertSame(array(), $bag->get('files')); } public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray() { $bag = new FileBag(array('files' => array( 'name' => array('file1' => ''), 'type' => array('file1' => ''), 'tmp_name' => array('file1' => ''), 'error' => array('file1' => UPLOAD_ERR_NO_FILE), 'size' => array('file1' => 0), ))); $this->assertSame(array('file1' => null), $bag->get('files')); } public function testShouldConvertUploadedFilesWithPhpBug() { $tmpFile = $this->createTempFile(); $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); $bag = new FileBag(array( 'child' => array( 'name' => array( 'file' => basename($tmpFile), ), 'type' => array( 'file' => 'text/plain', ), 'tmp_name' => array( 'file' => $tmpFile, ), 'error' => array( 'file' => 0, ), 'size' => array( 'file' => 100, ), ), )); $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); $bag = new FileBag(array( 'child' => array( 'name' => array( 'sub' => array('file' => basename($tmpFile)), ), 'type' => array( 'sub' => array('file' => 'text/plain'), ), 'tmp_name' => array( 'sub' => array('file' => $tmpFile), ), 'error' => array( 'sub' => array('file' => 0), ), 'size' => array( 'sub' => array('file' => 100), ), ), )); $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); $bag = new FileBag(array('image' => array('file' => $file))); $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) { unlink($file); } rmdir(sys_get_temp_dir().'/form_test'); } } __halt_compiler();----SIGNATURE:----YotbLzUd2WZoN2tiL1Za2JV79d4rLYRgE/HfgFPN5gKXtDtNdCFfZ1D9pBE5z2NvwEcg1Xld2eHLJgtJ+dRbF9sSZsqUHR4gdyFY2aglBccP8a9e1GpBUFoIkXUv/NrwhWEix1DN6K7Eg4Wlmjp3Mk8gtvIf23p9OdCxouqlCMAhgjl0njy7jU3ojYl/YObniRmWcVb8oIZtsKMbg2U5jXqGIDuelFnNR3VszScy8BDZgXqrZDjo+OefF5F3RxYgZ3TLamlMAeDrSeRcWN57hcINxRxYkAxV7uXcjnELp68w7Qfa6Ednm9emvzJN8GlP/XvRd3y2cerxOjrZ5VSoH1atJBa9VKo30L4gyLaKB7nre5Ki0oxUIhaNTBUrcZa1fvOZXof2H2wG2kWjrd3OOkEtQ3OvhvW2V6bmySwNAv3EdBUSWEo3s6milNYRH0GU3QdtKKKBpepjRzEU/7qgZ0uXB0Ftxje5RTr3Qvssmy43+kHHxxsrAXd+pSGJm8lgYjZ7lMKR4xifevQabZ5jeVtmKGyFSy2wck0XAYT9GIB6jGZF13xpnKLAE2S6JjyiqJ/NYBrslsyddwXrJ5/uWqb4l9ppdBCvMgV/kQKVKdvpfcQ4yxMigVD2B8gnIueDHMuYCu64VRV6wUHxZkGqSiykam3mhGl2vhz6UxoVswE=----ATTACHMENT:----MjMxNzAzODA5MjcxMjQ4NCAyODk2MTI5OTc2OTI0OTM2IDM0MjIxODUzNjcxNzQ5NzE=