|
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\EditOrganizationCommand; |
|
19
|
|
|
use Kreta\TaskManager\Application\Command\Organization\EditOrganizationHandler; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationName; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedEditOrganizationException; |
|
27
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
28
|
|
|
use PhpSpec\ObjectBehavior; |
|
29
|
|
|
use Prophecy\Argument; |
|
30
|
|
|
|
|
31
|
|
|
class EditOrganizationHandlerSpec extends ObjectBehavior |
|
32
|
|
|
{ |
|
33
|
|
|
private $slug; |
|
34
|
|
|
private $name; |
|
35
|
|
|
private $organizationId; |
|
36
|
|
|
private $editorId; |
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
function let(OrganizationRepository $repository, EditOrganizationCommand $command) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$this->beConstructedWith($repository); |
|
41
|
|
|
|
|
42
|
|
|
$command->name()->shouldBeCalled()->willReturn('Organization name'); |
|
43
|
|
|
$command->slug()->shouldBeCalled()->willReturn('organization-slug'); |
|
44
|
|
|
$command->id()->shouldBeCalled()->willReturn('organization-id'); |
|
45
|
|
|
$command->editorId()->shouldBeCalled()->willReturn('editor-id'); |
|
46
|
|
|
|
|
47
|
|
|
$this->slug = new Slug('organization-slug'); |
|
48
|
|
|
$this->name = new OrganizationName('Organization name'); |
|
49
|
|
|
$this->organizationId = OrganizationId::generate('organization-id'); |
|
50
|
|
|
$this->editorId = UserId::generate('editor-id'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
function it_edits_an_organization( |
|
|
|
|
|
|
54
|
|
|
EditOrganizationCommand $command, |
|
55
|
|
|
OrganizationRepository $repository, |
|
56
|
|
|
Organization $organization |
|
57
|
|
|
) { |
|
58
|
|
|
$this->shouldHaveType(EditOrganizationHandler::class); |
|
59
|
|
|
|
|
60
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); |
|
61
|
|
|
$organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true); |
|
62
|
|
|
$repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn(null); |
|
63
|
|
|
$organization->edit($this->name, $this->slug)->shouldBeCalled(); |
|
64
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
65
|
|
|
$this->__invoke($command); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function it_edits_an_organization_with_the_same_slug( |
|
69
|
|
|
EditOrganizationCommand $command, |
|
70
|
|
|
OrganizationRepository $repository, |
|
71
|
|
|
Organization $organization, |
|
72
|
|
|
Organization $organization2 |
|
73
|
|
|
) { |
|
74
|
|
|
$this->shouldHaveType(EditOrganizationHandler::class); |
|
75
|
|
|
|
|
76
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); |
|
77
|
|
|
$organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true); |
|
78
|
|
|
$repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization2); |
|
79
|
|
|
$organization2->id()->shouldBeCalled()->willReturn($this->organizationId); |
|
80
|
|
|
$organization->edit($this->name, $this->slug)->shouldBeCalled(); |
|
81
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
82
|
|
|
$this->__invoke($command); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
function it_edits_an_organization_without_slug( |
|
86
|
|
|
EditOrganizationCommand $command, |
|
87
|
|
|
OrganizationRepository $repository, |
|
88
|
|
|
Organization $organization |
|
89
|
|
|
) { |
|
90
|
|
|
$command->slug()->shouldBeCalled()->willReturn(null); |
|
91
|
|
|
$slug = new Slug('Organization name'); |
|
92
|
|
|
|
|
93
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); |
|
94
|
|
|
$organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true); |
|
95
|
|
|
$repository->organizationOfSlug($slug)->shouldBeCalled()->willReturn(null); |
|
96
|
|
|
$organization->edit($this->name, $slug)->shouldBeCalled(); |
|
97
|
|
|
$repository->persist(Argument::type(Organization::class))->shouldBeCalled(); |
|
98
|
|
|
$this->__invoke($command); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
function it_does_not_edits_an_organization_because_the_organization_does_not_exist( |
|
|
|
|
|
|
102
|
|
|
EditOrganizationCommand $command, |
|
103
|
|
|
OrganizationRepository $repository |
|
104
|
|
|
) { |
|
105
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null); |
|
106
|
|
|
$this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($command); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function it_does_not_edits_an_organization_because_the_organization_slug_already_exists( |
|
110
|
|
|
EditOrganizationCommand $command, |
|
111
|
|
|
OrganizationRepository $repository, |
|
112
|
|
|
Organization $organization, |
|
113
|
|
|
Organization $organization2 |
|
114
|
|
|
) { |
|
115
|
|
|
$organizationId2 = OrganizationId::generate('organization-id-2'); |
|
116
|
|
|
|
|
117
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); |
|
118
|
|
|
$organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true); |
|
119
|
|
|
$repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization2); |
|
120
|
|
|
$organization2->id()->shouldBeCalled()->willReturn($organizationId2); |
|
121
|
|
|
$this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
View Code Duplication |
function it_does_not_edits_an_organization_because_the_owner_does_not_authorized( |
|
|
|
|
|
|
125
|
|
|
EditOrganizationCommand $command, |
|
126
|
|
|
OrganizationRepository $repository, |
|
127
|
|
|
Organization $organization |
|
128
|
|
|
) { |
|
129
|
|
|
$repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); |
|
130
|
|
|
$organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(false); |
|
131
|
|
|
$this->shouldThrow(UnauthorizedEditOrganizationException::class)->during__invoke($command); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
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.