2017-05-11 07:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Logging\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Logging\DetailedErrorFormatter;
|
|
|
|
use SilverStripe\Logging\Tests\DetailedErrorFormatterTest\ErrorGenerator;
|
|
|
|
|
|
|
|
class DetailedErrorFormatterTest extends SapphireTest
|
|
|
|
{
|
2018-10-20 14:41:45 +02:00
|
|
|
public function testFormatWithException()
|
2017-05-11 07:38:29 +02:00
|
|
|
{
|
|
|
|
$generator = new ErrorGenerator();
|
|
|
|
$formatter = new DetailedErrorFormatter();
|
|
|
|
$exception = $generator->mockException();
|
|
|
|
|
2018-01-16 19:39:30 +01:00
|
|
|
$output = '' . $formatter->format(['context' => [
|
2017-05-11 07:38:29 +02:00
|
|
|
'exception' => $exception,
|
|
|
|
]]);
|
|
|
|
|
|
|
|
$base = __DIR__;
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('ERROR [Emergency]: Uncaught Exception: Error', $output);
|
|
|
|
$this->assertStringContainsString("Line 32 in $base/DetailedErrorFormatterTest/ErrorGenerator.php", $output);
|
|
|
|
$this->assertStringContainsString('* 32: throw new Exception(\'Error\');', $output);
|
|
|
|
$this->assertStringContainsString(
|
2021-11-16 00:55:00 +01:00
|
|
|
'SilverStripe\\Logging\\Tests\\DetailedErrorFormatterTest\\ErrorGenerator->mockException',
|
2017-05-11 07:38:29 +02:00
|
|
|
$output
|
|
|
|
);
|
|
|
|
}
|
2018-10-20 14:41:45 +02:00
|
|
|
|
|
|
|
public function testFormatWithoutException()
|
|
|
|
{
|
|
|
|
$record = [
|
|
|
|
'code' => 401,
|
|
|
|
'message' => 'Denied',
|
|
|
|
'file' => 'index.php',
|
|
|
|
'line' => 4,
|
|
|
|
];
|
|
|
|
|
|
|
|
$formatter = new DetailedErrorFormatter();
|
|
|
|
$result = $formatter->format($record);
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('ERRNO 401', $result, 'Status code was not found in trace');
|
|
|
|
$this->assertStringContainsString('Denied', $result, 'Message was not found in trace');
|
|
|
|
$this->assertStringContainsString('Line 4 in index.php', $result, 'Line or filename were not found in trace');
|
|
|
|
$this->assertStringContainsString(self::class, $result, 'Backtrace doesn\'t show current test class');
|
2018-10-20 14:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatBatch()
|
|
|
|
{
|
|
|
|
$records = [
|
|
|
|
[
|
|
|
|
'code' => 401,
|
|
|
|
'message' => 'Denied',
|
|
|
|
'file' => 'index.php',
|
|
|
|
'line' => 4,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'code' => 404,
|
|
|
|
'message' => 'Not found',
|
|
|
|
'file' => 'admin.php',
|
|
|
|
'line' => 7,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$formatter = new DetailedErrorFormatter();
|
|
|
|
$result = $formatter->formatBatch($records);
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('ERRNO 401', $result, 'First status code was not found in trace');
|
|
|
|
$this->assertStringContainsString('ERRNO 404', $result, 'Second status code was not found in trace');
|
|
|
|
$this->assertStringContainsString('Denied', $result, 'First message was not found in trace');
|
|
|
|
$this->assertStringContainsString('Not found', $result, 'Second message was not found in trace');
|
2018-10-20 14:41:45 +02:00
|
|
|
}
|
2017-05-11 07:38:29 +02:00
|
|
|
}
|