forked from broadway/broadway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvitationCommandHandlerTest.php
131 lines (112 loc) · 3.52 KB
/
InvitationCommandHandlerTest.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
<?php
require_once __DIR__ . '/Invites.php';
/**
* We drive the tests of our aggregate root through the command handler.
*
* A command handler scenario consists of three steps:
*
* - First, the scenario is setup with a history of events that already took place.
* - Second, a command is dispatched (this is handled by the command handler)
* - Third, the outcome is asserted. This can either be 1) some events are
* recorded, or 2) an exception is thrown.
*/
class InvitationCommandHandlerTest extends Broadway\CommandHandling\Testing\CommandHandlerScenarioTestCase
{
private $generator;
public function setUp()
{
parent::setUp();
$this->generator = new Broadway\UuidGenerator\Rfc4122\Version4Generator();
}
protected function createCommandHandler(Broadway\EventStore\EventStoreInterface $eventStore, Broadway\EventHandling\EventBusInterface $eventBus)
{
$repository = new InvitationRepository($eventStore, $eventBus);
return new InvitationCommandHandler($repository);
}
/**
* @test
*/
public function it_can_invite_someone()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([])
->when(new InviteCommand($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(new AcceptCommand($id))
->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(new AcceptCommand($id))
->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(new DeclineCommand($id));
}
/**
* @test
*/
public function new_invites_can_be_declined()
{
$id = $this->generator->generate();
$this->scenario
->withAggregateId($id)
->given([new InvitedEvent($id, 'asm89')])
->when(new DeclineCommand($id))
->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(new DeclineCommand($id))
->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(new AcceptCommand($id));
}
}