Skip to content

Commit e5c58cc

Browse files
committed
fix autoconfigure projectors
1 parent 9a254ce commit e5c58cc

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/DependencyInjection/PatchlevelEventSourcingExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
1717
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
1818
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
19+
use Patchlevel\EventSourcing\Attribute\Projector;
1920
use Patchlevel\EventSourcing\Clock\FrozenClock;
2021
use Patchlevel\EventSourcing\Clock\SystemClock;
2122
use Patchlevel\EventSourcing\Console\Command\DatabaseCreateCommand;
@@ -55,7 +56,6 @@
5556
use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper;
5657
use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository;
5758
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
58-
use Patchlevel\EventSourcing\Projection\Projector\Projector;
5959
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
6060
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
6161
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
@@ -93,6 +93,7 @@
9393
use Patchlevel\Hydrator\MetadataHydrator;
9494
use Psr\Log\LoggerInterface;
9595
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
96+
use Symfony\Component\DependencyInjection\ChildDefinition;
9697
use Symfony\Component\DependencyInjection\ContainerBuilder;
9798
use Symfony\Component\DependencyInjection\ContainerInterface;
9899
use Symfony\Component\DependencyInjection\Reference;
@@ -208,8 +209,12 @@ private function configureEventBus(array $config, ContainerBuilder $container):
208209
/** @param Config $config */
209210
private function configureProjection(array $config, ContainerBuilder $container): void
210211
{
211-
$container->registerForAutoconfiguration(Projector::class)
212-
->addTag('event_sourcing.projector');
212+
$container->registerAttributeForAutoconfiguration(
213+
Projector::class,
214+
static function (ChildDefinition $definition): void {
215+
$definition->addTag('event_sourcing.projector');
216+
},
217+
);
213218

214219
$container->register(InMemoryProjectorRepository::class)
215220
->setArguments([
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Patchlevel\EventSourcingBundle\Tests\Fixtures;
4+
5+
use Patchlevel\EventSourcing\Attribute\Projector;
6+
7+
#[Projector('profile')]
8+
class ProfileProjector
9+
{
10+
}

tests/Unit/PatchlevelEventSourcingBundleTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper;
3939
use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist;
4040
use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist;
41+
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
4142
use Patchlevel\EventSourcing\Repository\DefaultRepository;
4243
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
4344
use Patchlevel\EventSourcing\Repository\RepositoryManager;
@@ -62,6 +63,7 @@
6263
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Processor2;
6364
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Profile;
6465
use Patchlevel\EventSourcingBundle\Tests\Fixtures\ProfileCreated;
66+
use Patchlevel\EventSourcingBundle\Tests\Fixtures\ProfileProjector;
6567
use Patchlevel\EventSourcingBundle\Tests\Fixtures\SnapshotableProfile;
6668
use PHPUnit\Framework\TestCase;
6769
use Prophecy\PhpUnit\ProphecyTrait;
@@ -202,6 +204,43 @@ public function testProcessorListener(): void
202204
);
203205
}
204206

207+
208+
public function testAutoconfigureProcessorListener(): void
209+
{
210+
$container = new ContainerBuilder();
211+
$container->setDefinition(Processor1::class, new Definition(Processor1::class))
212+
->setAutoconfigured(true);
213+
$container->setDefinition(Processor2::class, new Definition(Processor1::class))
214+
->setAutoconfigured(false);
215+
216+
$this->compileContainer(
217+
$container,
218+
[
219+
'patchlevel_event_sourcing' => [
220+
'connection' => [
221+
'service' => 'doctrine.dbal.eventstore_connection',
222+
],
223+
'projection' => [
224+
'sync' => false,
225+
],
226+
],
227+
]
228+
);
229+
230+
self::assertInstanceOf(DefaultEventBus::class, $container->get(EventBus::class));
231+
self::assertEquals(
232+
[
233+
'Patchlevel\EventSourcingBundle\Tests\Fixtures\Processor1' => [
234+
[],
235+
],
236+
'Patchlevel\EventSourcingBundle\DataCollector\MessageListener' => [
237+
[]
238+
]
239+
],
240+
$container->findTaggedServiceIds('event_sourcing.processor')
241+
);
242+
}
243+
205244
public function testSymfonyEventBus(): void
206245
{
207246
$container = new ContainerBuilder();
@@ -710,6 +749,30 @@ public function testProjectionistAutoBoot(): void
710749
self::assertInstanceOf(ProjectionistAutoBootListener::class, $container->get(ProjectionistAutoBootListener::class));
711750
}
712751

752+
public function testAutoconfigureProjector(): void
753+
{
754+
$container = new ContainerBuilder();
755+
$container->setDefinition(ProfileProjector::class, new Definition(ProfileProjector::class))
756+
->setAutoconfigured(true);
757+
758+
$this->compileContainer(
759+
$container,
760+
[
761+
'patchlevel_event_sourcing' => [
762+
'connection' => [
763+
'service' => 'doctrine.dbal.eventstore_connection',
764+
],
765+
],
766+
]
767+
);
768+
769+
$repository = $container->get(ProjectorRepository::class);
770+
$projectors = $repository->projectors();
771+
772+
self::assertCount(1, $projectors);
773+
self::assertInstanceOf(ProfileProjector::class, $projectors[0]);
774+
}
775+
713776
public function testSchemaMerge(): void
714777
{
715778
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)