Skip to content

Commit

Permalink
Fix #614. File ValidationException handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlambe committed Nov 29, 2019
1 parent db9d5c7 commit 730f422
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Core/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ protected function prepareException(Exception $e)
* @param Request $request
* @param AuthenticationException $e
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return Response
*/
protected function unauthenticated($request, AuthenticationException $e)
Expand All @@ -195,6 +197,8 @@ protected function unauthenticated($request, AuthenticationException $e)
* @param ValidationException $e
* @param Request $request
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return SymfonyResponse
*/
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
Expand All @@ -212,17 +216,15 @@ protected function convertValidationExceptionToResponse(ValidationException $e,
* Convert a validation exception into a response.
*
* @param Request $request
* @param ValidationException $e
* @param ValidationException $exception
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
protected function invalid($request, ValidationException $e)
protected function invalid($request, ValidationException $exception)
{
$url = $e->redirectTo ?? url()->previous();

return redirect($url)
->withInput($request->except($this->dontFlash))
->withErrors($e->errors(), $e->errorBag);
return redirect($exception->redirectTo ?? url()->previous())
->withInput(Arr::except($request->input(), $this->dontFlash))
->withErrors($exception->errors(), $exception->errorBag);
}

/**
Expand All @@ -231,6 +233,8 @@ protected function invalid($request, ValidationException $e)
* @param Request $request
* @param ValidationException $e
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return JsonResponse
*/
protected function invalidJson($request, ValidationException $e)
Expand Down
53 changes: 53 additions & 0 deletions tests/Core/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\ResponseFactory;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Themosis\Core\Exceptions\Handler;
Expand Down Expand Up @@ -179,6 +185,53 @@ public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndA
$this->assertNotContains('"line":', $response);
$this->assertNotContains('"trace":', $response);
}

public function testValidateFileMethod()
{
$argumentExpected = ['input' => 'My input value'];
$argumentActual = null;

$this->container->singleton('redirect', function () use (&$argumentActual) {
$redirector = $this->createMock(Redirector::class);

$redirector->expects($this->once())
->method('to')
->willReturn($responser = $this->createMock(RedirectResponse::class));

$responser->expects($this->once())
->method('withInput')
->with($this->callback(function ($argument) use (&$argumentActual) {
$argumentActual = $argument;

return true;
}))
->willReturn($responser);

$responser->expects($this->once())
->method('withErrors')
->willReturn($responser);

return $redirector;
});

$file = $this->createMock(UploadedFile::class);
$file->method('getPathname')->willReturn('photo.jpg');
$file->method('getClientOriginalName')->willReturn('photo.jpg');
$file->method('getClientMimeType')->willReturn(null);
$file->method('getError')->willReturn(null);

$request = Request::create('/', 'POST', $argumentExpected, [], ['photo' => $file]);

$validator = $this->createMock(Validator::class);
$validator->method('errors')->willReturn(new MessageBag(['error' => 'My custom validation exception']));

$validationException = new ValidationException($validator);
$validationException->redirectTo = '/';

$this->handler->render($request, $validationException);

$this->assertEquals($argumentExpected, $argumentActual);
}
}

class CustomException extends \Exception implements Responsable
Expand Down

0 comments on commit 730f422

Please sign in to comment.