1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Tests\Unit\EventsRecorder; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
9
|
|
|
use Doctrine\ORM\UnitOfWork; |
10
|
|
|
use Dsantang\DomainEvents\DomainEvent; |
11
|
|
|
use Dsantang\DomainEvents\EventAware; |
12
|
|
|
use Dsantang\DomainEvents\Registry\EventsRegistry; |
13
|
|
|
use Dsantang\DomainEventsDoctrine\Aggregator; |
14
|
|
|
use Dsantang\DomainEventsDoctrine\EventsRecorder\DoctrineEventsRecorder; |
15
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\RandomDomainEvent; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
final class DoctrineEventsRecorderTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param DomainEvent[] $domainEvents |
22
|
|
|
* |
23
|
|
|
* @dataProvider provideEventAwareChangedEntities |
24
|
|
|
* |
25
|
|
|
* @test |
26
|
|
|
*/ |
27
|
|
|
public function onFlushWillAggregateAllTheEventAwareEntities(object $eventAwareEntity, array $domainEvents): void |
28
|
|
|
{ |
29
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class); |
30
|
|
|
|
31
|
|
|
$unitOfWork->expects(self::once()) |
32
|
|
|
->method('getScheduledEntityDeletions') |
33
|
|
|
->willReturn([]); |
34
|
|
|
|
35
|
|
|
$unitOfWork->expects(self::once()) |
36
|
|
|
->method('getScheduledEntityInsertions') |
37
|
|
|
->willReturn([]); |
38
|
|
|
|
39
|
|
|
$unitOfWork->expects(self::once()) |
40
|
|
|
->method('getScheduledEntityUpdates') |
41
|
|
|
->willReturn([$eventAwareEntity]); |
42
|
|
|
|
43
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
44
|
|
|
$entityManager->expects(self::once()) |
45
|
|
|
->method('getUnitOfWork') |
46
|
|
|
->willReturn($unitOfWork); |
47
|
|
|
|
48
|
|
|
$aggregator = $this->createMock(Aggregator::class); |
49
|
|
|
|
50
|
|
|
$aggregator->expects(self::once()) |
51
|
|
|
->method('aggregate') |
52
|
|
|
->with(...$domainEvents); |
53
|
|
|
|
54
|
|
|
$eventRecorder = new DoctrineEventsRecorder($aggregator); |
55
|
|
|
|
56
|
|
|
$eventArgs = $this->createMock(OnFlushEventArgs::class); |
57
|
|
|
$eventArgs->expects(self::once()) |
58
|
|
|
->method('getEntityManager') |
59
|
|
|
->willReturn($entityManager); |
60
|
|
|
|
61
|
|
|
$eventRecorder->onFlush($eventArgs); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return mixed[] array |
66
|
|
|
*/ |
67
|
|
|
public function provideEventAwareChangedEntities(): array |
68
|
|
|
{ |
69
|
|
|
$awareEntity = new class (new RandomDomainEvent()) implements EventAware { |
70
|
|
|
use EventsRegistry; |
71
|
|
|
|
72
|
|
|
public function __construct(DomainEvent $domainEvent) |
73
|
|
|
{ |
74
|
|
|
$this->recordedEvents = [$domainEvent]; |
75
|
|
|
} |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
$object = new class (){ |
79
|
|
|
}; |
80
|
|
|
|
81
|
|
|
return [ |
82
|
|
|
'with no changed entities' => [$object, []], |
83
|
|
|
'with a changed entity' => [$awareEntity, [new RandomDomainEvent()]], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|