-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ajaxExceptionJson' of https://github.com/laurencei/fram…
…ework into laurencei-ajaxExceptionJson
- Loading branch information
Showing
2 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Foundation; | ||
|
||
use Exception; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
use Illuminate\Container\Container; | ||
use Illuminate\Config\Repository as Config; | ||
use Illuminate\Foundation\Exceptions\Handler; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class FoundationExceptionsHandlerTest extends TestCase | ||
{ | ||
protected $config; | ||
|
||
protected $container; | ||
|
||
protected $handler; | ||
|
||
protected $request; | ||
|
||
public function setUp() | ||
{ | ||
$this->config = m::mock(Config::class); | ||
|
||
$this->request = m::mock('StdClass'); | ||
|
||
$this->container = Container::setInstance(new Container); | ||
|
||
$this->container->singleton('config', function () { | ||
return $this->config; | ||
}); | ||
|
||
$this->container->singleton('Illuminate\Contracts\Routing\ResponseFactory', function () { | ||
return new \Illuminate\Routing\ResponseFactory( | ||
m::mock(\Illuminate\Contracts\View\Factory::class), | ||
m::mock(\Illuminate\Routing\Redirector::class) | ||
); | ||
}); | ||
|
||
$this->handler = new Handler($this->container); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testReturnsHtmlPageWithStackTraceWhenHtmlRequestAndDebugTrue() | ||
{ | ||
$this->config->shouldReceive('get')->with('app.debug', null)->twice()->andReturn(true); | ||
$this->request->shouldReceive('expectsJson')->once()->andReturn(false); | ||
|
||
$response = $this->handler->render($this->request, new Exception('My custom error message'))->getContent(); | ||
|
||
$this->assertContains('<!DOCTYPE html>', $response); | ||
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response); | ||
$this->assertContains('My custom error message', $response); | ||
$this->assertContains('::main()', $response); | ||
$this->assertNotContains('"message":', $response); | ||
} | ||
|
||
public function testReturnsJsonWithStackTraceWhenAjaxRequestAndDebugTrue() | ||
{ | ||
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true); | ||
$this->request->shouldReceive('expectsJson')->once()->andReturn(true); | ||
|
||
$response = $this->handler->render($this->request, new Exception('My custom error message'))->getContent(); | ||
|
||
$this->assertNotContains('<!DOCTYPE html>', $response); | ||
$this->assertContains('"message": "My custom error message"', $response); | ||
$this->assertContains('"file":', $response); | ||
$this->assertContains('"line":', $response); | ||
$this->assertContains('"trace":', $response); | ||
} | ||
|
||
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndExceptionMessageIsMasked() | ||
{ | ||
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(false); | ||
$this->request->shouldReceive('expectsJson')->once()->andReturn(true); | ||
|
||
$response = $this->handler->render($this->request, new Exception('This error message should not be visible'))->getContent(); | ||
|
||
$this->assertContains('"message": "Server Error"', $response); | ||
$this->assertNotContains('<!DOCTYPE html>', $response); | ||
$this->assertNotContains('This error message should not be visible', $response); | ||
$this->assertNotContains('"file":', $response); | ||
$this->assertNotContains('"line":', $response); | ||
$this->assertNotContains('"trace":', $response); | ||
} | ||
|
||
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndHttpExceptionErrorIsShown() | ||
{ | ||
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(false); | ||
$this->request->shouldReceive('expectsJson')->once()->andReturn(true); | ||
|
||
$response = $this->handler->render($this->request, new HttpException(403, 'My custom error message'))->getContent(); | ||
|
||
$this->assertContains('"message": "My custom error message"', $response); | ||
$this->assertNotContains('<!DOCTYPE html>', $response); | ||
$this->assertNotContains('"message": "Server Error"', $response); | ||
$this->assertNotContains('"file":', $response); | ||
$this->assertNotContains('"line":', $response); | ||
$this->assertNotContains('"trace":', $response); | ||
} | ||
} |