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
|
|
|
|