Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add debug command #256

Merged
merged 3 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ vendor: composer.lock
composer install

.PHONY: phpcs-check
phpcs-check: vendor ## run phpcs
cs-check: vendor ## run phpcs
vendor/bin/phpcs

.PHONY: phpcs-fix
cs: vendor ## run phpcs fixer
cs: vendor ## run phpcs fixer
vendor/bin/phpcbf

.PHONY: phpstan
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint">
<exclude-pattern>src/Console/Command</exclude-pattern>
</exclude>
<exclude name="Generic.Files.LineLength.TooLong"/>
</rule>
</ruleset>
63 changes: 63 additions & 0 deletions src/Console/Command/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Console\Command;

use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function array_keys;
use function array_map;
use function array_values;

final class DebugCommand extends Command
{
protected static $defaultName = 'event-sourcing:debug';
protected static $defaultDescription = 'show event sourcing debug information';

public function __construct(
private readonly AggregateRootRegistry $aggregateRootRegistry,
private readonly EventRegistry $eventRegistry
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$console = new OutputStyle($input, $output);

$this->showAggregates($console);
$this->showEvents($console);

return 0;
}

private function showAggregates(OutputStyle $console): void
{
$console->title('Aggregates');

$aggregates = $this->aggregateRootRegistry->aggregateClasses();

$console->table(
['name', 'class'],
array_map(null, array_keys($aggregates), array_values($aggregates))
);
}

private function showEvents(OutputStyle $console): void
{
$console->title('Events');

$events = $this->eventRegistry->eventClasses();

$console->table(
['name', 'class'],
array_map(null, array_keys($events), array_values($events))
);
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Console/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Console\Command;

use Patchlevel\EventSourcing\Console\Command\DebugCommand;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

/** @covers \Patchlevel\EventSourcing\Console\Command\DebugCommand */
final class DebugCommandTest extends TestCase
{
public function testSuccessful(): void
{
$command = new DebugCommand(
new AggregateRootRegistry(['profile' => Profile::class]),
new EventRegistry(['profile.created' => ProfileCreated::class])
);

$input = new ArrayInput([]);
$output = new BufferedOutput();

$exitCode = $command->run($input, $output);

self::assertSame(0, $exitCode);

$content = $output->fetch();

self::assertStringContainsString('profile', $content);
self::assertStringContainsString(Profile::class, $content);
self::assertStringContainsString('profile.created', $content);
self::assertStringContainsString(ProfileCreated::class, $content);
}
}