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