Skip to content

Commit a5dc61e

Browse files
authored
Merge pull request #63 from patchlevel/fix-data-collector-for-symfony4
fix data collector for symfony 4
2 parents 27b17e2 + c027e4d commit a5dc61e

File tree

6 files changed

+95
-44
lines changed

6 files changed

+95
-44
lines changed

src/DataCollector/EventCollector.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
88
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
9-
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
109
use Symfony\Component\HttpFoundation\Request;
1110
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1212
use Symfony\Component\VarDumper\Cloner\Data;
1313
use Throwable;
1414

@@ -23,7 +23,7 @@
2323
* }
2424
* @psalm-property DataType $data
2525
*/
26-
final class EventCollector extends AbstractDataCollector
26+
final class EventCollector extends DataCollector
2727
{
2828
private EventListener $eventListener;
2929
/** @var array<class-string<AggregateRoot>, string> */
@@ -59,11 +59,6 @@ function (AggregateChanged $event) {
5959
];
6060
}
6161

62-
public static function getTemplate(): string
63-
{
64-
return '@PatchlevelEventSourcing/Collector/template.html.twig';
65-
}
66-
6762
/**
6863
* @return list<array{class: class-string<AggregateChanged>, aggregateId: string, payload: Data, playhead: int, recordedOn: string}>
6964
*/
@@ -79,4 +74,14 @@ public function getAggregates(): array
7974
{
8075
return $this->data['aggregates'];
8176
}
77+
78+
public function getName(): string
79+
{
80+
return static::class;
81+
}
82+
83+
public function reset(): void
84+
{
85+
$this->data = [];
86+
}
8287
}

src/DependencyInjection/PatchlevelEventSourcingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ private function configureProfiler(ContainerBuilder $container): void
450450
new Reference(EventListener::class),
451451
'%event_sourcing.aggregates%',
452452
])
453-
->addTag('data_collector');
453+
->addTag('data_collector', ['template' => '@PatchlevelEventSourcing/Collector/template.html.twig']);
454454
}
455455

456456
/**

tests/Fixtures/ProfileCreated.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Patchlevel\EventSourcingBundle\Tests\Fixtures;
4+
5+
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
6+
7+
class ProfileCreated extends AggregateChanged
8+
{
9+
public static function raise(string $id): static
10+
{
11+
return new static($id);
12+
}
13+
}

tests/Unit/AggregateAttributeLoaderTest.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcingBundle\Tests\Unit\DataCollector;
6+
7+
use Patchlevel\EventSourcingBundle\DataCollector\EventCollector;
8+
use Patchlevel\EventSourcingBundle\DataCollector\EventListener;
9+
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Profile;
10+
use Patchlevel\EventSourcingBundle\Tests\Fixtures\ProfileCreated;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class EventCollectorTest extends TestCase
16+
{
17+
public function testCollectData(): void
18+
{
19+
$eventListener = new EventListener();
20+
21+
$collector = new EventCollector(
22+
$eventListener,
23+
[Profile::class => 'profile']
24+
);
25+
26+
$event = ProfileCreated::raise('foo');
27+
$eventListener($event);
28+
29+
$collector->collect(
30+
new Request(),
31+
new Response()
32+
);
33+
34+
self::assertSame([Profile::class => 'profile'], $collector->getAggregates());
35+
self::assertCount(1, $collector->getEvents());
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcingBundle\Tests\Unit\Loader;
6+
7+
use InvalidArgumentException;
8+
use Patchlevel\EventSourcingBundle\Loader\AggregateAttributesLoader;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class AggregateAttributeLoaderTest extends TestCase
12+
{
13+
public function testDuplicateAggregateName()
14+
{
15+
$this->expectException(InvalidArgumentException::class);
16+
17+
$loader = new AggregateAttributesLoader();
18+
$loader->load([
19+
__DIR__ . '/../../Fixtures/AttributedAggregatesSameName'
20+
]);
21+
}
22+
23+
public function testLoadPathWithAttributedAggregates()
24+
{
25+
$loader = new AggregateAttributesLoader();
26+
$result = $loader->load([
27+
__DIR__ . '/../../Fixtures/AttributedAggregates'
28+
]);
29+
30+
self::assertCount(2, $result);
31+
}
32+
}

0 commit comments

Comments
 (0)