diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2018-08-03 04:01:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-03 04:01:37 +0300 |
commit | f8ea46b3252ee14da53b32a9118e2af494c29eac (patch) | |
tree | ef57f1b96a0eba2477fd3939ddfdb1401dbfbcff /tests | |
parent | cb1d83db863938ace3ebdafd072dfd32e434fded (diff) |
Reconstruct partial stack traces for fatal errors. (#13238)
* Reconstruct partial stack traces for fatal errors.
* tweak to fatal error breadcrumb
* Add docs + fix test.
* Add fatal error test and improve fatal error stack trace a bit.
* fix test on travis
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Integration/ErrorHandlerTest.php | 39 | ||||
-rw-r--r-- | tests/resources/trigger-fatal.php | 57 |
2 files changed, 96 insertions, 0 deletions
diff --git a/tests/PHPUnit/Integration/ErrorHandlerTest.php b/tests/PHPUnit/Integration/ErrorHandlerTest.php new file mode 100644 index 0000000000..eaf290c574 --- /dev/null +++ b/tests/PHPUnit/Integration/ErrorHandlerTest.php @@ -0,0 +1,39 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Integration; + + +use Piwik\Http; +use Piwik\Tests\Framework\Fixture; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; + +class ErrorHandlerTest extends IntegrationTestCase +{ + public function test_fatalErrorStackTracesReturned() + { + $url = Fixture::getRootUrl() . '/tests/resources/trigger-fatal.php?format=json'; + $response = Http::sendHttpRequest($url, 2); + + $response = json_decode($response, $isAssoc = true); + $response['message'] = $this->cleanMessage($response['message']); + + $this->assertEquals('error', $response['result']); + + $expectedFormat = <<<FORMAT +Allowed memory size of %s bytes exhausted (tried to allocate %s bytes) on {includePath}/tests/resources/trigger-fatal.php(22)#0 {includePath}/tests/resources/trigger-fatal.php(35): MyClass->triggerError()#1 {includePath}/tests/resources/trigger-fatal.php(51): MyDerivedClass::staticMethod()#2 {includePath}/tests/resources/trigger-fatal.php(57): myFunction() +FORMAT; + + $this->assertStringMatchesFormat($expectedFormat, $response['message']); + } + + private function cleanMessage($message) + { + return str_replace(PIWIK_INCLUDE_PATH, '{includePath}', $message); + } +}
\ No newline at end of file diff --git a/tests/resources/trigger-fatal.php b/tests/resources/trigger-fatal.php new file mode 100644 index 0000000000..973622e6e0 --- /dev/null +++ b/tests/resources/trigger-fatal.php @@ -0,0 +1,57 @@ +<?php + +define('PIWIK_PRINT_ERROR_BACKTRACE', true); +define('PIWIK_ENABLE_DISPATCH', false); + +require_once __DIR__ . '/../../tests/PHPUnit/proxy/index.php'; + +$environment = new \Piwik\Application\Environment(null); +$environment->init(); + +\Piwik\Access::getInstance()->setSuperUserAccess(true); + +class MyClass +{ + public function triggerError() + { + try { + \Piwik\ErrorHandler::pushFatalErrorBreadcrumb(static::class); + + $val = ""; + while (true) { + $val .= str_repeat("*", 1024 * 1024 * 1024); + } + } finally { + \Piwik\ErrorHandler::popFatalErrorBreadcrumb(); + } + } + + public static function staticMethod() + { + try { + \Piwik\ErrorHandler::pushFatalErrorBreadcrumb(static::class); + + $instance = new MyClass(); + $instance->triggerError(); + } finally { + \Piwik\ErrorHandler::popFatalErrorBreadcrumb(); + } + } +} + +class MyDerivedClass extends MyClass +{ +} + +function myFunction() +{ + try { + \Piwik\ErrorHandler::pushFatalErrorBreadcrumb(); + + MyDerivedClass::staticMethod(); + } finally { + \Piwik\ErrorHandler::popFatalErrorBreadcrumb(); + } +} + +myFunction(); |