|
12 | 12 | use PHPUnit_Framework_TestCase as TestCase; |
13 | 13 | use Prophecy\Prophecy\ObjectProphecy; |
14 | 14 | use ReflectionProperty; |
| 15 | +use Traversable; |
15 | 16 | use Whoops\Handler\JsonResponseHandler; |
16 | 17 | use Whoops\Handler\PrettyPageHandler; |
17 | 18 | use Whoops\Run as Whoops; |
@@ -76,33 +77,68 @@ public function testWillInjectJsonResponseHandlerIfConfigurationExpectsIt() |
76 | 77 | } |
77 | 78 |
|
78 | 79 | /** |
79 | | - * @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt |
| 80 | + * @depends testWillInjectJsonResponseHandlerIfConfigurationExpectsIt |
| 81 | + * @dataProvider provideConfig |
| 82 | + * |
| 83 | + * @param bool $showsTrace |
| 84 | + * @param bool $isAjaxOnly |
| 85 | + * @param bool $requestIsAjax |
80 | 86 | */ |
81 | | - public function testJsonResponseHandlerCanBeConfigured() |
| 87 | + public function testJsonResponseHandlerCanBeConfigured($showsTrace, $isAjaxOnly, $requestIsAjax) |
82 | 88 | { |
83 | 89 | // 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 | + } |
85 | 93 |
|
86 | 94 | $config = [ |
87 | 95 | 'whoops' => [ |
88 | 96 | 'json_exceptions' => [ |
89 | 97 | 'display' => true, |
90 | | - 'show_trace' => true, |
91 | | - 'ajax_only' => true, |
| 98 | + 'show_trace' => $showsTrace, |
| 99 | + 'ajax_only' => $isAjaxOnly, |
92 | 100 | ], |
93 | 101 | ], |
94 | 102 | ]; |
| 103 | + |
95 | 104 | $this->injectServiceInContainer($this->container, 'config', $config); |
96 | 105 |
|
97 | 106 | $factory = $this->factory; |
98 | 107 | $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); |
99 | 116 |
|
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); |
103 | 122 |
|
104 | | - if (method_exists($jsonHandler, 'onlyForAjaxRequests')) { |
105 | | - $this->assertAttributeSame(true, 'onlyForAjaxRequests', $jsonHandler); |
| 123 | + if (method_exists($handler, 'onlyForAjaxRequests')) { |
| 124 | + $this->assertAttributeSame($isAjaxOnly, 'onlyForAjaxRequests', $handler); |
106 | 125 | } |
107 | 126 | } |
| 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 | + } |
108 | 144 | } |
0 commit comments