From 51a0d04cf25bfa5ed4390d4d20055177940770e9 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sun, 30 Jan 2022 12:01:47 +0000 Subject: [PATCH 1/3] fix data collector for symfony 4 --- src/DataCollector/EventCollector.php | 15 +++++++- tests/Fixtures/ProfileCreated.php | 13 +++++++ .../Unit/DataCollector/EventCollectorTest.php | 37 +++++++++++++++++++ .../AggregateAttributeLoaderTest.php | 11 ++---- 4 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 tests/Fixtures/ProfileCreated.php create mode 100644 tests/Unit/DataCollector/EventCollectorTest.php rename tests/Unit/{ => Loader}/AggregateAttributeLoaderTest.php (73%) diff --git a/src/DataCollector/EventCollector.php b/src/DataCollector/EventCollector.php index a515ef2c..4337d98b 100644 --- a/src/DataCollector/EventCollector.php +++ b/src/DataCollector/EventCollector.php @@ -6,9 +6,10 @@ use Patchlevel\EventSourcing\Aggregate\AggregateChanged; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector; +use Symfony\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\DataCollector\DataCollector; use Symfony\Component\VarDumper\Cloner\Data; use Throwable; @@ -23,7 +24,7 @@ * } * @psalm-property DataType $data */ -final class EventCollector extends AbstractDataCollector +final class EventCollector extends DataCollector implements TemplateAwareDataCollectorInterface { private EventListener $eventListener; /** @var array, string> */ @@ -79,4 +80,14 @@ public function getAggregates(): array { return $this->data['aggregates']; } + + public function getName(): string + { + return static::class; + } + + public function reset(): void + { + $this->data = []; + } } diff --git a/tests/Fixtures/ProfileCreated.php b/tests/Fixtures/ProfileCreated.php new file mode 100644 index 00000000..3479609c --- /dev/null +++ b/tests/Fixtures/ProfileCreated.php @@ -0,0 +1,13 @@ + 'profile'] + ); + + $event = ProfileCreated::raise('foo'); + $eventListener($event); + + $collector->collect( + new Request(), + new Response() + ); + + self::assertSame([Profile::class => 'profile'], $collector->getAggregates()); + self::assertCount(1, $collector->getEvents()); + } +} diff --git a/tests/Unit/AggregateAttributeLoaderTest.php b/tests/Unit/Loader/AggregateAttributeLoaderTest.php similarity index 73% rename from tests/Unit/AggregateAttributeLoaderTest.php rename to tests/Unit/Loader/AggregateAttributeLoaderTest.php index 644dbb4a..b523582d 100644 --- a/tests/Unit/AggregateAttributeLoaderTest.php +++ b/tests/Unit/Loader/AggregateAttributeLoaderTest.php @@ -2,14 +2,11 @@ declare(strict_types=1); -namespace Patchlevel\EventSourcingBundle\Tests\Unit; +namespace Patchlevel\EventSourcingBundle\Tests\Unit\Loader; -use Patchlevel\EventSourcing\Aggregate\AggregateChanged; -use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Patchlevel\EventSourcingBundle\Attribute\Aggregate; +use InvalidArgumentException; use Patchlevel\EventSourcingBundle\Loader\AggregateAttributesLoader; use PHPUnit\Framework\TestCase; -use InvalidArgumentException; class AggregateAttributeLoaderTest extends TestCase { @@ -20,7 +17,7 @@ public function testDuplicateAggregateName() $loader = new AggregateAttributesLoader(); $loader->load([ - __DIR__ . '/../Fixtures/AttributedAggregatesSameName' + __DIR__ . '/../../Fixtures/AttributedAggregatesSameName' ]); } @@ -28,7 +25,7 @@ public function testLoadPathWithAttributedAggregates() { $loader = new AggregateAttributesLoader(); $result = $loader->load([ - __DIR__ . '/../Fixtures/AttributedAggregates' + __DIR__ . '/../../Fixtures/AttributedAggregates' ]); self::assertCount(2, $result); From d243b776c9612a2a8da0a0ac3a6158b498049857 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sun, 30 Jan 2022 12:07:26 +0000 Subject: [PATCH 2/3] fix template definition --- src/DataCollector/EventCollector.php | 8 +------- .../PatchlevelEventSourcingExtension.php | 4 +++- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/DataCollector/EventCollector.php b/src/DataCollector/EventCollector.php index 4337d98b..eea120b7 100644 --- a/src/DataCollector/EventCollector.php +++ b/src/DataCollector/EventCollector.php @@ -6,7 +6,6 @@ use Patchlevel\EventSourcing\Aggregate\AggregateChanged; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Symfony\Bundle\FrameworkBundle\DataCollector\TemplateAwareDataCollectorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; @@ -24,7 +23,7 @@ * } * @psalm-property DataType $data */ -final class EventCollector extends DataCollector implements TemplateAwareDataCollectorInterface +final class EventCollector extends DataCollector { private EventListener $eventListener; /** @var array, string> */ @@ -60,11 +59,6 @@ function (AggregateChanged $event) { ]; } - public static function getTemplate(): string - { - return '@PatchlevelEventSourcing/Collector/template.html.twig'; - } - /** * @return list, aggregateId: string, payload: Data, playhead: int, recordedOn: string}> */ diff --git a/src/DependencyInjection/PatchlevelEventSourcingExtension.php b/src/DependencyInjection/PatchlevelEventSourcingExtension.php index 13384052..0d0d149d 100644 --- a/src/DependencyInjection/PatchlevelEventSourcingExtension.php +++ b/src/DependencyInjection/PatchlevelEventSourcingExtension.php @@ -450,7 +450,9 @@ private function configureProfiler(ContainerBuilder $container): void new Reference(EventListener::class), '%event_sourcing.aggregates%', ]) - ->addTag('data_collector'); + ->addTag('data_collector', [ + 'template' => '@PatchlevelEventSourcing/Collector/template.html.twig' + ]); } /** From c027e4d3d4c57f2032dc12528adc497055ee0e49 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sun, 30 Jan 2022 12:09:39 +0000 Subject: [PATCH 3/3] fix ci --- src/DependencyInjection/PatchlevelEventSourcingExtension.php | 4 +--- tests/Unit/Loader/AggregateAttributeLoaderTest.php | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/DependencyInjection/PatchlevelEventSourcingExtension.php b/src/DependencyInjection/PatchlevelEventSourcingExtension.php index 0d0d149d..219e54fe 100644 --- a/src/DependencyInjection/PatchlevelEventSourcingExtension.php +++ b/src/DependencyInjection/PatchlevelEventSourcingExtension.php @@ -450,9 +450,7 @@ private function configureProfiler(ContainerBuilder $container): void new Reference(EventListener::class), '%event_sourcing.aggregates%', ]) - ->addTag('data_collector', [ - 'template' => '@PatchlevelEventSourcing/Collector/template.html.twig' - ]); + ->addTag('data_collector', ['template' => '@PatchlevelEventSourcing/Collector/template.html.twig']); } /** diff --git a/tests/Unit/Loader/AggregateAttributeLoaderTest.php b/tests/Unit/Loader/AggregateAttributeLoaderTest.php index b523582d..27d06920 100644 --- a/tests/Unit/Loader/AggregateAttributeLoaderTest.php +++ b/tests/Unit/Loader/AggregateAttributeLoaderTest.php @@ -13,7 +13,6 @@ class AggregateAttributeLoaderTest extends TestCase public function testDuplicateAggregateName() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('found duplicate aggregate name "profileWithAttribute", it was found in class "Patchlevel\EventSourcingBundle\Tests\Fixtures\AttributedAggregatesSameName\SnapshotableProfileWithAttribute" and "Patchlevel\EventSourcingBundle\Tests\Fixtures\AttributedAggregatesSameName\ProfileWithAttribute"'); $loader = new AggregateAttributesLoader(); $loader->load([