Skip to content

Commit

Permalink
Merge pull request #27632 from fbrissi/bugfix/error_except_files
Browse files Browse the repository at this point in the history
[5.7] Error ValidationException handler
  • Loading branch information
taylorotwell authored Feb 25, 2019
2 parents 99cc3f7 + 604dfa7 commit ab0da3b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function convertValidationExceptionToResponse(ValidationException $e,
protected function invalid($request, ValidationException $exception)
{
return redirect($exception->redirectTo ?? url()->previous())
->withInput($request->except($this->dontFlash))
->withInput(array_except($request->input(), $this->dontFlash))
->withErrors($exception->errors(), $exception->errorBag);
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
use Exception;
use Mockery as m;
use RuntimeException;
use Illuminate\Http\Request;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Illuminate\Routing\Redirector;
use Illuminate\Support\MessageBag;
use Illuminate\Container\Container;
use Illuminate\Validation\Validator;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\View\Factory;
use Illuminate\Routing\ResponseFactory;
use Illuminate\Config\Repository as Config;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
Expand Down Expand Up @@ -130,6 +136,49 @@ 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 = m::mock(Redirector::class);

$redirector->shouldReceive('to')->once()
->andReturn($responser = m::mock(RedirectResponse::class));

$responser->shouldReceive('withInput')->once()->with(m::on(
function ($argument) use (&$argumentActual) {
$argumentActual = $argument;

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

$responser->shouldReceive('withErrors')->once()
->andReturn($responser);

return $redirector;
});

$file = m::mock(UploadedFile::class);
$file->shouldReceive('getPathname')->andReturn('photo.jpg');
$file->shouldReceive('getClientOriginalName')->andReturn('photo.jpg');
$file->shouldReceive('getClientMimeType')->andReturn(null);
$file->shouldReceive('getError')->andReturn(null);

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

$validator = m::mock(Validator::class);
$validator->shouldReceive('errors')->andReturn(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 ab0da3b

Please sign in to comment.