Skip to content

Commit 7a5aa92

Browse files
committed
bug symfony#23 Fixes symfony#9 Bridge error when no file is selected (ahundiak, Danielss89)
This PR was merged into the master branch. Discussion ---------- Fixes symfony#9 Bridge error when no file is selected Q A Bug fix? yes New feature? no BC breaks? no Deprecations? no Tests pass? yes Fixed tickets symfony#9 License MIT Doc PR Commits ------- a1a631a Update assert error message e5d62e6 Fixes based on code-review 101b608 Handles null file in createrequest bridge.
2 parents d16c63c + a1a631a commit 7a5aa92

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Factory/DiactorosFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ private function getFiles(array $uploadedFiles)
8686
$files = array();
8787

8888
foreach ($uploadedFiles as $key => $value) {
89+
if (null === $value) {
90+
$files[$key] = new DiactorosUploadedFile(null, 0, UPLOAD_ERR_NO_FILE, null, null);
91+
continue;
92+
}
8993
if ($value instanceof UploadedFile) {
9094
$files[$key] = $this->createUploadedFile($value);
9195
} else {
@@ -107,7 +111,7 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile)
107111
{
108112
return new DiactorosUploadedFile(
109113
$symfonyUploadedFile->getRealPath(),
110-
$symfonyUploadedFile->getSize(),
114+
$symfonyUploadedFile->getClientSize(),
111115
$symfonyUploadedFile->getError(),
112116
$symfonyUploadedFile->getClientOriginalName(),
113117
$symfonyUploadedFile->getClientMimeType()

Factory/HttpFoundationFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ private function getFiles(array $uploadedFiles)
8080
*/
8181
private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
8282
{
83-
$temporaryPath = $this->getTemporaryPath();
84-
$psrUploadedFile->moveTo($temporaryPath);
83+
$temporaryPath = '';
84+
$clientFileName = '';
85+
if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
86+
$temporaryPath = $this->getTemporaryPath();
87+
$psrUploadedFile->moveTo($temporaryPath);
8588

86-
$clientFileName = $psrUploadedFile->getClientFilename();
89+
$clientFileName = $psrUploadedFile->getClientFilename();
90+
}
8791

8892
return new UploadedFile(
8993
$temporaryPath,

Tests/Factory/DiactorosFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,33 @@ public function testCreateResponseFromBinaryFile()
172172

173173
$this->assertEquals('Binary', $psrResponse->getBody()->__toString());
174174
}
175+
176+
public function testUploadErrNoFile()
177+
{
178+
$file = new UploadedFile('', '', null, 0, UPLOAD_ERR_NO_FILE, true);
179+
$this->assertEquals(0, $file->getSize());
180+
$this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError());
181+
$this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error');
182+
$this->assertInternalType('integer', $file->getClientSize());
183+
184+
$request = new Request(array(), array(), array(), array(),
185+
array(
186+
'f1' => $file,
187+
'f2' => array('name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0),
188+
),
189+
array(
190+
'REQUEST_METHOD' => 'POST',
191+
'HTTP_HOST' => 'dunglas.fr',
192+
'HTTP_X_SYMFONY' => '2.8',
193+
),
194+
'Content'
195+
);
196+
197+
$psrRequest = $this->factory->createRequest($request);
198+
199+
$uploadedFiles = $psrRequest->getUploadedFiles();
200+
201+
$this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
202+
$this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
203+
}
175204
}

0 commit comments

Comments
 (0)