Open Monograph Press  3.3.0
FileBagTest.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 use PHPUnit\Framework\TestCase;
17 
24 class FileBagTest extends TestCase
25 {
30  {
31  new FileBag(array('file' => 'foo'));
32  }
33 
35  {
36  $tmpFile = $this->createTempFile();
37  $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
38 
39  $bag = new FileBag(array('file' => array(
40  'name' => basename($tmpFile),
41  'type' => 'text/plain',
42  'tmp_name' => $tmpFile,
43  'error' => 0,
44  'size' => 100,
45  )));
46 
47  $this->assertEquals($file, $bag->get('file'));
48  }
49 
51  {
52  $bag = new FileBag(array('file' => array(
53  'name' => '',
54  'type' => '',
55  'tmp_name' => '',
56  'error' => UPLOAD_ERR_NO_FILE,
57  'size' => 0,
58  )));
59 
60  $this->assertNull($bag->get('file'));
61  }
62 
64  {
65  $tmpFile = $this->createTempFile();
66  $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
67 
68  $bag = new FileBag(array(
69  'child' => array(
70  'name' => array(
71  'file' => basename($tmpFile),
72  ),
73  'type' => array(
74  'file' => 'text/plain',
75  ),
76  'tmp_name' => array(
77  'file' => $tmpFile,
78  ),
79  'error' => array(
80  'file' => 0,
81  ),
82  'size' => array(
83  'file' => 100,
84  ),
85  ),
86  ));
87 
88  $files = $bag->all();
89  $this->assertEquals($file, $files['child']['file']);
90  }
91 
93  {
94  $tmpFile = $this->createTempFile();
95  $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
96 
97  $bag = new FileBag(array(
98  'child' => array(
99  'name' => array(
100  'sub' => array('file' => basename($tmpFile)),
101  ),
102  'type' => array(
103  'sub' => array('file' => 'text/plain'),
104  ),
105  'tmp_name' => array(
106  'sub' => array('file' => $tmpFile),
107  ),
108  'error' => array(
109  'sub' => array('file' => 0),
110  ),
111  'size' => array(
112  'sub' => array('file' => 100),
113  ),
114  ),
115  ));
116 
117  $files = $bag->all();
118  $this->assertEquals($file, $files['child']['sub']['file']);
119  }
120 
122  {
123  $tmpFile = $this->createTempFile();
124  $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
125  $bag = new FileBag(array('image' => array('file' => $file)));
126 
127  $files = $bag->all();
128  $this->assertEquals($file, $files['image']['file']);
129  }
130 
131  protected function createTempFile()
132  {
133  return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
134  }
135 
136  protected function setUp()
137  {
138  mkdir(sys_get_temp_dir().'/form_test', 0777, true);
139  }
140 
141  protected function tearDown()
142  {
143  foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
144  unlink($file);
145  }
146 
147  rmdir(sys_get_temp_dir().'/form_test');
148  }
149 }
Symfony\Component\HttpFoundation\Tests\FileBagTest\createTempFile
createTempFile()
Definition: FileBagTest.php:131
Symfony\Component\HttpFoundation\Tests\FileBagTest\testShouldConvertNestedUploadedFilesWithPhpBug
testShouldConvertNestedUploadedFilesWithPhpBug()
Definition: FileBagTest.php:92
Symfony\Component\HttpFoundation\Tests\FileBagTest\testShouldNotConvertNestedUploadedFiles
testShouldNotConvertNestedUploadedFiles()
Definition: FileBagTest.php:121
Symfony\Component\HttpFoundation\Tests\FileBagTest\testShouldSetEmptyUploadedFilesToNull
testShouldSetEmptyUploadedFilesToNull()
Definition: FileBagTest.php:50
Symfony\Component\HttpFoundation\Tests\FileBagTest
Definition: FileBagTest.php:24
Symfony\Component\HttpFoundation\Tests\FileBagTest\tearDown
tearDown()
Definition: FileBagTest.php:141
Symfony\Component\HttpFoundation\Tests\FileBagTest\testShouldConvertsUploadedFiles
testShouldConvertsUploadedFiles()
Definition: FileBagTest.php:34
Symfony\Component\HttpFoundation\Tests\FileBagTest\testFileMustBeAnArrayOrUploadedFile
testFileMustBeAnArrayOrUploadedFile()
Definition: FileBagTest.php:29
Symfony\Component\HttpFoundation\Tests\FileBagTest\setUp
setUp()
Definition: FileBagTest.php:136
Symfony\Component\HttpFoundation\FileBag
Definition: lib/vendor/symfony/http-foundation/FileBag.php:22
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\File\UploadedFile
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:25
Symfony\Component\HttpFoundation\Tests\FileBagTest\testShouldConvertUploadedFilesWithPhpBug
testShouldConvertUploadedFilesWithPhpBug()
Definition: FileBagTest.php:63