EditProfileHandlerSpec   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 0
loc 153
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_does_not_edit_profile_when_the_user_does_not_exist() 0 14 1
A it_does_not_edit_profile_when_the_username_is_already_in_use() 0 20 1
B it_does_not_edit_profile_when_the_email_is_already_in_use() 0 25 1
B it_edits_profile() 0 35 1
B it_edits_profile_with_image() 0 42 1
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
declare(strict_types=1);
14
15
namespace Spec\Kreta\IdentityAccess\Application\Command;
16
17
use BenGorFile\File\Domain\Model\FileMimeType;
18
use BenGorFile\File\Domain\Model\FileName;
19
use BenGorFile\File\Domain\Model\Filesystem;
20
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException;
21
use BenGorUser\User\Domain\Model\UserEmail;
22
use BenGorUser\User\Domain\Model\UserId;
23
use Kreta\IdentityAccess\Application\Command\EditProfileCommand;
24
use Kreta\IdentityAccess\Application\Command\EditProfileHandler;
25
use Kreta\IdentityAccess\Domain\Model\User\FullName;
26
use Kreta\IdentityAccess\Domain\Model\User\User;
27
use Kreta\IdentityAccess\Domain\Model\User\UserEmailAlreadyExistsException;
28
use Kreta\IdentityAccess\Domain\Model\User\Username;
29
use Kreta\IdentityAccess\Domain\Model\User\UsernameAlreadyExistsException;
30
use Kreta\IdentityAccess\Domain\Model\User\UserRepository;
31
use PhpSpec\ObjectBehavior;
32
use Prophecy\Argument;
33
34
class EditProfileHandlerSpec extends ObjectBehavior
35
{
36
    function let(UserRepository $repository, Filesystem $filesystem)
37
    {
38
        $this->beConstructedWith($repository, $filesystem);
39
    }
40
41
    function it_is_initializable()
42
    {
43
        $this->shouldHaveType(EditProfileHandler::class);
44
    }
45
46
    function it_does_not_edit_profile_when_the_user_does_not_exist(
47
        EditProfileCommand $command,
48
        UserRepository $repository
49
    ) {
50
        $command->id()->shouldBeCalled()->willReturn('user-id');
51
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
52
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
53
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
54
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
55
56
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn(null);
57
58
        $this->shouldThrow(UserDoesNotExistException::class)->during__invoke($command);
59
    }
60
61
    function it_does_not_edit_profile_when_the_username_is_already_in_use(
62
        EditProfileCommand $command,
63
        UserRepository $repository,
64
        User $user,
65
        User $anotherUser
66
    ) {
67
        $command->id()->shouldBeCalled()->willReturn('user-id');
68
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
69
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
70
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
71
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
72
73
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
74
75
        $repository->userOfUsername(Username::from('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
76
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
77
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('another-user-id'));
78
79
        $this->shouldThrow(UsernameAlreadyExistsException::class)->during__invoke($command);
80
    }
81
82
    function it_does_not_edit_profile_when_the_email_is_already_in_use(
83
        EditProfileCommand $command,
84
        UserRepository $repository,
85
        User $user,
86
        User $anotherUser,
87
        User $anotherUser2
88
    ) {
89
        $command->id()->shouldBeCalled()->willReturn('user-id');
90
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
91
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
92
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
93
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
94
95
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
96
97
        $repository->userOfUsername(Username::from('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
98
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
99
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
100
101
        $repository->userOfEmail(new UserEmail('[email protected]'))->shouldBeCalled()->willReturn($anotherUser2);
102
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
103
        $anotherUser2->id()->shouldBeCalled()->willReturn(new UserId('another-user-id'));
104
105
        $this->shouldThrow(UserEmailAlreadyExistsException::class)->during__invoke($command);
106
    }
107
108
    function it_edits_profile(
109
        EditProfileCommand $command,
110
        UserRepository $repository,
111
        User $user,
112
        User $anotherUser,
113
        User $anotherUser2
114
    ) {
115
        $command->id()->shouldBeCalled()->willReturn('user-id');
116
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
117
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
118
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
119
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
120
121
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
122
123
        $repository->userOfUsername(Username::from('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
124
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
125
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
126
127
        $repository->userOfEmail(new UserEmail('[email protected]'))->shouldBeCalled()->willReturn($anotherUser2);
128
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
129
        $anotherUser2->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
130
131
        $user->editProfile(
132
            new UserEmail('[email protected]'),
133
            Username::from('kreta-username'),
134
            new FullName('kreta', 'lastname')
135
        )->shouldBeCalled();
136
137
        $command->uploadedImage()->shouldBeCalled()->willReturn(null);
138
139
        $repository->persist($user)->shouldBeCalled();
140
141
        $this->__invoke($command);
142
    }
143
144
    function it_edits_profile_with_image(
145
        EditProfileCommand $command,
146
        UserRepository $repository,
147
        User $user,
148
        User $anotherUser,
149
        User $anotherUser2,
150
        Filesystem $filesystem
151
    ) {
152
        $command->id()->shouldBeCalled()->willReturn('user-id');
153
        $command->email()->shouldBeCalled()->willReturn('[email protected]');
154
        $command->username()->shouldBeCalled()->willReturn('kreta-username');
155
        $command->firstName()->shouldBeCalled()->willReturn('kreta');
156
        $command->lastName()->shouldBeCalled()->willReturn('lastname');
157
158
        $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user);
159
160
        $repository->userOfUsername(Username::from('kreta-username'))->shouldBeCalled()->willReturn($anotherUser);
161
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
162
        $anotherUser->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
163
164
        $repository->userOfEmail(new UserEmail('[email protected]'))->shouldBeCalled()->willReturn($anotherUser2);
165
        $user->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
166
        $anotherUser2->id()->shouldBeCalled()->willReturn(new UserId('user-id'));
167
168
        $user->editProfile(
169
            new UserEmail('[email protected]'),
170
            Username::from('kreta-username'),
171
            new FullName('kreta', 'lastname')
172
        )->shouldBeCalled();
173
174
        $command->uploadedImage()->shouldBeCalled()->willReturn('image data content');
175
176
        $command->imageName()->shouldBeCalled()->willReturn('image-name.png');
177
        $command->imageMimeType()->shouldBeCalled()->willReturn('image/png');
178
        $mimeType = new FileMimeType('image/png');
179
        $filesystem->write(Argument::type(FileName::class), 'image data content')->shouldBeCalled();
180
        $user->uploadImage(Argument::type(FileName::class), $mimeType)->shouldBeCalled();
181
182
        $repository->persist($user)->shouldBeCalled();
183
184
        $this->__invoke($command);
185
    }
186
}
187