|
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\TaskManager\Application\Command\Organization; |
|
14
|
|
|
|
|
15
|
|
|
use Kreta\TaskManager\Application\Command\Organization\CreateOrganizationCommand; |
|
16
|
|
|
use Kreta\TaskManager\Application\Command\Organization\CreateOrganizationHandler; |
|
17
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
18
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\User\User; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserDoesNotExistException; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserRepository; |
|
25
|
|
|
use PhpSpec\ObjectBehavior; |
|
26
|
|
|
use Prophecy\Argument; |
|
27
|
|
|
|
|
28
|
|
|
class CreateOrganizationHandlerSpec extends ObjectBehavior |
|
29
|
|
|
{ |
|
30
|
|
|
function let(OrganizationRepository $repository, UserRepository $userRepository) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->beConstructedWith($repository, $userRepository); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
function it_is_initializable() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->shouldHaveType(CreateOrganizationHandler::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
function it_creates_an_organization( |
|
|
|
|
|
|
41
|
|
|
CreateOrganizationCommand $command, |
|
42
|
|
|
OrganizationRepository $repository, |
|
43
|
|
|
UserRepository $userRepository, |
|
44
|
|
|
User $user |
|
45
|
|
|
) { |
|
46
|
|
|
$userId = UserId::generate('user-id'); |
|
47
|
|
|
$command->id()->shouldBeCalled()->willReturn('organization-id'); |
|
48
|
|
|
$repository->organizationOfId(Argument::type(OrganizationId::class))->shouldBeCalled()->willReturn(null); |
|
49
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
50
|
|
|
$userRepository->userOfId($userId)->shouldBeCalled()->willReturn($user); |
|
51
|
|
|
$user->id()->shouldBeCalled()->willReturn($userId); |
|
52
|
|
|
$command->name()->shouldBeCalled()->willReturn('Organization name'); |
|
53
|
|
|
$command->slug()->shouldBeCalled()->willReturn('organization-slug'); |
|
54
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
55
|
|
|
$this->__invoke($command); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
function it_creates_an_organization_without_organization_id( |
|
|
|
|
|
|
59
|
|
|
CreateOrganizationCommand $command, |
|
60
|
|
|
OrganizationRepository $repository, |
|
61
|
|
|
UserRepository $userRepository, |
|
62
|
|
|
User $user |
|
63
|
|
|
) { |
|
64
|
|
|
$userId = UserId::generate('user-id'); |
|
65
|
|
|
$command->id()->shouldBeCalled()->willReturn(null); |
|
66
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
67
|
|
|
$userRepository->userOfId($userId)->shouldBeCalled()->willReturn($user); |
|
68
|
|
|
$user->id()->shouldBeCalled()->willReturn($userId); |
|
69
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
70
|
|
|
$command->name()->shouldBeCalled()->willReturn('Organization name'); |
|
71
|
|
|
$command->slug()->shouldBeCalled()->willReturn('organization-slug'); |
|
72
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
73
|
|
|
$this->__invoke($command); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function it_creates_an_organization_without_slug( |
|
77
|
|
|
CreateOrganizationCommand $command, |
|
78
|
|
|
OrganizationRepository $repository, |
|
79
|
|
|
UserRepository $userRepository, |
|
80
|
|
|
User $user |
|
81
|
|
|
) { |
|
82
|
|
|
$userId = UserId::generate('user-id'); |
|
83
|
|
|
$command->id()->shouldBeCalled()->willReturn('organization-id'); |
|
84
|
|
|
$repository->organizationOfId(Argument::type(OrganizationId::class))->shouldBeCalled()->willReturn(null); |
|
85
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
86
|
|
|
$userRepository->userOfId($userId)->shouldBeCalled()->willReturn($user); |
|
87
|
|
|
$user->id()->shouldBeCalled()->willReturn($userId); |
|
88
|
|
|
$repository->organizationOfId(Argument::type(OrganizationId::class))->shouldBeCalled()->willReturn(null); |
|
89
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
90
|
|
|
$command->name()->shouldBeCalled()->willReturn('Organization name'); |
|
91
|
|
|
$command->slug()->shouldBeCalled()->willReturn(null); |
|
92
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
93
|
|
|
$this->__invoke($command); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
function it_does_not_create_an_organization_because_already_exists_an_organization_with_id( |
|
97
|
|
|
CreateOrganizationCommand $command, |
|
98
|
|
|
OrganizationRepository $repository, |
|
99
|
|
|
Organization $organization |
|
100
|
|
|
) { |
|
101
|
|
|
$command->id()->shouldBeCalled()->willReturn('organization-id'); |
|
102
|
|
|
$repository->organizationOfId(Argument::type(OrganizationId::class)) |
|
103
|
|
|
->shouldBeCalled()->willReturn($organization); |
|
104
|
|
|
$this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
function it_does_not_create_an_organization_because_user_does_not_exist( |
|
108
|
|
|
CreateOrganizationCommand $command, |
|
109
|
|
|
UserRepository $userRepository |
|
110
|
|
|
) { |
|
111
|
|
|
$command->id()->shouldBeCalled()->willReturn(null); |
|
112
|
|
|
$command->creatorId()->shouldBeCalled()->willReturn('user-id'); |
|
113
|
|
|
$userRepository->userOfId(Argument::type(UserId::class))->shouldBeCalled()->willReturn(null); |
|
114
|
|
|
$this->shouldThrow(UserDoesNotExistException::class)->during__invoke($command); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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.