Skip to content
Merged
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
86 changes: 84 additions & 2 deletions tests/http/stateless/ApplicationErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace yii2\extensions\psrbridge\tests\http\stateless;

use PHPUnit\Framework\Attributes\{DataProviderExternal, Group, RequiresPhpExtension};
use yii\base\{Exception, InvalidConfigException};
use yii\base\{Event, Exception, InvalidConfigException};
use yii\helpers\Json;
use yii\log\{FileTarget, Logger};
use yii2\extensions\psrbridge\http\Response;
use yii2\extensions\psrbridge\http\{Response, StatelessApplication};
use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider;
use yii2\extensions\psrbridge\tests\support\FactoryHelper;
use yii2\extensions\psrbridge\tests\TestCase;
Expand Down Expand Up @@ -75,6 +75,79 @@ public function testErrorViewLogic(
@\runkit_constant_redefine('YII_DEBUG', true);
}

/**
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
public function testEventAfterRequestIsTriggeredWhenHandlingException(): void
{
$eventTriggered = false;
$eventName = null;
$eventSender = null;
$eventCount = 0;

$app = $this->statelessApplication(
[
'flushLogger' => false,
'components' => [
'errorHandler' => ['errorAction' => null],
],
],
);

$app->on(
StatelessApplication::EVENT_AFTER_REQUEST,
static function (Event $event) use (&$eventTriggered, &$eventCount, &$eventName, &$eventSender): void {
if ($event->name === StatelessApplication::EVENT_AFTER_REQUEST) {
$eventTriggered = true;
$eventName = $event->name;
$eventSender = $event->sender;
$eventCount++;
}
},
);

$response = $app->handle(FactoryHelper::createRequest('GET', '/site/trigger-exception'));

self::assertSame(
500,
$response->getStatusCode(),
"Expected HTTP '500' for route 'site/trigger-exception'.",
);
self::assertSame(
'text/html; charset=UTF-8',
$response->getHeaderLine('Content-Type'),
"Expected Content-Type 'text/html; charset=UTF-8' for route 'site/trigger-exception'.",
);
self::assertStringContainsString(
self::normalizeLineEndings(
<<<HTML
<pre>Exception (Exception) &apos;yii\base\Exception&apos; with message &apos;Exception error message.&apos;
HTML,
),
self::normalizeLineEndings($response->getBody()->getContents()),
'Response body should contain content from Response object.',
);
self::assertTrue(
$eventTriggered,
'EVENT_AFTER_REQUEST should be triggered when handling an exception.',
);
self::assertSame(
StatelessApplication::EVENT_AFTER_REQUEST,
$eventName,
'Triggered event should be EVENT_AFTER_REQUEST.',
);
self::assertSame(
$app,
$eventSender,
'Event sender should be the StatelessApplication instance.',
);
self::assertSame(
1,
$eventCount,
'EVENT_AFTER_REQUEST should be triggered exactly once when handling an exception.',
);
}

/**
* @throws InvalidConfigException if the configuration is invalid or incomplete.
*/
Expand Down Expand Up @@ -196,6 +269,15 @@ public function testLogExceptionIsCalledWhenHandlingException(): void
$response->getHeaderLine('Content-Type'),
"Expected Content-Type 'text/html; charset=UTF-8' for route 'site/trigger-exception'.",
);
self::assertStringContainsString(
self::normalizeLineEndings(
<<<HTML
<pre>Exception (Exception) &apos;yii\base\Exception&apos; with message &apos;Exception error message.&apos;
HTML,
),
self::normalizeLineEndings($response->getBody()->getContents()),
'Response body should contain content from Response object.',
);

$logMessages = $app->getLog()->getLogger()->messages;

Expand Down
Loading