Skip to content

Commit

Permalink
improve mt
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Aug 8, 2023
1 parent 8279161 commit fbffb54
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ext-json": "*",
"chevere/chevere": "^3.1",
"chevere/http": "^0.2",
"chevere/throwable-handler": "^0.10",
"chevere/throwable-handler": "^0.10.x-dev",
"chevere/trace": "^0.8",
"chevere/var-dump": "^0.8",
"phpseclib/phpseclib": "~3.0"
Expand Down
27 changes: 15 additions & 12 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,26 @@ private function handleDumpVars(): void
))
->withVariables(...$this->vars)
->process($this->writer);
$dumpString = $this->writer->__toString();
if ($dumpString !== '') {
$this->body .= '<div class="dump">' . $dumpString . '</div>';
$dump = $this->writer->__toString();
if ($dump === '') {
return;
}
$this->body .= <<<HTML
<div class="dump">{$dump}</div>
HTML;
}

private function handleBacktrace(): void
{
if ($this->isFlagBacktrace) {
$traceDocument = new Trace(
$this->backtrace,
new ThrowableHandlerHtmlFormat()
);
$this->body .= '<div class="backtrace">'
. "\n"
. $traceDocument->__toString()
. '</div>';
if (! $this->isFlagBacktrace) {
return;
}
$trace = new Trace(
$this->backtrace,
new ThrowableHandlerHtmlFormat()
);
$this->body .= <<<HTML
<div class="backtrace">{$trace->__toString()}</div>
HTML;
}
}
14 changes: 7 additions & 7 deletions src/ThrowableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ThrowableParser
{
public const OPEN_TEMPLATE = '<div class="throwable">';

public const CLOSE_TEMPLATE = '</div>';
public const CLOSE_TEMPLATE = '</div><!-- t -->';

public const ITEM_TEMPLATE = <<<HTML
<div class="throwable-item">
Expand All @@ -42,18 +42,16 @@ final class ThrowableParser

private string $emote = '⚠️Throwable';

private FormatInterface $format;

private int $index = 0;

private ThrowableReadInterface $throwableRead;
private FormatInterface $format;

public function __construct(
private Throwable $throwable, // @phpstan-ignore-line
private ThrowableReadInterface $throwableRead,
private string $extra = '',
) {
$this->throwableRead = new ThrowableRead($throwable);
$this->format = new HtmlFormat();
$throwable = $this->throwableRead->throwable();
$this->topic = basename(
str_replace(
'\\',
Expand Down Expand Up @@ -118,7 +116,9 @@ private function parse(Throwable $throwable): ?Throwable
: '',
'%trace%' => $traceDocument->__toString(),
];
$this->appendBodyLine(strtr(static::ITEM_TEMPLATE, $translate));
$this->appendBodyLine(
strtr(static::ITEM_TEMPLATE, $translate)
);

return $throwable->getPrevious();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Xr.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ private function getConfigFile(): string
return $configFullPath;
}
}
// @infection-ignore-all
$parentDirectory = dirname($configDirectory) . DIRECTORY_SEPARATOR;
// @infection-ignore-all
if ($parentDirectory === $configDirectory) {
return '';
}
Expand Down
4 changes: 3 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
declare(strict_types=1);

namespace Chevere\Xr {
use Chevere\ThrowableHandler\ThrowableRead;
use Chevere\Writer\Interfaces\WriterInterface;
use Chevere\Writer\StreamWriter;
use Chevere\Xr\Interfaces\XrInterface;
Expand Down Expand Up @@ -112,7 +113,8 @@ function throwableHandler(Throwable $throwable, string $extra = ''): void
if ($xr === null || $xr->isEnabled() === false) {
return; // @codeCoverageIgnore
}
$parser = new ThrowableParser($throwable, $extra);
$read = new ThrowableRead($throwable);
$parser = new ThrowableParser($read, $extra);
$xr->client()
->sendMessage(
(new Message(
Expand Down
37 changes: 31 additions & 6 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ public function testEmptyBacktrace(): void
);
}

public function testMissingFileLine(): void
{
$trace = [
[],
];
$message = new Message($trace);
$this->assertSame('', $message->filePath());
$this->assertSame(0, $message->fileLine());
$this->assertSame(
[
'file_line' => '0',
'file_path' => '',
],
$this->filterArray($message->toArray(), 'file_path', 'file_line')
);
}

public function testDeclaredBacktrace(): void
{
$testFile = 'test';
Expand Down Expand Up @@ -127,9 +144,12 @@ public function testWithWriter(): void
);
}

public function testWithVariables(): void
public function testWithDumpVars(): void
{
$message = (new Message())->withWriter(getWriter());
$body = 'body string';
$message = (new Message())
->withBody($body)
->withWriter(getWriter());
$variable = 'Hola, mundo!';
$length = strlen($variable);
$withVariables = $message->withVariables($variable);
Expand All @@ -138,20 +158,25 @@ public function testWithVariables(): void
$variable,
$withVariables->vars()[0]
);
$this->assertSame('<div class="dump"><pre>
Arg•1 <span style="color:#ff8700">string</span> ' . $variable . ' <em><span style="color:rgb(108 108 108 / 65%);">(length=' . $length . ')</span></em></pre></div>', $withVariables->toArray()['body']);
$expected = $body . <<<HTML
<div class="dump"><pre>
Arg•1 <span style="color:#ff8700">string</span> {$variable} <em><span style="color:rgb(108 108 108 / 65%);">(length={$length})</span></em></pre></div>
HTML;
$this->assertSame($expected, $withVariables->toArray()['body']);
}

public function testWithBacktraceFlag(): void
{
$message = new Message();
$body = 'body string';
$message = (new Message())->withBody($body);
$line = strval(__LINE__ - 1);
$this->assertFalse($message->isEnableBacktrace());
$withBacktraceFlag = $message->withFlags(XR_BACKTRACE);
$this->assertNotSame($message, $withBacktraceFlag);
$this->assertTrue($withBacktraceFlag->isEnableBacktrace());
$expected = $body . '<div class="backtrace">';
$this->assertStringContainsString(
'<div class="backtrace">',
$expected,
$withBacktraceFlag->toArray()['body']
);
$this->assertStringContainsString(
Expand Down
56 changes: 47 additions & 9 deletions tests/ThrowableParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,58 @@
namespace Chevere\Tests;

use Chevere\Throwable\Errors\TypeError;
use Chevere\ThrowableHandler\Formats\HtmlFormat;
use Chevere\ThrowableHandler\Formats\PlainFormat;
use Chevere\ThrowableHandler\ThrowableRead;
use Chevere\Trace\Trace;
use Chevere\Xr\ThrowableParser;
use Exception;
use PHPUnit\Framework\TestCase;
use function Chevere\Message\message;

final class ThrowableParserTest extends TestCase
{
public function testTopLevel(): void
{
$throwable = new Exception('foo');
$parser = new ThrowableParser($throwable, '');
$read = new ThrowableRead($throwable);
$parser = new ThrowableParser($read);
$this->assertSame(Exception::class, $parser->topic());
$this->assertSame(
Exception::class,
$parser->throwableRead()->className()
);
$this->assertStringStartsWith(
ThrowableParser::OPEN_TEMPLATE . "\n",
$parser->body()
);
$this->assertStringEndsWith(
ThrowableParser::CLOSE_TEMPLATE . "\n",
$parser->body()
);
$this->assertStringContainsString(
'<div class="throwable-code">0</div>',
$parser->body()
);
$trace = new Trace(
$parser->throwableRead()->trace(),
new HtmlFormat()
);
$trace = (string) $trace;
$this->assertStringContainsString(
<<<HTML
<div class="throwable-backtrace backtrace">{$trace}</div>
HTML,
$parser->body()
);
$this->assertSame('⚠️Throwable', $parser->emote());
$this->assertStringContainsString(Exception::class, $parser->body());
}

public function testNamespaced(): void
{
$throwable = new TypeError(message: message('foo'));
$parser = new ThrowableParser($throwable, '');
$throwable = new TypeError('foo');
$read = new ThrowableRead($throwable);
$parser = new ThrowableParser($read);
$this->assertSame('TypeError', $parser->topic());
$this->assertSame(
TypeError::class,
Expand All @@ -51,19 +79,29 @@ public function testNamespaced(): void

public function testWithPrevious(): void
{
$throwable = new Exception('foo', previous: new Exception('bar'));
$parser = new ThrowableParser($throwable, '');
$file = __FILE__;
$line = __LINE__ + 1;
$previous = new Exception('bar');
$throwable = new Exception('foo', previous: $previous);
$read = new ThrowableRead($throwable);
$parser = new ThrowableParser($read);
$body = strip_tags($parser->body());
$this->assertStringContainsString(
'<div class="throwable-message">bar</div>',
$parser->body()
<<<PLAIN
#0 {$file}:{$line}
{main}()
PLAIN,
$body
);
}

public function testWithExtra(): void
{
$extra = 'EXTRA EXTRA! TODD SMELLS';
$throwable = new Exception('foo');
$parser = new ThrowableParser($throwable, $extra);
$read = new ThrowableRead($throwable);
$format = new PlainFormat();
$parser = new ThrowableParser($read, $extra);
$this->assertStringContainsString($extra, $parser->body());
}
}

0 comments on commit fbffb54

Please sign in to comment.