1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\UserBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use HMLB\DDDBundle\Repository\PersistentCommandRepository; |
6
|
|
|
use HMLB\DDDBundle\Repository\PersistentEventRepository; |
7
|
|
|
use HMLB\UserBundle\Command\ChangeEmail; |
8
|
|
|
use HMLB\UserBundle\Command\ChangePassword; |
9
|
|
|
use HMLB\UserBundle\Command\ConfirmEmail; |
10
|
|
|
use HMLB\UserBundle\Command\RegisterUser; |
11
|
|
|
use HMLB\UserBundle\Event\EmailChanged; |
12
|
|
|
use HMLB\UserBundle\Event\EmailConfirmed; |
13
|
|
|
use HMLB\UserBundle\Event\PasswordChanged; |
14
|
|
|
use HMLB\UserBundle\Event\UserRegistered; |
15
|
|
|
use HMLB\UserBundle\User\Role; |
16
|
|
|
use HMLB\UserBundle\User\UserRepository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* RegisterUserTest. |
20
|
|
|
* |
21
|
|
|
* @author Hugues Maignol <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class UserCommandsTest extends AbstractCommandTest |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
|
public function usersCanRegister() |
29
|
|
|
{ |
30
|
|
|
$command = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]); |
31
|
|
|
|
32
|
|
|
/** @var PersistentCommandRepository $commandRepo */ |
33
|
|
|
$commandRepo = $this->container->get('hmlb_ddd.repository.command'); |
34
|
|
|
$this->handleCommandAndAssertTraced( |
35
|
|
|
$this->getCommandBus(), |
36
|
|
|
$command, |
37
|
|
|
$commandRepo |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$foundCommands = $commandRepo->getByMessage(RegisterUser::class); |
41
|
|
|
$this->assertCount(1, $foundCommands); |
42
|
|
|
$this->assertSame($command, $foundCommands[0]); |
43
|
|
|
|
44
|
|
|
/** @var PersistentEventRepository $eventRepository */ |
45
|
|
|
$eventRepository = $this->container->get('hmlb_ddd.repository.event'); |
46
|
|
|
|
47
|
|
|
$foundEvents = $eventRepository->getByMessage(UserRegistered::class); |
48
|
|
|
$this->assertCount(1, $foundEvents); |
49
|
|
|
|
50
|
|
|
/** @var UserRegistered $event */ |
51
|
|
|
$event = $foundEvents[0]; |
52
|
|
|
$this->assertInstanceOf(UserRegistered::class, $event); |
53
|
|
|
$this->assertEquals($command->getUsername(), $event->getUsername()); |
54
|
|
|
|
55
|
|
|
$foundEvent = $eventRepository->get($event->getId()); |
56
|
|
|
$this->assertSame($event, $foundEvent); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function usersCanChangePassword() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]); |
65
|
|
|
$this->getCommandBus()->handle($register); |
66
|
|
|
|
67
|
|
|
$user = $this->getUserRepository()->getByUsername($register->getUsername()); |
68
|
|
|
$beginningPwd = $user->getPassword(); |
69
|
|
|
$command = new ChangePassword('456', $user->getId()); |
70
|
|
|
|
71
|
|
|
$commandRepo = $this->getCommandRepository(); |
72
|
|
|
$this->handleCommandAndAssertTraced( |
73
|
|
|
$this->getCommandBus(), |
74
|
|
|
$command, |
75
|
|
|
$commandRepo |
76
|
|
|
); |
77
|
|
|
$endPwd = $user->getPassword(); |
78
|
|
|
|
79
|
|
|
$this->assertNotEquals($beginningPwd, $endPwd); |
80
|
|
|
|
81
|
|
|
$eventRepository = $this->getEventRepository(); |
82
|
|
|
|
83
|
|
|
$foundEvents = $eventRepository->getByMessage(PasswordChanged::class); |
84
|
|
|
$this->assertCount(1, $foundEvents); |
85
|
|
|
|
86
|
|
|
/** @var PasswordChanged $event */ |
87
|
|
|
$event = $foundEvents[0]; |
88
|
|
|
$this->assertInstanceOf(PasswordChanged::class, $event); |
89
|
|
|
$this->assertEquals($command->getUserId(), $event->getUserId()); |
90
|
|
|
$this->assertNotEquals($event->getOldPassword(), $event->getNewPassword()); |
91
|
|
|
|
92
|
|
|
$foundEvent = $eventRepository->get($event->getId()); |
93
|
|
|
$this->assertSame($event, $foundEvent); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function usersCanChangeEmail() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]); |
102
|
|
|
$this->getCommandBus()->handle($register); |
103
|
|
|
|
104
|
|
|
$user = $this->getUserRepository()->getByUsername($register->getUsername()); |
105
|
|
|
$beforeEmail = $user->getEmail(); |
106
|
|
|
$command = new ChangeEmail('[email protected]', $user->getId()); |
107
|
|
|
|
108
|
|
|
$commandRepo = $this->getCommandRepository(); |
109
|
|
|
$this->handleCommandAndAssertTraced( |
110
|
|
|
$this->getCommandBus(), |
111
|
|
|
$command, |
112
|
|
|
$commandRepo |
113
|
|
|
); |
114
|
|
|
$afterEmail = $user->getEmail(); |
115
|
|
|
|
116
|
|
|
$this->assertNotEquals($beforeEmail, $afterEmail); |
117
|
|
|
$this->assertEquals('[email protected]', $afterEmail); |
118
|
|
|
|
119
|
|
|
$eventRepository = $this->getEventRepository(); |
120
|
|
|
|
121
|
|
|
$foundEvents = $eventRepository->getByMessage(EmailChanged::class); |
122
|
|
|
$this->assertCount(1, $foundEvents); |
123
|
|
|
|
124
|
|
|
/** @var EmailChanged $event */ |
125
|
|
|
$event = $foundEvents[0]; |
126
|
|
|
$this->assertInstanceOf(EmailChanged::class, $event); |
127
|
|
|
$this->assertEquals($beforeEmail, $event->getOldEmail()); |
128
|
|
|
$this->assertEquals($afterEmail, $event->getNewEmail()); |
129
|
|
|
|
130
|
|
|
$foundEvent = $eventRepository->get($event->getId()); |
131
|
|
|
$this->assertSame($event, $foundEvent); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @test |
136
|
|
|
*/ |
137
|
|
|
public function usersCanValidateEmail() |
138
|
|
|
{ |
139
|
|
|
$register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]); |
140
|
|
|
$this->getCommandBus()->handle($register); |
141
|
|
|
|
142
|
|
|
$user = $this->getUserRepository()->getByUsername($register->getUsername()); |
143
|
|
|
$this->assertFalse($user->isEmailConfirmed()); |
144
|
|
|
$command = new ConfirmEmail($user->getConfirmationToken(), $user->getId()); |
145
|
|
|
|
146
|
|
|
$commandRepo = $this->getCommandRepository(); |
147
|
|
|
$this->handleCommandAndAssertTraced( |
148
|
|
|
$this->getCommandBus(), |
149
|
|
|
$command, |
150
|
|
|
$commandRepo |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->assertTrue($user->isEmailConfirmed()); |
154
|
|
|
|
155
|
|
|
$eventRepository = $this->getEventRepository(); |
156
|
|
|
|
157
|
|
|
$foundEvents = $eventRepository->getByMessage(EmailConfirmed::class); |
158
|
|
|
$this->assertCount(1, $foundEvents); |
159
|
|
|
|
160
|
|
|
/** @var EmailConfirmed $event */ |
161
|
|
|
$event = $foundEvents[0]; |
162
|
|
|
$this->assertInstanceOf(EmailConfirmed::class, $event); |
163
|
|
|
$this->assertEquals($user->getId(), $event->getUserId()); |
164
|
|
|
$this->assertEquals($user->getEmail(), $event->getEmail()); |
165
|
|
|
|
166
|
|
|
$foundEvent = $eventRepository->get($event->getId()); |
167
|
|
|
$this->assertSame($event, $foundEvent); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function getUserRepository(): UserRepository |
171
|
|
|
{ |
172
|
|
|
return $this->container->get('hmlb_user.repository.user'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function getCommandRepository(): PersistentCommandRepository |
176
|
|
|
{ |
177
|
|
|
return $this->container->get('hmlb_ddd.repository.command'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function getEventRepository(): PersistentEventRepository |
181
|
|
|
{ |
182
|
|
|
return $this->container->get('hmlb_ddd.repository.event'); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.