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\TaskManager\Application\Command\User; |
16
|
|
|
|
17
|
|
|
use Kreta\TaskManager\Application\Command\User\AddUserCommand; |
18
|
|
|
use Kreta\TaskManager\Application\Command\User\AddUserHandler; |
19
|
|
|
use Kreta\TaskManager\Domain\Model\User\User; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserAlreadyExistsException; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserRepository; |
23
|
|
|
use PhpSpec\ObjectBehavior; |
24
|
|
|
use Prophecy\Argument; |
25
|
|
|
|
26
|
|
View Code Duplication |
class AddUserHandlerSpec extends ObjectBehavior |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
function let(UserRepository $repository) |
29
|
|
|
{ |
30
|
|
|
$this->beConstructedWith($repository); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function it_is_initializable() |
34
|
|
|
{ |
35
|
|
|
$this->shouldHaveType(AddUserHandler::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function it_adds_user(AddUserCommand $command, UserRepository $repository) |
39
|
|
|
{ |
40
|
|
|
$command->userId()->shouldBeCalled()->willReturn('user-id'); |
41
|
|
|
$repository->userOfId(UserId::generate('user-id'))->shouldBeCalled()->willReturn(null); |
42
|
|
|
$repository->persist(Argument::type(User::class))->shouldBeCalled(); |
43
|
|
|
$this->__invoke($command); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
function it_does_not_add_user_when_the_user_already_exists( |
47
|
|
|
AddUserCommand $command, |
48
|
|
|
UserRepository $repository, |
49
|
|
|
User $user |
50
|
|
|
) { |
51
|
|
|
$command->userId()->shouldBeCalled()->willReturn('user-id'); |
52
|
|
|
$repository->userOfId(UserId::generate('user-id'))->shouldBeCalled()->willReturn($user); |
53
|
|
|
$user->id()->shouldBeCalled()->willReturn(UserId::generate('user-id')); |
54
|
|
|
$this->shouldThrow(UserAlreadyExistsException::class)->during__invoke($command); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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.