Skip to content

Commit af1858c

Browse files
add test coverage for error handler convertExceptionToString and convertExceptionToArray methods (#20051)
1 parent bd452da commit af1858c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/framework/web/ErrorHandlerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,49 @@ public function testCorrectResponseCodeInErrorView()
4242
Exception: yii\web\NotFoundHttpException', $out);
4343
}
4444

45+
public function testFormatRaw()
46+
{
47+
Yii::$app->response->format = yii\web\Response::FORMAT_RAW;
48+
49+
/** @var ErrorHandler $handler */
50+
$handler = Yii::$app->getErrorHandler();
51+
52+
ob_start(); // suppress response output
53+
$this->invokeMethod($handler, 'renderException', [new \Exception('Test Exception')]);
54+
$out = ob_get_clean();
55+
56+
$this->assertcontains('Test Exception', $out);
57+
58+
$this->assertTrue(is_string(Yii::$app->response->data));
59+
$this->assertcontains("Exception 'Exception' with message 'Test Exception'", Yii::$app->response->data);
60+
}
61+
62+
public function testFormatXml()
63+
{
64+
Yii::$app->response->format = yii\web\Response::FORMAT_XML;
65+
66+
/** @var ErrorHandler $handler */
67+
$handler = Yii::$app->getErrorHandler();
68+
69+
ob_start(); // suppress response output
70+
$this->invokeMethod($handler, 'renderException', [new \Exception('Test Exception')]);
71+
$out = ob_get_clean();
72+
73+
$this->assertcontains('Test Exception', $out);
74+
75+
$outArray = Yii::$app->response->data;
76+
77+
$this->assertTrue(is_array(Yii::$app->response->data));
78+
79+
$this->assertEquals('Exception', $outArray['name']);
80+
$this->assertEquals('Test Exception', $outArray['message']);
81+
$this->assertArrayHasKey('code', $outArray);
82+
$this->assertEquals('Exception', $outArray['type']);
83+
$this->assertContains('ErrorHandlerTest.php', $outArray['file']);
84+
$this->assertArrayHasKey('stack-trace', $outArray);
85+
$this->assertArrayHasKey('line', $outArray);
86+
}
87+
4588
public function testClearAssetFilesInErrorView()
4689
{
4790
Yii::$app->getView()->registerJsFile('somefile.js');

0 commit comments

Comments
 (0)