Skip to content

Commit 820186f

Browse files
Amrouche Hamzaxabbuh
authored andcommitted
[HttpFoundation] we should not pass size on FileBag
1 parent 332ad0a commit 820186f

File tree

9 files changed

+98
-49
lines changed

9 files changed

+98
-49
lines changed

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public function testSubmitPostOrPutRequest($method)
597597
{
598598
$path = tempnam(sys_get_temp_dir(), 'sf2');
599599
touch($path);
600-
600+
file_put_contents($path, 'zaza');
601601
$values = array(
602602
'author' => array(
603603
'name' => 'Bernhard',
@@ -630,7 +630,7 @@ public function testSubmitPostOrPutRequest($method)
630630

631631
$form->handleRequest($request);
632632

633-
$file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK);
633+
$file = new UploadedFile($path, 'upload.png', 'image/png', UPLOAD_ERR_OK);
634634

635635
$this->assertEquals('Bernhard', $form['name']->getData());
636636
$this->assertEquals($file, $form['image']->getData());
@@ -645,6 +645,7 @@ public function testSubmitPostOrPutRequestWithEmptyRootFormName($method)
645645
{
646646
$path = tempnam(sys_get_temp_dir(), 'sf2');
647647
touch($path);
648+
file_put_contents($path, 'zaza');
648649

649650
$values = array(
650651
'name' => 'Bernhard',
@@ -676,7 +677,7 @@ public function testSubmitPostOrPutRequestWithEmptyRootFormName($method)
676677

677678
$form->handleRequest($request);
678679

679-
$file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK);
680+
$file = new UploadedFile($path, 'upload.png', 'image/png', UPLOAD_ERR_OK);
680681

681682
$this->assertEquals('Bernhard', $form['name']->getData());
682683
$this->assertEquals($file, $form['image']->getData());
@@ -692,6 +693,7 @@ public function testSubmitPostOrPutRequestWithSingleChildForm($method)
692693
{
693694
$path = tempnam(sys_get_temp_dir(), 'sf2');
694695
touch($path);
696+
file_put_contents($path, 'zaza');
695697

696698
$files = array(
697699
'image' => array(
@@ -714,7 +716,7 @@ public function testSubmitPostOrPutRequestWithSingleChildForm($method)
714716

715717
$form->handleRequest($request);
716718

717-
$file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK);
719+
$file = new UploadedFile($path, 'upload.png', 'image/png', UPLOAD_ERR_OK);
718720

719721
$this->assertEquals($file, $form->getData());
720722

@@ -728,6 +730,7 @@ public function testSubmitPostOrPutRequestWithSingleChildFormUploadedFile($metho
728730
{
729731
$path = tempnam(sys_get_temp_dir(), 'sf2');
730732
touch($path);
733+
file_put_contents($path, 'zaza');
731734

732735
$values = array(
733736
'name' => 'Bernhard',

src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function requestHandlerProvider()
188188
private function createUploadedFileMock(RequestHandlerInterface $requestHandler, $path, $originalName)
189189
{
190190
if ($requestHandler instanceof HttpFoundationRequestHandler) {
191-
return new UploadedFile($path, $originalName, null, null, null, true);
191+
return new UploadedFile($path, $originalName, null, null, true);
192192
}
193193

194194
return array(

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class UploadedFile extends File
2727
private $test = false;
2828
private $originalName;
2929
private $mimeType;
30-
private $size;
3130
private $error;
3231

3332
/**
@@ -47,22 +46,24 @@ class UploadedFile extends File
4746
* @param string $path The full temporary path to the file
4847
* @param string $originalName The original file name of the uploaded file
4948
* @param string|null $mimeType The type of the file as provided by PHP; null defaults to application/octet-stream
50-
* @param int|null $size The file size provided by the uploader
5149
* @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
5250
* @param bool $test Whether the test mode is active
5351
* Local files are used in test mode hence the code should not enforce HTTP uploads
5452
*
5553
* @throws FileException If file_uploads is disabled
5654
* @throws FileNotFoundException If the file does not exist
5755
*/
58-
public function __construct(string $path, string $originalName, string $mimeType = null, int $size = null, int $error = null, bool $test = false)
56+
public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false)
5957
{
6058
$this->originalName = $this->getName($originalName);
6159
$this->mimeType = $mimeType ?: 'application/octet-stream';
62-
$this->size = $size;
63-
if (null !== $size) {
64-
@trigger_error('Passing a size in the constructor is deprecated since Symfony 4.1 and will be removed in 5.0. Use getSize() instead.', E_USER_DEPRECATED);
60+
61+
if (4 < func_num_args() ? !is_bool($test) : null !== $error && @filesize($path) === $error) {
62+
@trigger_error(sprintf('Passing a size as 4th argument to the constructor of "%s" is deprecated since Symfony 4.1 and will be unsupported in 5.0.', __CLASS__), E_USER_DEPRECATED);
63+
$error = $test;
64+
$test = 5 < func_num_args() ? func_get_arg(5) : false;
6565
}
66+
6667
$this->error = $error ?: UPLOAD_ERR_OK;
6768
$this->test = $test;
6869

@@ -152,7 +153,7 @@ public function getClientSize()
152153
{
153154
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1 and will be removed in 5.0. Use getSize() instead.', __METHOD__), E_USER_DEPRECATED);
154155

155-
return $this->size;
156+
return $this->getSize();
156157
}
157158

158159
/**

src/Symfony/Component/HttpFoundation/FileBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function convertFileInformation($file)
8484
if (UPLOAD_ERR_NO_FILE == $file['error']) {
8585
$file = null;
8686
} else {
87-
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
87+
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']);
8888
}
8989
} else {
9090
$file = array_map(array($this, 'convertFileInformation'), $file);

src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function testFileUploadsWithNoMimeType()
4040
__DIR__.'/Fixtures/test.gif',
4141
'original.gif',
4242
null,
43-
null,
4443
UPLOAD_ERR_OK
4544
);
4645

@@ -57,7 +56,6 @@ public function testFileUploadsWithUnknownMimeType()
5756
__DIR__.'/Fixtures/.unknownextension',
5857
'original.gif',
5958
null,
60-
null,
6159
UPLOAD_ERR_OK
6260
);
6361

@@ -70,7 +68,6 @@ public function testGuessClientExtension()
7068
__DIR__.'/Fixtures/test.gif',
7169
'original.gif',
7270
'image/gif',
73-
null,
7471
null
7572
);
7673

@@ -83,7 +80,6 @@ public function testGuessClientExtensionWithIncorrectMimeType()
8380
__DIR__.'/Fixtures/test.gif',
8481
'original.gif',
8582
'image/jpeg',
86-
null,
8783
null
8884
);
8985

@@ -96,7 +92,6 @@ public function testErrorIsOkByDefault()
9692
__DIR__.'/Fixtures/test.gif',
9793
'original.gif',
9894
'image/gif',
99-
null,
10095
null
10196
);
10297

@@ -109,7 +104,6 @@ public function testGetClientOriginalName()
109104
__DIR__.'/Fixtures/test.gif',
110105
'original.gif',
111106
'image/gif',
112-
null,
113107
null
114108
);
115109

@@ -122,7 +116,6 @@ public function testGetClientOriginalExtension()
122116
__DIR__.'/Fixtures/test.gif',
123117
'original.gif',
124118
'image/gif',
125-
null,
126119
null
127120
);
128121

@@ -138,7 +131,6 @@ public function testMoveLocalFileIsNotAllowed()
138131
__DIR__.'/Fixtures/test.gif',
139132
'original.gif',
140133
'image/gif',
141-
null,
142134
UPLOAD_ERR_OK
143135
);
144136

@@ -158,7 +150,6 @@ public function testMoveLocalFileIsAllowedInTestMode()
158150
$path,
159151
'original.gif',
160152
'image/gif',
161-
null,
162153
UPLOAD_ERR_OK,
163154
true
164155
);
@@ -177,9 +168,7 @@ public function testGetClientOriginalNameSanitizeFilename()
177168
$file = new UploadedFile(
178169
__DIR__.'/Fixtures/test.gif',
179170
'../../original.gif',
180-
'image/gif',
181-
null,
182-
null
171+
'image/gif'
183172
);
184173

185174
$this->assertEquals('original.gif', $file->getClientOriginalName());
@@ -190,9 +179,7 @@ public function testGetSize()
190179
$file = new UploadedFile(
191180
__DIR__.'/Fixtures/test.gif',
192181
'original.gif',
193-
'image/gif',
194-
null,
195-
null
182+
'image/gif'
196183
);
197184

198185
$this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
@@ -208,7 +195,7 @@ public function testGetSize()
208195

209196
/**
210197
* @group legacy
211-
* @expectedDeprecation Passing a size in the constructor is deprecated since Symfony 4.1 and will be removed in 5.0. Use getSize() instead.
198+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
212199
*/
213200
public function testConstructDeprecatedSize()
214201
{
@@ -217,18 +204,34 @@ public function testConstructDeprecatedSize()
217204
'original.gif',
218205
'image/gif',
219206
filesize(__DIR__.'/Fixtures/test.gif'),
220-
null
207+
UPLOAD_ERR_OK,
208+
false
221209
);
222210

223211
$this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
224212
}
225213

226-
public function testGetExtension()
214+
/**
215+
* @group legacy
216+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
217+
*/
218+
public function testConstructDeprecatedSizeWhenPassingOnlyThe4Needed()
227219
{
228220
$file = new UploadedFile(
229221
__DIR__.'/Fixtures/test.gif',
230222
'original.gif',
231-
null
223+
'image/gif',
224+
filesize(__DIR__.'/Fixtures/test.gif')
225+
);
226+
227+
$this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
228+
}
229+
230+
public function testGetExtension()
231+
{
232+
$file = new UploadedFile(
233+
__DIR__.'/Fixtures/test.gif',
234+
'original.gif'
232235
);
233236

234237
$this->assertEquals('gif', $file->getExtension());
@@ -240,7 +243,6 @@ public function testIsValid()
240243
__DIR__.'/Fixtures/test.gif',
241244
'original.gif',
242245
null,
243-
null,
244246
UPLOAD_ERR_OK,
245247
true
246248
);
@@ -257,7 +259,6 @@ public function testIsInvalidOnUploadError($error)
257259
__DIR__.'/Fixtures/test.gif',
258260
'original.gif',
259261
null,
260-
null,
261262
$error
262263
);
263264

@@ -281,7 +282,6 @@ public function testIsInvalidIfNotHttpUpload()
281282
__DIR__.'/Fixtures/test.gif',
282283
'original.gif',
283284
null,
284-
null,
285285
UPLOAD_ERR_OK
286286
);
287287

src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ public function testFileMustBeAnArrayOrUploadedFile()
3131
new FileBag(array('file' => 'foo'));
3232
}
3333

34+
/**
35+
* @group legacy
36+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
37+
*/
3438
public function testShouldConvertsUploadedFiles()
3539
{
3640
$tmpFile = $this->createTempFile();
37-
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0);
41+
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 0);
3842

3943
$bag = new FileBag(array('file' => array(
4044
'name' => basename($tmpFile),
@@ -60,6 +64,26 @@ public function testShouldSetEmptyUploadedFilesToNull()
6064
$this->assertNull($bag->get('file'));
6165
}
6266

67+
/**
68+
* @group legacy
69+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
70+
*/
71+
public function testShouldNotTriggerDeprecationWhenPassingSize()
72+
{
73+
$tmpFile = $this->createTempFile();
74+
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 0);
75+
76+
$bag = new FileBag(array('file' => array(
77+
'name' => basename($tmpFile),
78+
'type' => 'text/plain',
79+
'tmp_name' => $tmpFile,
80+
'error' => 0,
81+
'size' => 123456,
82+
)));
83+
84+
$this->assertEquals($file, $bag->get('file'));
85+
}
86+
6387
public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
6488
{
6589
$bag = new FileBag(array('files' => array(
@@ -86,10 +110,14 @@ public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
86110
$this->assertSame(array('file1' => null), $bag->get('files'));
87111
}
88112

113+
/**
114+
* @group legacy
115+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
116+
*/
89117
public function testShouldConvertUploadedFilesWithPhpBug()
90118
{
91119
$tmpFile = $this->createTempFile();
92-
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0);
120+
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 0);
93121

94122
$bag = new FileBag(array(
95123
'child' => array(
@@ -115,10 +143,14 @@ public function testShouldConvertUploadedFilesWithPhpBug()
115143
$this->assertEquals($file, $files['child']['file']);
116144
}
117145

146+
/**
147+
* @group legacy
148+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
149+
*/
118150
public function testShouldConvertNestedUploadedFilesWithPhpBug()
119151
{
120152
$tmpFile = $this->createTempFile();
121-
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0);
153+
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 0);
122154

123155
$bag = new FileBag(array(
124156
'child' => array(
@@ -144,10 +176,14 @@ public function testShouldConvertNestedUploadedFilesWithPhpBug()
144176
$this->assertEquals($file, $files['child']['sub']['file']);
145177
}
146178

179+
/**
180+
* @group legacy
181+
* @expectedDeprecation Passing a size as 4th argument to the constructor of "Symfony\Component\HttpFoundation\File\UploadedFile" is deprecated since Symfony 4.1 and will be unsupported in 5.0.
182+
*/
147183
public function testShouldNotConvertNestedUploadedFiles()
148184
{
149185
$tmpFile = $this->createTempFile();
150-
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0);
186+
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 0);
151187
$bag = new FileBag(array('image' => array('file' => $file)));
152188

153189
$files = $bag->all();

src/Symfony/Component/HttpKernel/Client.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ protected function filterFiles(array $files)
168168
'',
169169
$value->getClientOriginalName(),
170170
$value->getClientMimeType(),
171-
null,
172171
UPLOAD_ERR_INI_SIZE,
173172
true
174173
);
@@ -177,7 +176,6 @@ protected function filterFiles(array $files)
177176
$value->getPathname(),
178177
$value->getClientOriginalName(),
179178
$value->getClientMimeType(),
180-
null,
181179
$value->getError(),
182180
true
183181
);

0 commit comments

Comments
 (0)