8000 Add test for unrecoverable App error by mvorisek · Pull Request #1709 · atk4/ui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add test for unrecoverable App error #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2056,9 +2056,6 @@ parameters:
-
path: 'src/Wizard.php'
message: '~^Method Atk4\\Ui\\Wizard::addFinish\(\) has no return type specified\.$~'
-
path: 'tests/AppTest.php'
message: '~^Method Atk4\\Ui\\Tests\\AppTest::getApp\(\) has no return type specified\.$~'
-
path: 'tests/CallbackTest.php'
message: '~^Property Atk4\\Ui\\Tests\\CallbackTest::\$var has no type specified\.$~'
Expand All @@ -2080,9 +2077,6 @@ parameters:
-
path: 'tests/DemosTest.php'
message: '~^Strict comparison using \!\=\= between bool and null will always evaluate to true\.$~'
-
path: 'tests/ExecutorFactoryTest.php'
message: '~^Method Atk4\\Ui\\Tests\\ExecutorFactoryTest::getApp\(\) has no return type specified\.$~'
-
path: 'tests/PaginatorTest.php'
message: '~^Property Atk4\\Ui\\Tests\\PaginatorTest::\$p has no type specified\.$~'
Expand Down
90 changes: 58 additions & 32 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Atk4\Core\WarnDynamicPropertyTrait;
use Atk4\Data\Persistence;
use Atk4\Ui\Exception\ExitApplicationError;
use Atk4\Ui\Exception\LateOutputError;
use Atk4\Ui\Exception\UnhandledCallbackExceptionError;
use Atk4\Ui\Panel\Right;
use Atk4\Ui\Persistence\Ui as UiPersistence;
Expand Down Expand Up @@ -298,6 +299,10 @@ public function callExit(): void
*/
public function caughtException(\Throwable $exception): void
{
if ($exception instanceof LateOutputError) {
$this->outputLateOutputError($exception);
}

while ($exception instanceof UnhandledCallbackExceptionError) {
$exception = $exception->getPrevious();
}
Expand Down Expand Up @@ -1051,40 +1056,16 @@ function () {

// RESPONSES

/** @var string[] */
private static $_sentHeaders = [];

/**
* Output Response to the client.
* This can be overridden for future PSR-7 implementation.
*
* This can be overridden for future PSR-7 implementation
* @internal should be called only from self::outputResponse()
*/
protected function outputResponse(string $data, array $headers): void
protected function outputResponseUnsafe(string $data, array $headersNew): void
{
$this->response_headers = $this->normalizeHeaders($this->response_headers);
$headersAll = array_merge($this->response_headers, $this->normalizeHeaders($headers));
$headersNew = array_diff_assoc($headersAll, self::$_sentHeaders);

$isCli = \PHP_SAPI === 'cli'; // for phpunit

$lateError = null;
foreach (ob_get_status(true) as $status) {
if ($status['buffer_used'] !== 0 && !$isCli) {
$lateError = 'Unexpected output detected.';

break;
}
}

if ($lateError === null && count($headersNew) > 0 && headers_sent() && !$isCli) {
$lateError = 'Headers already sent, more headers cannot be set at this stage.';
}

if (!headers_sent() || $isCli) {
if ($lateError !== null) {
$headersNew = ['content-type' => 'text/plain', self::HEADER_STATUS_CODE => '500'];
}

foreach ($headersNew as $k => $v) {
if (!$isCli) {
if ($k === self::HEADER_STATUS_CODE) {
Expand All @@ -1097,18 +1078,63 @@ protected function outputResponse(string $data, array $headers): void
header($kCamelCase . ': ' . $v);
}
}
}
}

echo $data;
}

/** @var string[] */
private static $_sentHeaders = [];

/**
* Output Response to the client.
*/
protected function outputResponse(string $data, array $headers): void
{
$this->response_headers = $this->normalizeHeaders($this->response_headers);
$headersAll = array_merge($this->response_headers, $this->normalizeHeaders($headers));
unset($headers);
$headersNew = array_diff_assoc($headersAll, self::$_sentHeaders);
unset($headersAll);

self::$_sentHeaders[$k] = $v;
foreach (ob_get_status(true) as $status) {
if ($status['buffer_used'] !== 0) {
throw new LateOutputError('Unexpected output detected');
}
}

if ($lateError !== null) {
echo "\n" . '!! FATAL UI ERROR: ' . $lateError . ' !!' . "\n";
$isCli = \PHP_SAPI === 'cli'; // for phpunit

exit(1);
if (count($headersNew) > 0 && headers_sent() && !$isCli) {
throw new LateOutputError('Headers already sent, more headers cannot be set at this stage');
}

echo $data;
foreach ($headersNew as $k => $v) {
self::$_sentHeaders[$k] = $v;
}

$this->outputResponseUnsafe($data, $headersNew);
}

/**
* @return never
*/
protected function outputLateOutputError(LateOutputError $exception): void
{
$plainTextMessage = "\n" . '!! FATAL UI ERROR: ' . $exception->getMessage() . ' !!' . "\n";

$headersAll = $this->normalizeHeaders(['content-type' => 'text/plain', self::HEADER_STATUS_CODE => '500']);
$headersNew = array_diff_assoc($headersAll, self::$_sentHeaders);
unset($headersAll);

foreach ($headersNew as $k => $v) {
self::$_sentHeaders[$k] = $v;
}

$this->outputResponseUnsafe($plainTextMessage, $headersNew);

exit(1); // should be never reached from phpunit because we set catch_exceptions = false
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/LateOutputError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Exception;

/**
* @internal
*/
final class LateOutputError extends \Error
{
}
21 changes: 20 additions & 1 deletion tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\App;
use Atk4\Ui\Exception\LateOutputError;
use Atk4\Ui\HtmlTemplate;

class AppTest extends TestCase
{
protected function getApp()
protected function getApp(): App
{
return new App([
'catch_exceptions' => false,
Expand Down Expand Up @@ -41,4 +42,22 @@ public function testTemplateClassCustom(): void
$app->loadTemplate('html.html')
);
}

public function testUnexpectedOutputLateError(): void
{
$app = $this->getApp();

ob_start();
$testStr = 'direct output test';
try {
echo $testStr;

$this->expectException(LateOutputError::class);
$this->expectExceptionMessage('Unexpected output detected');
$app->terminateHtml('');
} finally {
$this->assertSame($testStr, ob_get_contents());
ob_end_clean();
}
}
}
2 changes: 1 addition & 1 deletion tests/ExecutorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function setUp(): void
$this->app->initLayout([\Atk4\Ui\Layout\Admin::class]);
}

protected function getApp()
protected function getApp(): App
{
return new App([
'catch_exceptions' => false,
Expand Down
0