Skip to content

Commit 4f12d9b

Browse files
committed
fix(Collector): Improve file upload error handling
- Update property names in RequestFileCollector for consistency - Replace 'Error' with 'ErrorMessage' for clearer error reporting - Modify test cases to reflect updated file names in error handling - Correct JSON snapshot data for accurate test results
1 parent ab58bbe commit 4f12d9b

File tree

4 files changed

+36
-172
lines changed

4 files changed

+36
-172
lines changed

src/Collectors/RequestFileCollector.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function __construct(private Request $request) {}
2424
/**
2525
* @noinspection CallableParameterUseCaseInTypeContextInspection
2626
*
27+
* @see tests/FeatureTest.php
28+
*
2729
* ```json
2830
* {
2931
* "name": "images.jpeg",
@@ -41,10 +43,12 @@ public function collect(): array
4143

4244
array_walk_recursive($files, static function (UploadedFile &$uploadedFile): void {
4345
$uploadedFile = [
44-
'Name' => $uploadedFile->getClientOriginalName(),
45-
'Type' => $uploadedFile->getMimeType(),
46-
'Error' => $uploadedFile->getError(),
47-
'Size' => Utils::humanBytes($uploadedFile->getSize()),
46+
'name' => $uploadedFile->getClientOriginalName(),
47+
'type' => $uploadedFile->getMimeType(),
48+
'tmp_name' => $uploadedFile->getPathname(),
49+
// 'error' => $uploadedFile->getError(),
50+
'error' => $uploadedFile->getErrorMessage(),
51+
'size' => Utils::humanBytes($uploadedFile->getSize()),
4852
];
4953
});
5054

tests/FeatureTest.php

+12-42
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,31 @@
2121
use Illuminate\Http\UploadedFile;
2222

2323
it('can proactive report exception', function (): void {
24-
$jpgFile = UploadedFile::fake()->image('jpg.jpg');
25-
$pngFile = UploadedFile::fake()->image('png.png');
2624
$this
2725
->post('proactive-report-exception?foo=bar', [
2826
'bar' => 'baz',
2927
'password' => 'password',
30-
'image' => new UploadedFile(
31-
path: $jpgFile->path(),
32-
originalName: $jpgFile->getBasename(),
33-
mimeType: $jpgFile->getMimeType(),
34-
error: null,
35-
test: true
36-
),
37-
'images_list' => [
38-
new UploadedFile(
39-
path: $jpgFile->path(),
40-
originalName: $jpgFile->getBasename(),
41-
mimeType: $jpgFile->getMimeType(),
42-
error: \UPLOAD_ERR_INI_SIZE,
43-
test: true
44-
),
45-
new UploadedFile(
46-
path: $pngFile->path(),
47-
originalName: $pngFile->getBasename(),
48-
mimeType: $pngFile->getMimeType(),
49-
error: \UPLOAD_ERR_PARTIAL,
50-
test: true
51-
),
52-
],
53-
'images_map' => [
54-
'jpg' => new UploadedFile(
55-
path: $jpgFile->path(),
56-
originalName: $jpgFile->getBasename(),
57-
mimeType: $jpgFile->getMimeType(),
58-
error: \UPLOAD_ERR_CANT_WRITE,
59-
test: true
60-
),
61-
'png' => new UploadedFile(
62-
path: $pngFile->path(),
63-
originalName: $pngFile->getBasename(),
64-
mimeType: $pngFile->getMimeType(),
65-
error: \UPLOAD_ERR_EXTENSION,
66-
test: true
67-
),
68-
],
6928
])
7029
->assertOk();
7130
})->group(__DIR__, __FILE__);
7231

7332
it('can automatic report exception', function (): void {
33+
$jpgFile = UploadedFile::fake()->image($jpgName = 'image.jpg');
34+
$pngFile = UploadedFile::fake()->image($pngName = 'image.png');
35+
7436
$this
7537
->post('automatic-report-exception?foo=bar', [
7638
'bar' => 'baz',
7739
'password' => 'password',
78-
// 'file' => new UploadedFile(__FILE__, basename(__FILE__)),
40+
'image' => new UploadedFile($jpgFile->path(), $jpgName, $jpgFile->getMimeType(), null, true),
41+
'images_list' => [
42+
new UploadedFile($jpgFile->path(), $jpgName, $jpgFile->getMimeType(), \UPLOAD_ERR_INI_SIZE, true),
43+
new UploadedFile($pngFile->path(), $pngName, $pngFile->getMimeType(), \UPLOAD_ERR_PARTIAL, true),
44+
],
45+
'images_map' => [
46+
'jpg' => new UploadedFile($jpgFile->path(), $jpgName, $jpgFile->getMimeType(), \UPLOAD_ERR_CANT_WRITE, true),
47+
'png' => new UploadedFile($pngFile->path(), $pngName, $pngFile->getMimeType(), \UPLOAD_ERR_EXTENSION, true),
48+
],
7949
])
8050
->assertServerError();
8151
})->group(__DIR__, __FILE__);

tests/TestCase.php

+16-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
namespace Guanguans\LaravelExceptionNotifyTests;
1818

1919
use Guanguans\LaravelExceptionNotify\Channels\Channel;
20-
use Guanguans\LaravelExceptionNotify\Channels\DumpChannel;
2120
use Guanguans\LaravelExceptionNotify\Collectors\ApplicationCollector;
2221
use Guanguans\LaravelExceptionNotify\Collectors\ChoreCollector;
2322
use Guanguans\LaravelExceptionNotify\Collectors\ExceptionBasicCollector;
@@ -28,7 +27,6 @@
2827
use Guanguans\LaravelExceptionNotify\Collectors\RequestHeaderCollector;
2928
use Guanguans\LaravelExceptionNotify\Collectors\RequestPostCollector;
3029
use Guanguans\LaravelExceptionNotify\Collectors\RequestQueryCollector;
31-
use Guanguans\LaravelExceptionNotify\Events\ReportingEvent;
3230
use Guanguans\LaravelExceptionNotify\ExceptionNotifyServiceProvider;
3331
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
3432
use Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify;
@@ -47,7 +45,6 @@
4745
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
4846
use phpmock\phpunit\PHPMock;
4947
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
50-
use function Spatie\Snapshots\assertMatchesJsonSnapshot;
5148

5249
class TestCase extends \Orchestra\Testbench\TestCase
5350
{
@@ -146,28 +143,6 @@ protected function defineRoutes($router): void
146143
static fn () => tap(
147144
response('proactive-report-exception'),
148145
static function (): void {
149-
config()->set('exception-notify.channels.stack.channels', [
150-
'dump',
151-
'log',
152-
'mail',
153-
'bark',
154-
'chanify',
155-
'dingTalk',
156-
'discord',
157-
'lark',
158-
'ntfy',
159-
'pushDeer',
160-
'slack',
161-
'telegram',
162-
'weWork',
163-
]);
164-
165-
ExceptionNotify::reporting(static function (ReportingEvent $reportingEvent): void {
166-
if ($reportingEvent->channelContract instanceof DumpChannel) {
167-
assertMatchesJsonSnapshot($reportingEvent->content);
168-
}
169-
});
170-
171146
ExceptionNotify::report(new RuntimeException('What happened?'));
172147
}
173148
)
@@ -176,6 +151,22 @@ static function (): void {
176151
$router->any(
177152
'automatic-report-exception',
178153
static fn () => tap(response('automatic-report-exception'), static function (): void {
154+
config()->set('exception-notify.channels.stack.channels', [
155+
'dump',
156+
'log',
157+
'mail',
158+
'bark',
159+
'chanify',
160+
'dingTalk',
161+
'discord',
162+
'lark',
163+
'ntfy',
164+
'pushDeer',
165+
'slack',
166+
'telegram',
167+
'weWork',
168+
]);
169+
179170
throw new RuntimeException('What happened?');
180171
})
181172
);

tests/__snapshots__/FeatureTest__it_can_proactive_report_exception__1.json

-101
This file was deleted.

0 commit comments

Comments
 (0)