forked from broadway/broadway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvitesTest.php
140 lines (122 loc) · 3.5 KB
/
InvitesTest.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
require_once __DIR__ . '/Invites.php';
/**.
*
* An aggregate root scenario consists of three steps:
*
* - First, the scenario is setup with a history of events that already took place.
* - Second, an action is taken on the aggregate.
* - Third, the outcome is asserted. This can either be 1) some events are
* recorded, or 2) an exception is thrown.
*/
class InvitationTest extends Broadway\EventSourcing\Testing\AggregateRootScenarioTestCase
{
private $generator;
public function setUp()
{
parent::setUp();
$this->generator = new Broadway\UuidGenerator\Rfc4122\Version4Generator();
}
protected function getAggregateRootClass()
{
return Invitation::class;
}
/**
* @test
*/
public function it_can_invite_someone()
{
$id = $this->generator->generate();
$this->scenario
->when(function () use ($id) {
return Invitation::invite($id, 'asm89');
})
->then([new InvitedEvent($id, 'asm89')]);
}
/**
* @test
*/
public function new_invites_can_be_accepted()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89')])
->when(function ($invite) {
$invite->accept();
})
->then([new AcceptedEvent($id)]);
}
/**
* @test
*/
public function accepting_an_accepted_invite_yields_no_change()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89'), new AcceptedEvent($id)])
->when(function ($aggregate) {
$aggregate->accept();
})
->then([]);
}
/**
* @test
* @expectedException RuntimeException
* @expectedExceptionMessage Already accepted.
*/
public function an_accepted_invite_cannot_be_declined()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89'), new AcceptedEvent($id)])
->when(function ($invite) {
$invite->decline();
});
}
/**
* @test
*/
public function new_invites_can_be_declined()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89')])
->when(function ($invite) {
$invite->decline();
})
->then([new DeclinedEvent($id)]);
}
/**
* @test
*/
public function declining_a_declined_invite_yields_no_change()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89'), new DeclinedEvent($id)])
->when(function ($invite) {
$invite->decline();
})
->then([]);
}
/**
* @test
* @expectedException RuntimeException
* @expectedExceptionMessage Already declined.
*/
public function a_declined_invite_cannot_be_accepted()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89'), new DeclinedEvent($id)])
->when(function ($invite) {
$invite->accept();
});
}
}