Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8912754

Browse files
committed
feature #26475 [HttpFoundation] split FileException into specialized ones about upload handling (fmata)
This PR was merged into the 4.1-dev branch. Discussion ---------- [HttpFoundation] split FileException into specialized ones about upload handling | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26411 | License | MIT Commits ------- 9e586cc [HttpFoundation] split FileException into specialized ones about upload handling
2 parents 9fda6d3 + 9e586cc commit 8912754

10 files changed

+230
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines 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
-----
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class CannotWriteFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class ExtensionFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class FormSizeFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class IniSizeFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class NoFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class NoTmpDirFileException extends FileException
20+
{
21+
}
Lines 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 <[email protected]>
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 <[email protected]>
18+
*/
19+
class PartialFileException extends FileException
20+
{
21+
}

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

Lines 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

Lines 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)