Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit c41c464

Browse files
authored
Merge pull request #68 from joselfonseca/feature/improve-test-coverage
Improve testing coverage.
2 parents 9825b5c + 4e13115 commit c41c464

File tree

11 files changed

+43
-14
lines changed

11 files changed

+43
-14
lines changed

app/Exceptions/Handler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Exceptions;
44

5-
use Illuminate\Auth\AuthenticationException;
65
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
76
use Throwable;
87

app/Http/Controllers/Api/Assets/UploadFileController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ protected function validateMime($mime)
188188
*/
189189
protected function validateBodySize($contentLength, $content)
190190
{
191-
if ($contentLength > config('files.maxsize', 1000000)) {
192-
throw new BodyTooLargeException();
193-
}
194-
if (mb_strlen($content) > config('files.maxsize', 1000000)) {
191+
if ($contentLength > config('files.maxsize', 1000000) || mb_strlen($content) > config('files.maxsize', 1000000)) {
195192
throw new BodyTooLargeException();
196193
}
197194
}

app/Http/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Kernel extends HttpKernel
3737
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
3838
\App\Http\Middleware\VerifyCsrfToken::class,
3939
\Illuminate\Routing\Middleware\SubstituteBindings::class,
40-
]
40+
],
4141
];
4242

4343
/**

app/Http/Middleware/Authenticate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class Authenticate extends Middleware
88
{
9-
109
}

app/Http/Middleware/PreventRequestsDuringMaintenance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class PreventRequestsDuringMaintenance extends Middleware
1414
protected $except = [
1515
//
1616
];
17-
}
17+
}

app/Http/Middleware/RedirectIfAuthenticated.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
/**
1010
* @codeCoverageIgnore
1111
* Class RedirectIfAuthenticated
12-
* @package App\Http\Middleware
1312
*/
1413
class RedirectIfAuthenticated
1514
{
@@ -33,4 +32,4 @@ public function handle($request, Closure $next, ...$guards)
3332

3433
return $next($request);
3534
}
36-
}
35+
}

app/Providers/BroadcastServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class BroadcastServiceProvider extends ServiceProvider
99
{
1010
/**
11+
* @codeCoverageIgnore
1112
* Bootstrap any application services.
1213
*
1314
* @return void

app/Providers/ErrorHandlerServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Dingo\Api\Exception\ResourceException;
77
use Dingo\Api\Routing\Helpers;
88
use Illuminate\Auth\AuthenticationException;
9-
use Illuminate\Database\Eloquent\ModelNotFoundException;
109
use Illuminate\Support\ServiceProvider;
1110
use Illuminate\Validation\ValidationException;
1211

@@ -35,9 +34,6 @@ public function register()
3534
app('Dingo\Api\Exception\Handler')->register(function (AuthenticationException $exception) {
3635
return $this->response->errorUnauthorized('Unauthenticated.');
3736
});
38-
app('Dingo\Api\Exception\Handler')->register(function (ModelNotFoundException $exception) {
39-
return $this->response->errorNotFound('404 Not Found');
40-
});
4137
app('Dingo\Api\Exception\Handler')->register(function (BodyTooLargeException $exception) {
4238
return $this->response->error('The body is too large', 413);
4339
});

tests/Feature/Assets/UploadImageTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature\Assets;
44

5+
use Illuminate\Http\UploadedFile;
56
use Tests\TestCase;
67
use App\Entities\User;
78
use Laravel\Passport\Passport;
@@ -153,4 +154,21 @@ function test_it_validates_url()
153154
$this->assertEquals(422, $response->getStatusCode());
154155
$this->assertArrayHasKey('Content-Type', $jsonResponse['errors']);
155156
}
157+
158+
function test_it_validates_size_using_multipart_file()
159+
{
160+
Storage::fake();
161+
Passport::actingAs(
162+
factory(User::class)->create()
163+
);
164+
config()->set('files.maxsize', 10);
165+
$file = UploadedFile::fake()->image('avatar.jpg')->size(1000);
166+
$response = $this->post('api/assets', [
167+
'file' => $file
168+
]);
169+
$jsonResponse = json_decode($response->getContent(), true);
170+
$this->assertEquals(413, $response->getStatusCode());
171+
$this->assertArrayHasKey('message', $jsonResponse);
172+
$this->assertEquals('The body is too large', $jsonResponse['message']);
173+
}
156174
}

tests/Feature/PingTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ public function test_it_returns_ping()
2323
$response->assertStatus(200);
2424
$response->assertJson(['status' => 'ok']);
2525
}
26+
27+
/**
28+
*
29+
*/
30+
public function test_it_returns_404()
31+
{
32+
$response = $this->json('GET', 'api/non-existing-resource');
33+
$response->assertStatus(404);
34+
$response->assertJson(['message' => '404 Not Found', 'status_code' => 404]);
35+
}
2636
}

0 commit comments

Comments
 (0)