Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Error ValidationException handler #27632

Merged
merged 5 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
driesvints marked this conversation as resolved.
Show resolved Hide resolved
->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