Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit9e586cc

Browse files
committed
[HttpFoundation] split FileException into specialized ones about upload handling
1 parentca53d3e commit9e586cc

File tree

10 files changed

+230
-0
lines changed

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 <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+
namespaceSymfony\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 CannotWriteFileExceptionextends 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 <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+
namespaceSymfony\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 ExtensionFileExceptionextends 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 <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+
namespaceSymfony\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 FormSizeFileExceptionextends 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 <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+
namespaceSymfony\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 IniSizeFileExceptionextends 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 <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+
namespaceSymfony\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 NoFileExceptionextends 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 <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+
namespaceSymfony\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 NoTmpDirFileExceptionextends 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 <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+
namespaceSymfony\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 PartialFileExceptionextends 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
namespaceSymfony\Component\HttpFoundation\File;
1313

14+
useSymfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
15+
useSymfony\Component\HttpFoundation\File\Exception\ExtensionFileException;
1416
useSymfony\Component\HttpFoundation\File\Exception\FileException;
1517
useSymfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
18+
useSymfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
19+
useSymfony\Component\HttpFoundation\File\Exception\IniSizeFileException;
20+
useSymfony\Component\HttpFoundation\File\Exception\NoFileException;
21+
useSymfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException;
22+
useSymfony\Component\HttpFoundation\File\Exception\PartialFileException;
1623
useSymfony\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+
caseUPLOAD_ERR_INI_SIZE:
222+
thrownewIniSizeFileException($this->getErrorMessage());
223+
caseUPLOAD_ERR_FORM_SIZE:
224+
thrownewFormSizeFileException($this->getErrorMessage());
225+
caseUPLOAD_ERR_PARTIAL:
226+
thrownewPartialFileException($this->getErrorMessage());
227+
caseUPLOAD_ERR_NO_FILE:
228+
thrownewNoFileException($this->getErrorMessage());
229+
caseUPLOAD_ERR_CANT_WRITE:
230+
thrownewCannotWriteFileException($this->getErrorMessage());
231+
caseUPLOAD_ERR_NO_TMP_DIR:
232+
thrownewNoTmpDirFileException($this->getErrorMessage());
233+
caseUPLOAD_ERR_EXTENSION:
234+
thrownewExtensionFileException($this->getErrorMessage());
235+
}
236+
213237
thrownewFileException($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
namespaceSymfony\Component\HttpFoundation\Tests\File;
1313

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

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

148+
publicfunctionfailedUploadedFile()
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+
yieldarray(newUploadedFile(
152+
__DIR__.'/Fixtures/test.gif',
153+
'original.gif',
154+
'image/gif',
155+
$error
156+
));
157+
}
158+
}
159+
160+
/**
161+
* @dataProvider failedUploadedFile
162+
*/
163+
publicfunctiontestMoveFailed(UploadedFile$file)
164+
{
165+
switch ($file->getError()) {
166+
caseUPLOAD_ERR_INI_SIZE:
167+
$exceptionClass = IniSizeFileException::class;
168+
break;
169+
caseUPLOAD_ERR_FORM_SIZE:
170+
$exceptionClass = FormSizeFileException::class;
171+
break;
172+
caseUPLOAD_ERR_PARTIAL:
173+
$exceptionClass = PartialFileException::class;
174+
break;
175+
caseUPLOAD_ERR_NO_FILE:
176+
$exceptionClass = NoFileException::class;
177+
break;
178+
caseUPLOAD_ERR_CANT_WRITE:
179+
$exceptionClass = CannotWriteFileException::class;
180+
break;
181+
caseUPLOAD_ERR_NO_TMP_DIR:
182+
$exceptionClass = NoTmpDirFileException::class;
183+
break;
184+
caseUPLOAD_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
publicfunctiontestMoveLocalFileIsAllowedInTestMode()
141197
{
142198
$path =__DIR__.'/Fixtures/test.copy.gif';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp