|
13 | 13 | use yii\base\ErrorException; |
14 | 14 | use yii\web\Application; |
15 | 15 | use Yii; |
| 16 | +use yii\web\ErrorHandlerRenderEvent; |
16 | 17 | use yii\web\NotFoundHttpException; |
17 | 18 | use yii\web\View; |
18 | 19 | use yiiunit\TestCase; |
@@ -124,6 +125,80 @@ public function testClearAssetFilesInErrorActionView(): void |
124 | 125 | $this->assertStringNotContainsString('<script', $out); |
125 | 126 | } |
126 | 127 |
|
| 128 | + public function testAfterRenderEventCanModifyOutput(): void |
| 129 | + { |
| 130 | + /** @var ErrorHandler $handler */ |
| 131 | + $handler = Yii::$app->getErrorHandler(); |
| 132 | + |
| 133 | + $exception = new Exception('Some Exception'); |
| 134 | + |
| 135 | + $actualException = null; |
| 136 | + |
| 137 | + $handler->on( |
| 138 | + ErrorHandler::EVENT_AFTER_RENDER, |
| 139 | + static function (ErrorHandlerRenderEvent $event) use (&$actualException): void { |
| 140 | + $actualException = $event->exception; |
| 141 | + $event->output .= "\n<!--after-render-->"; |
| 142 | + } |
| 143 | + ); |
| 144 | + |
| 145 | + ob_start(); // suppress response output |
| 146 | + $this->invokeMethod($handler, 'renderException', [$exception]); |
| 147 | + ob_get_clean(); |
| 148 | + |
| 149 | + $this->assertSame($exception, $actualException); |
| 150 | + $this->assertStringContainsString('<!--after-render-->', Yii::$app->response->data); |
| 151 | + } |
| 152 | + |
| 153 | + public function testAfterRenderEventCanModifyOutputInErrorActionView(): void |
| 154 | + { |
| 155 | + /** @var ErrorHandler $handler */ |
| 156 | + $handler = Yii::$app->getErrorHandler(); |
| 157 | + $handler->errorAction = 'test/error'; |
| 158 | + |
| 159 | + $exception = new NotFoundHttpException('Resource not found'); |
| 160 | + |
| 161 | + $actualException = null; |
| 162 | + |
| 163 | + $handler->on( |
| 164 | + ErrorHandler::EVENT_AFTER_RENDER, |
| 165 | + static function (ErrorHandlerRenderEvent $event) use (&$actualException): void { |
| 166 | + $actualException = $event->exception; |
| 167 | + $event->output .= "\n<!--after-render-error-action-->"; |
| 168 | + } |
| 169 | + ); |
| 170 | + |
| 171 | + ob_start(); // suppress response output |
| 172 | + $this->invokeMethod($handler, 'renderException', [$exception]); |
| 173 | + ob_get_clean(); |
| 174 | + |
| 175 | + $this->assertSame($exception, $actualException); |
| 176 | + $this->assertStringContainsString('<!--after-render-error-action-->', Yii::$app->response->data); |
| 177 | + } |
| 178 | + |
| 179 | + public function testAfterRenderEventCanModifyOutputForPhpErrors(): void |
| 180 | + { |
| 181 | + /** @var ErrorHandler $handler */ |
| 182 | + $handler = Yii::$app->getErrorHandler(); |
| 183 | + |
| 184 | + $exception = new ErrorException('PHP Warning', E_WARNING, E_WARNING, __FILE__, __LINE__); |
| 185 | + |
| 186 | + $handler->exception = $exception; |
| 187 | + |
| 188 | + $handler->on( |
| 189 | + ErrorHandler::EVENT_AFTER_RENDER, |
| 190 | + static function (ErrorHandlerRenderEvent $event): void { |
| 191 | + $event->output .= "\n<!--php-error-after-render-->"; |
| 192 | + } |
| 193 | + ); |
| 194 | + |
| 195 | + ob_start(); // suppress response output |
| 196 | + $this->invokeMethod($handler, 'renderException', [$exception]); |
| 197 | + ob_get_clean(); |
| 198 | + |
| 199 | + $this->assertStringContainsString('<!--php-error-after-render-->', Yii::$app->response->data); |
| 200 | + } |
| 201 | + |
127 | 202 | public function testRenderCallStackItem(): void |
128 | 203 | { |
129 | 204 | $handler = Yii::$app->getErrorHandler(); |
|
0 commit comments