Completed
Push — master ( 269c38...74a7a2 )
by Hugues
02:49
created

UserCommandsTest::emailValidationCanBeRequested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\Event\EmailChanged;
16
use HMLB\UserBundle\Event\EmailConfirmed;
17
use HMLB\UserBundle\Event\EmailValidationRequested;
18
use HMLB\UserBundle\Event\PasswordChanged;
19
use HMLB\UserBundle\Event\UserRegistered;
20
use HMLB\UserBundle\User\Role;
21
use HMLB\UserBundle\User\User;
22
use HMLB\UserBundle\User\UserRepository;
23
24
/**
25
 * RegisterUserTest.
26
 *
27
 * @author Hugues Maignol <[email protected]>
28
 */
29
class UserCommandsTest extends AbstractCommandTest
30
{
31
    /**
32
     * @test
33
     */
34
    public function usersCanRegister()
35
    {
36
        $command = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
37
38
        $this->handleCommandAndAssertTraced(
39
            $this->getCommandBus(),
40
            $command,
41
            $this->getCommandRepository()
42
        );
43
44
        /** @var UserRegistered $event */
45
        $event = $this->getEvent(UserRegistered::class);
46
        $this->assertEquals($command->getUsername(), $event->getUsername());
47
48
        /** @var User $user */
49
        $user = $this->container->get('hmlb_user.repository.user')->get($event->getUserId());
50
        $this->assertInstanceOf(User::class, $user);
51
        $this->assertEquals('test', $user->getUsername());
52
        $this->assertEquals('[email protected]', $user->getEmail());
53
    }
54
55
    /**
56
     * @test
57
     */
58
    public function usersCanChangePassword()
59
    {
60
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
61
        $this->getCommandBus()->handle($register);
62
63
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
64
        $beginningPwd = $user->getPassword();
65
        $command = new ChangePassword('456', $user->getId());
66
67
        $this->handleCommandAndAssertTraced(
68
            $this->getCommandBus(),
69
            $command,
70
            $this->getCommandRepository()
71
        );
72
        $endPwd = $user->getPassword();
73
74
        $this->assertNotEquals($beginningPwd, $endPwd);
75
76
        /** @var PasswordChanged $event */
77
        $event = $this->getEvent(PasswordChanged::class);
78
        $this->assertEquals($command->getUserId(), $event->getUserId());
79
        $this->assertNotEquals($event->getOldPassword(), $event->getNewPassword());
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function usersCanChangeEmail()
86
    {
87
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
88
        $this->getCommandBus()->handle($register);
89
90
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
91
        $beforeEmail = $user->getEmail();
92
        $command = new ChangeEmail('[email protected]', $user->getId());
93
94
        $commandRepo = $this->getCommandRepository();
95
        $this->handleCommandAndAssertTraced(
96
            $this->getCommandBus(),
97
            $command,
98
            $commandRepo
99
        );
100
        $afterEmail = $user->getEmail();
101
102
        $this->assertNotEquals($beforeEmail, $afterEmail);
103
        $this->assertEquals('[email protected]', $afterEmail);
104
105
        /** @var EmailChanged $event */
106
        $event = $this->getEvent(EmailChanged::class);
107
108
        $this->assertEquals($beforeEmail, $event->getOldEmail());
109
        $this->assertEquals($afterEmail, $event->getNewEmail());
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function usersCanValidateEmail()
116
    {
117
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
118
        $this->getCommandBus()->handle($register);
119
120
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
121
        $this->assertFalse($user->isEmailConfirmed());
122
        $command = new ConfirmEmail($user->getConfirmationToken(), $user->getId());
123
124
        $this->handleCommandAndAssertTraced(
125
            $this->getCommandBus(),
126
            $command,
127
            $this->getCommandRepository()
128
        );
129
130
        $this->assertTrue($user->isEmailConfirmed());
131
132
        /** @var EmailConfirmed $event */
133
        $event = $this->getEvent(EmailConfirmed::class);
134
        $this->assertEquals($user->getId(), $event->getUserId());
135
        $this->assertEquals($user->getEmail(), $event->getEmail());
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function emailValidationCanBeRequested()
142
    {
143
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
144
        $this->getCommandBus()->handle($register);
145
146
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
147
        $confirm = new ConfirmEmail($user->getConfirmationToken(), $user->getId());
148
        $this->getCommandBus()->handle($confirm);
149
        $this->assertTrue($user->isEmailConfirmed());
150
151
        $command = new RequestEmailValidation($user->getId());
152
153
        $commandRepo = $this->getCommandRepository();
154
        $this->handleCommandAndAssertTraced(
155
            $this->getCommandBus(),
156
            $command,
157
            $commandRepo
158
        );
159
160
        $this->assertFalse($user->isEmailConfirmed());
161
162
        /** @var EmailValidationRequested $event */
163
        $event = $this->getEvent(EmailValidationRequested::class);
164
        $this->assertEquals($user->getId(), $event->getUserId());
165
        $this->assertEquals($user->getConfirmationToken(), $event->getValidationToken());
166
    }
167
168
    /**
169
     * @param $eventClassName
170
     *
171
     * @return PersistentEvent
172
     */
173
    private function getEvent($eventClassName)
174
    {
175
        $eventRepository = $this->getEventRepository();
176
177
        $foundEvents = $eventRepository->getByMessage($eventClassName);
178
        $this->assertCount(1, $foundEvents);
179
180
        $event = $foundEvents[0];
181
        $this->assertInstanceOf($eventClassName, $event);
182
183
        return $event;
184
    }
185
186
    private function getUserRepository(): UserRepository
187
    {
188
        return $this->container->get('hmlb_user.repository.user');
189
    }
190
191
    private function getCommandRepository(): PersistentCommandRepository
192
    {
193
        return $this->container->get('hmlb_ddd.repository.command');
194
    }
195
196
    private function getEventRepository(): PersistentEventRepository
197
    {
198
        return $this->container->get('hmlb_ddd.repository.event');
199
    }
200
}
201