Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/http/StatelessApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use stdClass;
use Yii;
use yii\base\{InvalidConfigException, Security};
use yii\base\UserException;
use yii\di\NotInstantiableException;
use yii\helpers\Json;
use yii\i18n\{Formatter, I18N};
Expand All @@ -31,6 +32,7 @@
use function gc_status;
use function ini_get;
use function ini_set;
use function is_string;
use function memory_get_usage;
use function ob_get_level;
use function ob_start;
Expand Down Expand Up @@ -844,6 +846,53 @@ public function testReturnFalseFromCleanWhenMemoryUsageIsBelowThreshold(): void
ini_set('memory_limit', $originalLimit);
}

public function testReturnHtmlErrorResponseForUserException(): void
{
$request = FactoryHelper::createServerRequestCreator()->createFromGlobals();

$app = $this->statelessApplication(
[
'components' => [
'errorHandler' => [
'errorAction' => 'stub/error',
],
],
],
);

$app->handle($request);

$errorHandler = $app->errorHandler;
$errorHandler->discardExistingOutput = false;

$exception = new UserException('User-friendly error message.');

$response = $errorHandler->handleException($exception);
$data = is_string($response->data) ? $response->data : '';

self::assertSame(
200,
$response->getStatusCode(),
"Response 'status code' should be '200' when handling a 'UserException' with a configured 'errorAction' " .
"that executes successfully in 'ErrorHandler', confirming proper error action delegation.",
);
self::assertSame(
Response::FORMAT_HTML,
$response->format,
"Response 'format' should be 'html' for 'UserException' handled by 'ErrorHandler'.",
);
self::assertNotEmpty(
$data,
"Response 'data' should not be empty when 'ErrorHandler' handles a 'UserException', ensuring error " .
'details are present.',
);
self::assertStringContainsString(
'User-friendly error message.',
$data,
"Response 'data' should contain the exception message when 'ErrorHandler' handles a 'UserException'.",
);
}

/**
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
Expand Down
Loading