Skip to content

Commit

Permalink
Fix VarDump event time
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Apr 14, 2024
1 parent adcd319 commit 9c8a196
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Sender/Console/Renderer/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function render(OutputInterface $output, Frame $frame): void

$size = $frame->getSize();
Common::renderMetadata($output, [
'Time' => $frame->time->format('Y-m-d H:i:s.u'),
'Time' => $frame->time,
'Size' => Files::normalizeSize($size) . ($size > 1024 ? \sprintf(' (%d bytes)', $size) : ''),
]);

Expand Down
9 changes: 3 additions & 6 deletions src/Sender/Console/Renderer/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function render(OutputInterface $output, Frame $frame): void
private function renderData(OutputInterface $output, Frame\Http $frame): void
{
$request = $frame->request;
$date = $frame->time->format('Y-m-d H:i:s.u');
$uri = (string)$request->getUri();
$method = $request->getMethod();
$body = $request->getBody();

$color = match ($frame->request->getMethod()) {
Expand All @@ -46,11 +43,11 @@ private function renderData(OutputInterface $output, Frame\Http $frame): void
default => Color::Gray->value,
};

Common::renderHeader1($output, 'HTTP', ...[$color => $method]);
Common::renderHeader1($output, 'HTTP', ...[$color => $request->getMethod()]);

Common::renderMetadata($output, [
'Time' => $date,
'URI' => $uri,
'Time' => $frame->time,
'URI' => (string)$request->getUri(),
]);

if ($request->getQueryParams() !== []) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sender/Console/Renderer/Sentry/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function renderMessageHeader(OutputInterface $output, array $messa
} catch (\Throwable) {
$time = new DateTimeImmutable();
}
$meta['Time'] = $time->format('Y-m-d H:i:s.u');
$meta['Time'] = $time;
isset($message['event_id']) and $meta['Event ID'] = $message['event_id'];
isset($message['transaction']) and $meta['Transaction'] = $message['transaction'];
isset($message['server_name']) and $meta['Server'] = $message['server_name'];
Expand Down
2 changes: 1 addition & 1 deletion src/Sender/Console/Renderer/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function render(OutputInterface $output, Frame $frame): void

Common::renderHeader1($output, 'SMTP');
Common::renderMetadata($output, [
'Time' => $frame->time->format('Y-m-d H:i:s.u'),
'Time' => $frame->time,
]);
$message = $frame->message;

Expand Down
3 changes: 1 addition & 2 deletions src/Sender/Console/Renderer/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public function describe(OutputInterface $output, Data $data, array $context, in
$this->dumper->setColors($output->isDecorated());

$meta = [];
$meta['Time'] = (new DateTimeImmutable())->setTimestamp((int)$context['timestamp'])
->format('Y-m-d H:i:s');
$meta['Time'] = (new DateTimeImmutable())->setTimestamp((int)$context['timestamp']);

try {
if (isset($context['source'])) {
Expand Down
20 changes: 12 additions & 8 deletions src/Sender/Console/Support/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Buggregator\Trap\Sender\Console\Support;

use DateTimeInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -52,12 +53,13 @@ public static function renderHighlightedLine(OutputInterface $output, string $li
}

/**
* @param array<array-key, string|string[]> $data
* @param array<array-key, mixed> $data
*/
public static function renderMetadata(OutputInterface $output, array $data): void
{
$maxHeaderLength = \max(\array_map('strlen', \array_keys($data)));

/** @var mixed $value */
foreach ($data as $head => $value) {
// Align headers to the right
self::renderHeader(
Expand Down Expand Up @@ -125,13 +127,10 @@ public static function renderHeaders(OutputInterface $output, array $headers): v
}
}

/**
* @param string|string[]|array $value
*/
public static function renderHeader(
OutputInterface $output,
string $name,
string|array $value,
mixed $value,
Color $keyColor = Color::Green,
Color $valueColor = Color::Default,
Color $secondKeyColor = Color::Gray,
Expand All @@ -144,9 +143,14 @@ public static function renderHeader(
$output->write(\sprintf('<fg=%s>%s</>: ', $secondKeyColor->value, $name));
}

if (!\is_scalar($item)) {
$item = \print_r($item, true);
}
$item = match (true) {
$value instanceof DateTimeInterface => $value->format('u') === '000000'
? $value->format('Y-m-d H:i:s')
: $value->format('Y-m-d H:i:s.u'),
\is_scalar($value) || $value instanceof \Stringable => (string)$value,
default => \print_r($item, true),
};

$output->writeln(\sprintf('<fg=%s>%s</>', $valueColor->value, $item));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Test/Mock/StreamClientMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Buggregator\Trap\Test\Mock\StreamClientMock\DisconnectCommand;
use Buggregator\Trap\Traffic\StreamClient;
use DateTimeImmutable;
use DateTimeInterface;
use Fiber;
use Generator;

Expand All @@ -22,6 +23,7 @@ final class StreamClientMock implements StreamClient

private function __construct(
private readonly Generator $generator,
private readonly DateTimeInterface $createdAt = new DateTimeImmutable(),
) {
$this->queue = new \SplQueue();
}
Expand Down Expand Up @@ -69,7 +71,7 @@ public function disconnect(): void

public function isDisconnected(): bool
{
$this->disconnected = $this->disconnected && $this->generator->valid();
$this->disconnected = $this->disconnected || !$this->generator->valid();
return $this->disconnected;
}

Expand Down Expand Up @@ -148,6 +150,6 @@ private function fetchFromGenerator(): void

public function getCreatedAt(): DateTimeImmutable
{
return new DateTimeImmutable();
return $this->createdAt;
}
}
2 changes: 1 addition & 1 deletion src/Traffic/Dispatcher/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function dispatch(StreamClient $stream): iterable
continue;
}

yield new Frame\VarDumper($line, $stream->getCreatedAt());
yield new Frame\VarDumper($line);
}
}

Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Traffic/Dispatcher/VarDumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Buggregator\Trap\Tests\Unit\Traffic\Dispatcher;

use Buggregator\Trap\Test\Mock\StreamClientMock;
use Buggregator\Trap\Tests\Unit\FiberTrait;
use Buggregator\Trap\Traffic\Dispatcher\VarDumper;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* @covers \Buggregator\Trap\Traffic\Dispatcher\Http
*/
class VarDumperTest extends TestCase
{
use FiberTrait;

public static function detectProvider(): iterable
{
yield ["ABC\n", true];
yield ["ABC\r\n", false];
yield ["A B C\n", false];
}

#[DataProvider('detectProvider')]
public function testDetect(string $data, ?bool $expected): void
{
$dispatcher = new VarDumper();
$this->assertSame($expected, $dispatcher->detect($data, new DateTimeImmutable()));
}

public function testDispatchFramesTime(): void
{
$dispatcher = new VarDumper();
$stream = StreamClientMock::createFromGenerator((function (): \Generator {
yield "ABC\n";
yield "DEF\n";
yield "GHI\n";
})());

$resultGenerator = $dispatcher->dispatch($stream);

$frames = $this->runInFiber(static fn () => \iterator_to_array($resultGenerator));

self::assertCount(3, $frames);
self::assertNotSame($frames[0]->time, $frames[1]->time);
self::assertNotSame($frames[1]->time, $frames[2]->time);
}
}

0 comments on commit 9c8a196

Please sign in to comment.