-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathEventSourcingAggregateRootTest.php
More file actions
100 lines (81 loc) · 2.02 KB
/
EventSourcingAggregateRootTest.php
File metadata and controls
100 lines (81 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Ddd\Domain\EventSourcing;
use Ddd\Domain\DomainEventPublisher;
use Ddd\Domain\FakeDomainEvent;
use Ddd\Domain\SpySubscriber;
class EventSourcingAggregateRootTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SpyEventSourcingAggregateRoot
*/
private $aggregateRoot;
/**
* @var SpySubscriber
*/
private $subscriber;
public function setUp()
{
$this->aggregateRoot = new SpyEventSourcingAggregateRoot();
$this->subscriber = new SpySubscriber('fake-event');
DomainEventPublisher::instance()->subscribe($this->subscriber);
}
/**
* @test
*/
public function itShouldPublishDomainEvent()
{
$this->aggregateRoot->someBusinessLogic();
$this->assertTrue($this->subscriber->isHandled);
}
/**
* @test
*/
public function itShouldApplyDomainEvent()
{
$this->aggregateRoot->someBusinessLogic();
$this->assertTrue($this->aggregateRoot->isApplied());
}
/**
* @test
*/
public function itShouldRecordDomainEvent()
{
$this->aggregateRoot->someBusinessLogic();
$this->assertEquals(
new FakeDomainEvent('fake-event'),
$this->aggregateRoot->recordedEvents()[0]
);
}
/**
* @test
*/
public function itShouldClearRecordedDomainEvent()
{
$this->aggregateRoot->someBusinessLogic();
$this->aggregateRoot->clearRecordedEvents();
$this->assertEmpty($this->aggregateRoot->recordedEvents());
}
}
class SpyEventSourcingAggregateRoot
{
use EventSourcingAggregateRoot;
/**
* @var bool
*/
private $isApplied = false;
public function someBusinessLogic()
{
$this->publishThat(new FakeDomainEvent('fake-event'));
}
protected function applyFakeDomainEvent(FakeDomainEvent $domainEvent)
{
$this->isApplied = true;
}
/**
* @return boolean
*/
public function isApplied()
{
return $this->isApplied;
}
}