Completed
Pull Request — master (#240)
by Beñat
04:56
created

EditProfileHandlerSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Spec\Kreta\IdentityAccess\Application\Command;
14
15
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException;
16
use BenGorUser\User\Domain\Model\UserEmail;
17
use BenGorUser\User\Domain\Model\UserId;
18
use Kreta\IdentityAccess\Application\Command\EditProfileCommand;
19
use Kreta\IdentityAccess\Application\Command\EditProfileHandler;
20
use Kreta\IdentityAccess\Domain\Model\User\FullName;
21
use Kreta\IdentityAccess\Domain\Model\User\User;
22
use Kreta\IdentityAccess\Domain\Model\User\UserEmailAlreadyExistsException;
23
use Kreta\IdentityAccess\Domain\Model\User\Username;
24
use Kreta\IdentityAccess\Domain\Model\User\UsernameAlreadyExistsException;
25
use Kreta\IdentityAccess\Domain\Model\User\UserRepository;
26
use PhpSpec\ObjectBehavior;
27
28
class EditProfileHandlerSpec extends ObjectBehavior
29
{
30
    function let(UserRepository $repository)
31
    {
32
        $this->beConstructedWith($repository);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType(EditProfileHandler::class);
38
    }
39
40
    function it_does_not_edit_profile_when_the_user_does_not_exist(
41
        EditProfileCommand $command,
42
        UserRepository $repository
43
    ) {
44
        $command->id()->shouldBeCalled()->willReturn('user-id');
45
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
46
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
47
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
48
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
49
50
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn(null);
51
52
        $this->shouldThrow(UserDoesNotExistException::class)->during__invoke($command);
53
    }
54
55
    function it_does_not_edit_profile_when_the_username_is_already_in_use(
56
        EditProfileCommand $command,
57
        UserRepository $repository,
58
        User $user,
59
        User $anotherUser
60
    ) {
61
        $command->id()->shouldBeCalled()->willReturn('user-id');
62
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
63
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
64
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
65
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
66
67
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
68
69
        $repository->userOfUsername(new Username('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
70
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
71
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('another-user-id'));
72
73
        $this->shouldThrow(UsernameAlreadyExistsException::class)->during__invoke($command);
74
    }
75
76
    function it_does_not_edit_profile_when_the_email_is_already_in_use(
77
        EditProfileCommand $command,
78
        UserRepository $repository,
79
        User $user,
80
        User $anotherUser,
81
        User $anotherUser2
82
    ) {
83
        $command->id()->shouldBeCalled()->willReturn('user-id');
84
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
85
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
86
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
87
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
88
89
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
90
91
        $repository->userOfUsername(new Username('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
92
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
93
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
94
95
        $repository->userOfEmail(new UserEmail('[email protected]'))->shouldBeCalled()->willReturn($anotherUser2);
96
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
97
        $anotherUser2->id()->shouldBeCalled()->willReturn(new UserId('another-user-id'));
98
99
        $this->shouldThrow(UserEmailAlreadyExistsException::class)->during__invoke($command);
100
    }
101
102
    function it_edits_profile(
103
        EditProfileCommand $command,
104
        UserRepository $repository,
105
        User $user,
106
        User $anotherUser,
107
        User $anotherUser2
108
    ) {
109
        $command->id()->shouldBeCalled()->willReturn('user-id');
110
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
111
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
112
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
113
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
114
115
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
116
117
        $repository->userOfUsername(new Username('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
118
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
119
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
120
121
        $repository->userOfEmail(new UserEmail('[email protected]'))->shouldBeCalled()->willReturn($anotherUser2);
122
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
123
        $anotherUser2->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
124
125
        $user->editProfile(
126
            new UserEmail('[email protected]'),
127
            new Username('kreta-username'),
128
            new FullName('kreta', 'lastname')
129
        )->shouldBeCalled();
130
131
        $repository->persist($user)->shouldBeCalled();
132
133
        $this->__invoke($command);
134
    }
135
}
136