Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9e586cc

Browse filesBrowse files
committed
[HttpFoundation] split FileException into specialized ones about upload handling
1 parent ca53d3e commit 9e586cc
Copy full SHA for 9e586cc

10 files changed

+230
-0
lines changed

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ CHANGELOG
1212
`*` and `*/*` default values (if they are present in the Accept HTTP header)
1313
when looking for items.
1414
* deprecated `Request::getSession()` when no session has been set. Use `Request::hasSession()` instead.
15+
* added `CannotWriteFileException`, `ExtensionFileException`, `FormSizeFileException`,
16+
`IniSizeFileException`, `NoFileException`, `NoTmpDirFileException`, `PartialFileException` to
17+
handle failed `UploadedFile`.
1518

1619
4.0.0
1720
-----
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_CANT_WRITE error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class CannotWriteFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_EXTENSION error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class ExtensionFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_FORM_SIZE error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class FormSizeFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_INI_SIZE error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class IniSizeFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_NO_FILE error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class NoFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_NO_TMP_DIR error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class NoTmpDirFileException extends FileException
20+
{
21+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
12+
namespace Symfony\Component\HttpFoundation\File\Exception;
13+
14+
/**
15+
* Thrown when an UPLOAD_ERR_PARTIAL error occurred with UploadedFile.
16+
*
17+
* @author Florent Mata <florentmata@gmail.com>
18+
*/
19+
class PartialFileException extends FileException
20+
{
21+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/File/UploadedFile.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111

1212
namespace Symfony\Component\HttpFoundation\File;
1313

14+
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
15+
use Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException;
1416
use Symfony\Component\HttpFoundation\File\Exception\FileException;
1517
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
18+
use Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
19+
use Symfony\Component\HttpFoundation\File\Exception\IniSizeFileException;
20+
use Symfony\Component\HttpFoundation\File\Exception\NoFileException;
21+
use Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException;
22+
use Symfony\Component\HttpFoundation\File\Exception\PartialFileException;
1623
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
1724

1825
/**
@@ -210,6 +217,23 @@ public function move($directory, $name = null)
210217
return $target;
211218
}
212219

220+
switch ($this->error) {
221+
case UPLOAD_ERR_INI_SIZE:
222+
throw new IniSizeFileException($this->getErrorMessage());
223+
case UPLOAD_ERR_FORM_SIZE:
224+
throw new FormSizeFileException($this->getErrorMessage());
225+
case UPLOAD_ERR_PARTIAL:
226+
throw new PartialFileException($this->getErrorMessage());
227+
case UPLOAD_ERR_NO_FILE:
228+
throw new NoFileException($this->getErrorMessage());
229+
case UPLOAD_ERR_CANT_WRITE:
230+
throw new CannotWriteFileException($this->getErrorMessage());
231+
case UPLOAD_ERR_NO_TMP_DIR:
232+
throw new NoTmpDirFileException($this->getErrorMessage());
233+
case UPLOAD_ERR_EXTENSION:
234+
throw new ExtensionFileException($this->getErrorMessage());
235+
}
236+
213237
throw new FileException($this->getErrorMessage());
214238
}
215239

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\File;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
16+
use Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException;
17+
use Symfony\Component\HttpFoundation\File\Exception\FileException;
18+
use Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
19+
use Symfony\Component\HttpFoundation\File\Exception\IniSizeFileException;
20+
use Symfony\Component\HttpFoundation\File\Exception\NoFileException;
21+
use Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException;
22+
use Symfony\Component\HttpFoundation\File\Exception\PartialFileException;
1523
use Symfony\Component\HttpFoundation\File\UploadedFile;
1624

1725
class UploadedFileTest extends TestCase
@@ -137,6 +145,54 @@ public function testMoveLocalFileIsNotAllowed()
137145
$movedFile = $file->move(__DIR__.'/Fixtures/directory');
138146
}
139147

148+
public function failedUploadedFile()
149+
{
150+
foreach (array(UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_EXTENSION, -1) as $error) {
151+
yield array(new UploadedFile(
152+
__DIR__.'/Fixtures/test.gif',
153+
'original.gif',
154+
'image/gif',
155+
$error
156+
));
157+
}
158+
}
159+
160+
/**
161+
* @dataProvider failedUploadedFile
162+
*/
163+
public function testMoveFailed(UploadedFile $file)
164+
{
165+
switch ($file->getError()) {
166+
case UPLOAD_ERR_INI_SIZE:
167+
$exceptionClass = IniSizeFileException::class;
168+
break;
169+
case UPLOAD_ERR_FORM_SIZE:
170+
$exceptionClass = FormSizeFileException::class;
171+
break;
172+
case UPLOAD_ERR_PARTIAL:
173+
$exceptionClass = PartialFileException::class;
174+
break;
175+
case UPLOAD_ERR_NO_FILE:
176+
$exceptionClass = NoFileException::class;
177+
break;
178+
case UPLOAD_ERR_CANT_WRITE:
179+
$exceptionClass = CannotWriteFileException::class;
180+
break;
181+
case UPLOAD_ERR_NO_TMP_DIR:
182+
$exceptionClass = NoTmpDirFileException::class;
183+
break;
184+
case UPLOAD_ERR_EXTENSION:
185+
$exceptionClass = ExtensionFileException::class;
186+
break;
187+
default:
188+
$exceptionClass = FileException::class;
189+
}
190+
191+
$this->expectException($exceptionClass);
192+
193+
$file->move(__DIR__.'/Fixtures/directory');
194+
}
195+
140196
public function testMoveLocalFileIsAllowedInTestMode()
141197
{
142198
$path = __DIR__.'/Fixtures/test.copy.gif';

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.