UserCommandsTest::getCommandRepository()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace HMLB\UserBundle\Tests\Command;
6
7
use HMLB\DDD\Message\Event\PersistentEvent;
8
use HMLB\DDDBundle\Repository\PersistentCommandRepository;
9
use HMLB\DDDBundle\Repository\PersistentEventRepository;
10
use HMLB\UserBundle\Command\ChangeEmail;
11
use HMLB\UserBundle\Command\ChangePassword;
12
use HMLB\UserBundle\Command\ConfirmEmail;
13
use HMLB\UserBundle\Command\RegisterUser;
14
use HMLB\UserBundle\Command\RequestEmailValidation;
15
use HMLB\UserBundle\Command\RequestPasswordReset;
16
use HMLB\UserBundle\Command\ResetPassword;
17
use HMLB\UserBundle\Event\EmailChanged;
18
use HMLB\UserBundle\Event\EmailConfirmed;
19
use HMLB\UserBundle\Event\EmailValidationRequested;
20
use HMLB\UserBundle\Event\PasswordChanged;
21
use HMLB\UserBundle\Event\PasswordReset;
22
use HMLB\UserBundle\Event\PasswordResetRequested;
23
use HMLB\UserBundle\Event\UserRegistered;
24
use HMLB\UserBundle\User\Role;
25
use HMLB\UserBundle\User\User;
26
use HMLB\UserBundle\User\UserRepository;
27
28
/**
29
 * RegisterUserTest.
30
 *
31
 * @author Hugues Maignol <[email protected]>
32
 */
33
class UserCommandsTest extends AbstractCommandTest
34
{
35
    /**
36
     * @test
37
     */
38
    public function usersCanRegister()
39
    {
40
        $command = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
41
42
        $this->handleCommandAndAssertTraced(
43
            $this->getCommandBus(),
44
            $command,
45
            $this->getCommandRepository()
46
        );
47
48
        /** @var UserRegistered $event */
49
        $event = $this->getEvent(UserRegistered::class);
50
        $this->assertEquals($command->getUsername(), $event->getUsername());
51
52
        /** @var User $user */
53
        $user = $this->container->get('hmlb_user.repository.user')->get($event->getUserId());
54
        $this->assertInstanceOf(User::class, $user);
55
        $this->assertEquals('test', $user->getUsername());
56
        $this->assertEquals('[email protected]', $user->getEmail());
57
    }
58
59
    /**
60
     * @test
61
     */
62
    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
        $this->handleCommandAndAssertTraced(
72
            $this->getCommandBus(),
73
            $command,
74
            $this->getCommandRepository()
75
        );
76
        $endPwd = $user->getPassword();
77
78
        $this->assertNotEquals($beginningPwd, $endPwd);
79
80
        /** @var PasswordChanged $event */
81
        $event = $this->getEvent(PasswordChanged::class);
82
        $this->assertEquals($command->getUserId(), $event->getUserId());
83
        $this->assertNotEquals($event->getOldPassword(), $event->getNewPassword());
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function usersCanChangeEmail()
90
    {
91
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
92
        $this->getCommandBus()->handle($register);
93
94
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
95
        $beforeEmail = $user->getEmail();
96
        $command = new ChangeEmail('[email protected]', $user->getId());
97
98
        $commandRepo = $this->getCommandRepository();
99
        $this->handleCommandAndAssertTraced(
100
            $this->getCommandBus(),
101
            $command,
102
            $commandRepo
103
        );
104
        $afterEmail = $user->getEmail();
105
106
        $this->assertNotEquals($beforeEmail, $afterEmail);
107
        $this->assertEquals('[email protected]', $afterEmail);
108
109
        /** @var EmailChanged $event */
110
        $event = $this->getEvent(EmailChanged::class);
111
112
        $this->assertEquals($beforeEmail, $event->getOldEmail());
113
        $this->assertEquals($afterEmail, $event->getNewEmail());
114
    }
115
116
    /**
117
     * @test
118
     */
119
    public function usersCanValidateEmail()
120
    {
121
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
122
        $this->getCommandBus()->handle($register);
123
124
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
125
        $this->assertFalse($user->isEmailConfirmed());
126
        $command = new ConfirmEmail($user->getConfirmationToken(), $user->getId());
127
128
        $this->handleCommandAndAssertTraced(
129
            $this->getCommandBus(),
130
            $command,
131
            $this->getCommandRepository()
132
        );
133
134
        $this->assertTrue($user->isEmailConfirmed());
135
136
        /** @var EmailConfirmed $event */
137
        $event = $this->getEvent(EmailConfirmed::class);
138
        $this->assertEquals($user->getId(), $event->getUserId());
139
        $this->assertEquals($user->getEmail(), $event->getEmail());
140
    }
141
142
    /**
143
     * @test
144
     */
145 View Code Duplication
    public function emailValidationCanBeRequested()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
146
    {
147
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
148
        $this->getCommandBus()->handle($register);
149
150
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
151
        $confirm = new ConfirmEmail($user->getConfirmationToken(), $user->getId());
152
        $this->getCommandBus()->handle($confirm);
153
        $this->assertTrue($user->isEmailConfirmed());
154
155
        $command = new RequestEmailValidation($user->getId());
156
157
        $this->handleCommandAndAssertTraced(
158
            $this->getCommandBus(),
159
            $command,
160
            $this->getCommandRepository()
161
        );
162
163
        $this->assertFalse($user->isEmailConfirmed());
164
165
        /** @var EmailValidationRequested $event */
166
        $event = $this->getEvent(EmailValidationRequested::class);
167
        $this->assertEquals($user->getId(), $event->getUserId());
168
        $this->assertEquals($user->getConfirmationToken(), $event->getValidationToken());
169
    }
170
171
    /**
172
     * @test
173
     */
174 View Code Duplication
    public function passwordResetCanBeRequested()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
175
    {
176
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
177
        $this->getCommandBus()->handle($register);
178
179
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
180
181
        $this->assertFalse($user->isPasswordResetRequested());
182
        $this->assertEmpty($user->getResettingToken());
183
184
        $command = new RequestPasswordReset($user->getId());
185
186
        $this->handleCommandAndAssertTraced(
187
            $this->getCommandBus(),
188
            $command,
189
            $this->getCommandRepository()
190
        );
191
192
        $this->assertTrue($user->isPasswordResetRequested());
193
        $this->assertNotEmpty($user->getResettingToken());
194
195
        /** @var PasswordResetRequested $event */
196
        $event = $this->getEvent(PasswordResetRequested::class);
197
        $this->assertEquals($user->getId(), $event->getUserId());
198
        $this->assertEquals($user->getResettingToken(), $event->getResetToken());
199
    }
200
201
    /**
202
     * @test
203
     */
204
    public function passwordCanBeReset()
205
    {
206
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
207
        $this->getCommandBus()->handle($register);
208
209
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
210
211
        $request = new RequestPasswordReset($user->getId());
212
        $this->getCommandBus()->handle($request);
213
214
        $command = new ResetPassword('456', $user->getResettingToken(), $user->getId());
215
216
        $beginningPwd = $user->getPassword();
217
        $this->handleCommandAndAssertTraced(
218
            $this->getCommandBus(),
219
            $command,
220
            $this->getCommandRepository()
221
        );
222
223
        $endPwd = $user->getPassword();
224
225
        $this->assertFalse($user->isPasswordResetRequested());
226
        $this->assertEmpty($user->getResettingToken());
227
        $this->assertNotEquals($beginningPwd, $endPwd);
228
229
        /** @var PasswordReset $event */
230
        $event = $this->getEvent(PasswordReset::class);
231
        $this->assertEquals($user->getId(), $event->getUserId());
232
        $this->assertEquals($beginningPwd, $event->getOldPassword());
233
        $this->assertEquals($endPwd, $event->getNewPassword());
234
    }
235
236
    /**
237
     * @param $eventClassName
238
     *
239
     * @return PersistentEvent
240
     */
241
    private function getEvent($eventClassName)
242
    {
243
        $eventRepository = $this->getEventRepository();
244
245
        $foundEvents = $eventRepository->getByMessage($eventClassName);
246
        $this->assertCount(1, $foundEvents);
247
248
        $event = $foundEvents[0];
249
        $this->assertInstanceOf($eventClassName, $event);
250
251
        return $event;
252
    }
253
254
    private function getUserRepository(): UserRepository
255
    {
256
        return $this->container->get('hmlb_user.repository.user');
257
    }
258
259
    private function getCommandRepository(): PersistentCommandRepository
260
    {
261
        return $this->container->get('hmlb_ddd.repository.command');
262
    }
263
264
    private function getEventRepository(): PersistentEventRepository
265
    {
266
        return $this->container->get('hmlb_ddd.repository.event');
267
    }
268
}
269