Skip to content

Commit

Permalink
improve output style
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Feb 7, 2024
1 parent 508eedd commit 0aa0335
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
43 changes: 27 additions & 16 deletions src/Console/OutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,43 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

use function array_keys;
use function array_values;
use function sprintf;

final class OutputStyle extends SymfonyStyle
{
public function message(EventSerializer $serializer, Message $message): void
{
$event = $message->event();
$data = $serializer->serialize($event, [Encoder::OPTION_PRETTY_PRINT => true]);

try {
$data = $serializer->serialize($event, [Encoder::OPTION_PRETTY_PRINT => true]);
} catch (Throwable $error) {
$this->error(
sprintf(
'Error while serializing event "%s": %s',
$message->event()::class,
$error->getMessage(),
),
);

if ($this->isVeryVerbose()) {
$this->throwable($error);
}

return;
}

$this->title($data->name);

$this->horizontalTable([
'eventClass',
'aggregateName',
'aggregateId',
'playhead',
'recordedOn',
], [
[
$event::class,
$message->aggregateName(),
$message->aggregateId(),
$message->playhead(),
$message->recordedOn()->format(DateTimeInterface::ATOM),
],
]);
$headers = $message->headers();

if (isset($headers['recordedOn']) && $headers['recordedOn'] instanceof DateTimeInterface) {
$headers['recordedOn'] = $headers['recordedOn']->format(DateTimeInterface::ATOM);
}

$this->horizontalTable(array_keys($headers), [array_values($headers)]);

$this->block($data->payload);
}
Expand Down
37 changes: 34 additions & 3 deletions tests/Unit/Console/OutputStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

Expand All @@ -23,7 +24,7 @@ final class OutputStyleTest extends TestCase
{
use ProphecyTrait;

public function testWrite(): void
public function testMessage(): void
{
$input = new ArrayInput([]);
$output = new BufferedOutput();
Expand Down Expand Up @@ -52,8 +53,38 @@ public function testWrite(): void
$content = $output->fetch();

self::assertStringContainsString('profile.created', $content);
self::assertStringContainsString('Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated', $content);
self::assertStringContainsString('Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile', $content);
self::assertStringContainsString('profile', $content);
self::assertStringContainsString('{"id":"1","email":"[email protected]"}', $content);
}

public function testMessageWithError(): void
{
$input = new ArrayInput([]);
$output = new BufferedOutput();

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$serializer = $this->prophesize(EventSerializer::class);
$serializer
->serialize($event, [Encoder::OPTION_PRETTY_PRINT => true])
->willThrow(new RuntimeException('Unknown Error'));

$message = Message::create($event)
->withAggregateName('profile')
->withAggregateId('1')
->withPlayhead(1)
->withRecordedOn(new DateTimeImmutable());

$console = new OutputStyle($input, $output);

$console->message($serializer->reveal(), $message);

$content = $output->fetch();

self::assertStringContainsString('Unknown Error', $content);
self::assertStringContainsString(ProfileCreated::class, $content);
}
}

0 comments on commit 0aa0335

Please sign in to comment.