Completed
Push — master ( ce13b1...d3482a )
by Hugues
02:12
created

UserCommandsTest::usersCanChangeEmail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 34
Ratio 100 %
Metric Value
dl 34
loc 34
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
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\RegisterUser;
10
use HMLB\UserBundle\Event\EmailChanged;
11
use HMLB\UserBundle\Event\PasswordChanged;
12
use HMLB\UserBundle\Event\UserRegistered;
13
use HMLB\UserBundle\User\Role;
14
use HMLB\UserBundle\User\UserRepository;
15
16
/**
17
 * RegisterUserTest.
18
 *
19
 * @author Hugues Maignol <[email protected]>
20
 */
21
class UserCommandsTest extends AbstractCommandTest
22
{
23
    /**
24
     * @test
25
     */
26
    public function usersCanRegister()
27
    {
28
        $command = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
29
30
        /** @var PersistentCommandRepository $commandRepo */
31
        $commandRepo = $this->container->get('hmlb_ddd.repository.command');
32
        $this->handleCommandAndAssertTraced(
33
            $this->getCommandBus(),
34
            $command,
35
            $commandRepo
36
        );
37
38
        $foundCommands = $commandRepo->getByMessage(RegisterUser::class);
39
        $this->assertCount(1, $foundCommands);
40
        $this->assertSame($command, $foundCommands[0]);
41
42
        /** @var PersistentEventRepository $eventRepository */
43
        $eventRepository = $this->container->get('hmlb_ddd.repository.event');
44
45
        $foundEvents = $eventRepository->getByMessage(UserRegistered::class);
46
        $this->assertCount(1, $foundEvents);
47
48
        /** @var UserRegistered $event */
49
        $event = $foundEvents[0];
50
        $this->assertInstanceOf(UserRegistered::class, $event);
51
        $this->assertEquals($command->getUsername(), $event->getUsername());
52
53
        $foundEvent = $eventRepository->get($event->getId());
54
        $this->assertSame($event, $foundEvent);
55
    }
56
57
    /**
58
     * @test
59
     */
60 View Code Duplication
    public function usersCanChangePassword()
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...
61
    {
62
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
63
        $this->getCommandBus()->handle($register);
64
65
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
66
        $beginningPwd = $user->getPassword();
67
        $command = new ChangePassword('456', $user->getId());
68
69
        $commandRepo = $this->getCommandRepository();
70
        $this->handleCommandAndAssertTraced(
71
            $this->getCommandBus(),
72
            $command,
73
            $commandRepo
74
        );
75
        $endPwd = $user->getPassword();
76
77
        $this->assertNotEquals($beginningPwd, $endPwd);
78
79
        $eventRepository = $this->getEventRepository();
80
81
        $foundEvents = $eventRepository->getByMessage(PasswordChanged::class);
82
        $this->assertCount(1, $foundEvents);
83
84
        /** @var PasswordChanged $event */
85
        $event = $foundEvents[0];
86
        $this->assertInstanceOf(PasswordChanged::class, $event);
87
        $this->assertEquals($command->getUserId(), $event->getUserId());
88
        $this->assertNotEquals($event->getOldPassword(), $event->getNewPassword());
89
90
        $foundEvent = $eventRepository->get($event->getId());
91
        $this->assertSame($event, $foundEvent);
92
    }
93
94
    /**
95
     * @test
96
     */
97 View Code Duplication
    public function usersCanChangeEmail()
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...
98
    {
99
        $register = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
100
        $this->getCommandBus()->handle($register);
101
102
        $user = $this->getUserRepository()->getByUsername($register->getUsername());
103
        $beforeEmail = $user->getEmail();
104
        $command = new ChangeEmail('[email protected]', $user->getId());
105
106
        $commandRepo = $this->getCommandRepository();
107
        $this->handleCommandAndAssertTraced(
108
            $this->getCommandBus(),
109
            $command,
110
            $commandRepo
111
        );
112
        $afterEmail = $user->getEmail();
113
114
        $this->assertNotEquals($beforeEmail, $afterEmail);
115
        $this->assertEquals('[email protected]', $afterEmail);
116
117
        $eventRepository = $this->getEventRepository();
118
119
        $foundEvents = $eventRepository->getByMessage(EmailChanged::class);
120
        $this->assertCount(1, $foundEvents);
121
122
        /** @var EmailChanged $event */
123
        $event = $foundEvents[0];
124
        $this->assertInstanceOf(EmailChanged::class, $event);
125
        $this->assertEquals($beforeEmail, $event->getOldEmail());
126
        $this->assertEquals($afterEmail, $event->getNewEmail());
127
128
        $foundEvent = $eventRepository->get($event->getId());
129
        $this->assertSame($event, $foundEvent);
130
    }
131
132
    private function getUserRepository(): UserRepository
133
    {
134
        return $this->container->get('hmlb_user.repository.user');
135
    }
136
137
    private function getCommandRepository(): PersistentCommandRepository
138
    {
139
        return $this->container->get('hmlb_ddd.repository.command');
140
    }
141
142
    private function getEventRepository(): PersistentEventRepository
143
    {
144
        return $this->container->get('hmlb_ddd.repository.event');
145
    }
146
}
147