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

UserCommandsTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12
Metric Value
wmc 5
lcom 1
cbo 12
dl 0
loc 88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B usersCanRegister() 0 30 1
B usersCanChangePassword() 0 33 1
A getUserRepository() 0 4 1
A getCommandRepository() 0 4 1
A getEventRepository() 0 4 1
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\ChangePassword;
8
use HMLB\UserBundle\Command\RegisterUser;
9
use HMLB\UserBundle\Event\PasswordChanged;
10
use HMLB\UserBundle\Event\UserRegistered;
11
use HMLB\UserBundle\User\Role;
12
use HMLB\UserBundle\User\UserRepository;
13
14
/**
15
 * RegisterUserTest.
16
 *
17
 * @author Hugues Maignol <[email protected]>
18
 */
19
class UserCommandsTest extends AbstractCommandTest
20
{
21
    /**
22
     * @test
23
     */
24
    public function usersCanRegister()
25
    {
26
        $command = new RegisterUser('test', '[email protected]', '123', [new Role('ROLE_USER')]);
27
28
        /** @var PersistentCommandRepository $commandRepo */
29
        $commandRepo = $this->container->get('hmlb_ddd.repository.command');
30
        $this->handleCommandAndAssertTraced(
31
            $this->getCommandBus(),
32
            $command,
33
            $commandRepo
34
        );
35
36
        $foundCommands = $commandRepo->getByMessage(RegisterUser::class);
37
        $this->assertCount(1, $foundCommands);
38
        $this->assertSame($command, $foundCommands[0]);
39
40
        /** @var PersistentEventRepository $eventRepository */
41
        $eventRepository = $this->container->get('hmlb_ddd.repository.event');
42
43
        $foundEvents = $eventRepository->getByMessage(UserRegistered::class);
44
        $this->assertCount(1, $foundEvents);
45
46
        /** @var UserRegistered $event */
47
        $event = $foundEvents[0];
48
        $this->assertInstanceOf(UserRegistered::class, $event);
49
        $this->assertEquals($command->getUsername(), $event->getUsername());
50
51
        $foundEvent = $eventRepository->get($event->getId());
52
        $this->assertSame($event, $foundEvent);
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
        $commandRepo = $this->getCommandRepository();
68
        $this->handleCommandAndAssertTraced(
69
            $this->getCommandBus(),
70
            $command,
71
            $commandRepo
72
        );
73
        $endPwd = $user->getPassword();
74
75
        $this->assertNotEquals($beginningPwd, $endPwd);
76
77
        $eventRepository = $this->getEventRepository();
78
79
        $foundEvents = $eventRepository->getByMessage(PasswordChanged::class);
80
        $this->assertCount(1, $foundEvents);
81
82
        /** @var PasswordChanged $event */
83
        $event = $foundEvents[0];
84
        $this->assertInstanceOf(PasswordChanged::class, $event);
85
        $this->assertEquals($command->getUserId(), $event->getUserId());
86
        $this->assertNotEquals($event->getOldPassword(), $event->getNewPassword());
87
88
        $foundEvent = $eventRepository->get($event->getId());
89
        $this->assertSame($event, $foundEvent);
90
    }
91
92
    private function getUserRepository(): UserRepository
93
    {
94
        return $this->container->get('hmlb_user.repository.user');
95
    }
96
97
    private function getCommandRepository(): PersistentCommandRepository
98
    {
99
        return $this->container->get('hmlb_ddd.repository.command');
100
    }
101
102
    private function getEventRepository(): PersistentEventRepository
103
    {
104
        return $this->container->get('hmlb_ddd.repository.event');
105
    }
106
}
107