From 730f422b04c8ece7427a2305a1fe1a408f76fb31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Lambe=CC=81?= Date: Fri, 29 Nov 2019 17:42:01 +0100 Subject: [PATCH] Fix #614. File ValidationException handler. --- src/Core/Exceptions/Handler.php | 18 ++++++---- tests/Core/ExceptionHandlerTest.php | 53 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/Core/Exceptions/Handler.php b/src/Core/Exceptions/Handler.php index bb4b42f6..c3f93451 100644 --- a/src/Core/Exceptions/Handler.php +++ b/src/Core/Exceptions/Handler.php @@ -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) @@ -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) @@ -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); } /** @@ -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) diff --git a/tests/Core/ExceptionHandlerTest.php b/tests/Core/ExceptionHandlerTest.php index e132c091..28a3aba7 100644 --- a/tests/Core/ExceptionHandlerTest.php +++ b/tests/Core/ExceptionHandlerTest.php @@ -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; @@ -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