Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Respect JsonResponseHandler flags when false #442

Merged
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
4 changes: 2 additions & 2 deletions src/Container/WhoopsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ private function registerJsonHandler(Whoops $whoops, $config)

$handler = new JsonResponseHandler();

if (isset($config['json_exceptions']['show_trace'])) {
if (isset($config['json_exceptions']['show_trace']) && true === $config['json_exceptions']['show_trace']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can have here ! empty? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I think stricter is better :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! empty is functionally equivalent here, more succinct, and more in line with other similar checks against configuration flags we're doing elsewhere.

$handler->addTraceToOutput(true);
}

if (isset($config['json_exceptions']['ajax_only'])) {
if (isset($config['json_exceptions']['ajax_only']) && true === $config['json_exceptions']['ajax_only']) {
if (method_exists(\Whoops\Util\Misc::class, 'isAjaxRequest')) {
// Whoops 2.x
if (! \Whoops\Util\Misc::isAjaxRequest()) {
Expand Down
53 changes: 43 additions & 10 deletions test/Container/WhoopsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use ReflectionProperty;
use Traversable;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run as Whoops;
Expand Down Expand Up @@ -76,33 +77,65 @@ public function testWillInjectJsonResponseHandlerIfConfigurationExpectsIt()
}

/**
* @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt
* @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt
* @dataProvider provideConfig
*
* @param bool $showsTrace
* @param bool $isAjaxOnly
* @param bool $requestIsAjax
*/
public function testJsonResponseHandlerCanBeConfigured()
public function testJsonResponseHandlerCanBeConfigured($showsTrace, $isAjaxOnly, $requestIsAjax)
{
// Set for Whoops 2.x json handler detection
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
if ($requestIsAjax) {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
}

$config = [
'whoops' => [
'json_exceptions' => [
'display' => true,
'show_trace' => true,
'ajax_only' => true,
'show_trace' => $showsTrace,
'ajax_only' => $isAjaxOnly,
],
],
];

$this->injectServiceInContainer($this->container, 'config', $config);

$factory = $this->factory;
$whoops = $factory($this->container->reveal());
$handler = $whoops->popHandler();

// If ajax only, not ajax request and Whoops 2, it does not inject JsonResponseHandler
if ($isAjaxOnly
&& ! $requestIsAjax
&& method_exists(\Whoops\Util\Misc::class, 'isAjaxRequest')
) {
$this->assertInstanceOf(PrettyPageHandler::class, $handler);

$jsonHandler = $whoops->popHandler();
$this->assertInstanceOf(JsonResponseHandler::class, $jsonHandler);
$this->assertAttributeSame(true, 'returnFrames', $jsonHandler);
// Skip remaining assertions
return;
}

$this->assertAttributeSame($showsTrace, 'returnFrames', $handler);

if (method_exists($jsonHandler, 'onlyForAjaxRequests')) {
$this->assertAttributeSame(true, 'onlyForAjaxRequests', $jsonHandler);
if (method_exists($handler, 'onlyForAjaxRequests')) {
$this->assertAttributeSame($isAjaxOnly, 'onlyForAjaxRequests', $handler);
}
}

/**
* @return Traversable
*/
public function provideConfig()
{
yield 'Shows trace' => [true, true, true];
yield 'Does not show trace' => [false, true, true];

yield 'Ajax only, request is ajax' => [true, true, true];
yield 'Ajax only, request is not ajax' => [true, true, false];

yield 'Not ajax only' => [true, false, false];
}
}