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

Commit 36a0aa1

Browse files
committed
Merge branch 'hotfix/442'
Close #442
2 parents 036e0d0 + b95338f commit 36a0aa1

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ All notable changes to this project will be documented in this file, in reverse
101101

102102
### Fixed
103103

104-
- Nothing.
104+
- [#442](https://github.com/zendframework/zend-expressive/pull/442) fixes how
105+
the `WhoopsFactory` disables JSON output for whoops; previously, providing
106+
boolean `false` values for either of the configuration flags
107+
`json_exceptions.show_trace` or `json_exceptions.ajax_only` would result in
108+
enabling the settings; these flags are now correctly evaluated by the
109+
`WhoopsFactory`.
105110

106111
## 1.0.6 - 2017-01-09
107112

src/Container/WhoopsFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Interop\Container\ContainerInterface;
1111
use Whoops\Handler\JsonResponseHandler;
1212
use Whoops\Run as Whoops;
13+
use Whoops\Util\Misc as WhoopsUtil;
1314

1415
/**
1516
* Create and return an instance of the Whoops runner.
@@ -75,14 +76,15 @@ private function registerJsonHandler(Whoops $whoops, $config)
7576

7677
$handler = new JsonResponseHandler();
7778

78-
if (isset($config['json_exceptions']['show_trace'])) {
79+
if (! empty($config['json_exceptions']['show_trace'])) {
7980
$handler->addTraceToOutput(true);
8081
}
8182

82-
if (isset($config['json_exceptions']['ajax_only'])) {
83-
if (method_exists(\Whoops\Util\Misc::class, 'isAjaxRequest')) {
84-
// Whoops 2.x
85-
if (! \Whoops\Util\Misc::isAjaxRequest()) {
83+
if (! empty($config['json_exceptions']['ajax_only'])) {
84+
if (method_exists(WhoopsUtil::class, 'isAjaxRequest')) {
85+
// Whoops 2.x; don't push handler on stack unless we are in
86+
// an XHR request.
87+
if (! WhoopsUtil::isAjaxRequest()) {
8688
return;
8789
}
8890
} elseif (method_exists($handler, 'onlyForAjaxRequests')) {

test/Container/WhoopsFactoryTest.php

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPUnit_Framework_TestCase as TestCase;
1313
use Prophecy\Prophecy\ObjectProphecy;
1414
use ReflectionProperty;
15+
use Traversable;
1516
use Whoops\Handler\JsonResponseHandler;
1617
use Whoops\Handler\PrettyPageHandler;
1718
use Whoops\Run as Whoops;
@@ -76,33 +77,68 @@ public function testWillInjectJsonResponseHandlerIfConfigurationExpectsIt()
7677
}
7778

7879
/**
79-
* @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt
80+
* @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt
81+
* @dataProvider provideConfig
82+
*
83+
* @param bool $showsTrace
84+
* @param bool $isAjaxOnly
85+
* @param bool $requestIsAjax
8086
*/
81-
public function testJsonResponseHandlerCanBeConfigured()
87+
public function testJsonResponseHandlerCanBeConfigured($showsTrace, $isAjaxOnly, $requestIsAjax)
8288
{
8389
// Set for Whoops 2.x json handler detection
84-
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
90+
if ($requestIsAjax) {
91+
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
92+
}
8593

8694
$config = [
8795
'whoops' => [
8896
'json_exceptions' => [
8997
'display' => true,
90-
'show_trace' => true,
91-
'ajax_only' => true,
98+
'show_trace' => $showsTrace,
99+
'ajax_only' => $isAjaxOnly,
92100
],
93101
],
94102
];
103+
95104
$this->injectServiceInContainer($this->container, 'config', $config);
96105

97106
$factory = $this->factory;
98107
$whoops = $factory($this->container->reveal());
108+
$handler = $whoops->popHandler();
109+
110+
// If ajax only, not ajax request and Whoops 2, it does not inject JsonResponseHandler
111+
if ($isAjaxOnly
112+
&& ! $requestIsAjax
113+
&& method_exists(\Whoops\Util\Misc::class, 'isAjaxRequest')
114+
) {
115+
$this->assertInstanceOf(PrettyPageHandler::class, $handler);
99116

100-
$jsonHandler = $whoops->popHandler();
101-
$this->assertInstanceOf(JsonResponseHandler::class, $jsonHandler);
102-
$this->assertAttributeSame(true, 'returnFrames', $jsonHandler);
117+
// Skip remaining assertions
118+
return;
119+
}
120+
121+
$this->assertAttributeSame($showsTrace, 'returnFrames', $handler);
103122

104-
if (method_exists($jsonHandler, 'onlyForAjaxRequests')) {
105-
$this->assertAttributeSame(true, 'onlyForAjaxRequests', $jsonHandler);
123+
if (method_exists($handler, 'onlyForAjaxRequests')) {
124+
$this->assertAttributeSame($isAjaxOnly, 'onlyForAjaxRequests', $handler);
106125
}
107126
}
127+
128+
/**
129+
* @return Traversable
130+
*/
131+
public function provideConfig()
132+
{
133+
// @codingStandardsIgnoreStart
134+
// test case => showsTrace, isAjaxOnly, requestIsAjax
135+
yield 'Shows trace' => [true, true, true];
136+
yield 'Does not show trace' => [false, true, true];
137+
138+
yield 'Ajax only, request is ajax' => [true, true, true];
139+
yield 'Ajax only, request is not ajax' => [true, true, false];
140+
141+
yield 'Not ajax only' => [true, false, false];
142+
// @codingStandardsIgnoreEnd
143+
}
108144
}

0 commit comments

Comments
 (0)