Skip to content

Commit

Permalink
add test coverage for error handler convertExceptionToString and conv…
Browse files Browse the repository at this point in the history
…ertExceptionToArray methods (#20051)
  • Loading branch information
salehhashemi1992 committed Oct 27, 2023
1 parent bd452da commit af1858c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/framework/web/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,49 @@ public function testCorrectResponseCodeInErrorView()
Exception: yii\web\NotFoundHttpException', $out);
}

public function testFormatRaw()
{
Yii::$app->response->format = yii\web\Response::FORMAT_RAW;

/** @var ErrorHandler $handler */
$handler = Yii::$app->getErrorHandler();

ob_start(); // suppress response output
$this->invokeMethod($handler, 'renderException', [new \Exception('Test Exception')]);
$out = ob_get_clean();

$this->assertcontains('Test Exception', $out);

$this->assertTrue(is_string(Yii::$app->response->data));
$this->assertcontains("Exception 'Exception' with message 'Test Exception'", Yii::$app->response->data);
}

public function testFormatXml()
{
Yii::$app->response->format = yii\web\Response::FORMAT_XML;

/** @var ErrorHandler $handler */
$handler = Yii::$app->getErrorHandler();

ob_start(); // suppress response output
$this->invokeMethod($handler, 'renderException', [new \Exception('Test Exception')]);
$out = ob_get_clean();

$this->assertcontains('Test Exception', $out);

$outArray = Yii::$app->response->data;

$this->assertTrue(is_array(Yii::$app->response->data));

$this->assertEquals('Exception', $outArray['name']);
$this->assertEquals('Test Exception', $outArray['message']);
$this->assertArrayHasKey('code', $outArray);
$this->assertEquals('Exception', $outArray['type']);
$this->assertContains('ErrorHandlerTest.php', $outArray['file']);
$this->assertArrayHasKey('stack-trace', $outArray);
$this->assertArrayHasKey('line', $outArray);
}

public function testClearAssetFilesInErrorView()
{
Yii::$app->getView()->registerJsFile('somefile.js');
Expand Down

0 comments on commit af1858c

Please sign in to comment.