Completed
Push — master ( f0708c...690234 )
by Gorka
02:59
created

it_changes_a_parent_task()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 32
nc 1
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Project\Task;
14
15
use Kreta\TaskManager\Application\Project\Task\ChangeParentTaskCommand;
16
use Kreta\TaskManager\Application\Project\Task\ChangeParentTaskHandler;
17
use Kreta\TaskManager\Domain\Model\Organization\MemberId;
18
use Kreta\TaskManager\Domain\Model\Organization\Organization;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
21
use Kreta\TaskManager\Domain\Model\Project\Project;
22
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
23
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
24
use Kreta\TaskManager\Domain\Model\Project\Task\Task;
25
use Kreta\TaskManager\Domain\Model\Project\Task\TaskAndTaskParentCannotBeTheSameException;
26
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
27
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId;
28
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentCannotBelongToOtherProjectException;
29
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentDoesNotExistException;
30
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
31
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
32
use PhpSpec\ObjectBehavior;
33
use Prophecy\Argument;
34
35
class ChangeParentTaskHandlerSpec extends ObjectBehavior
36
{
37
    function let(
38
        TaskRepository $repository,
39
        ProjectRepository $projectRepository,
40
        OrganizationRepository $organizationRepository
41
    ) {
42
        $this->beConstructedWith($repository, $projectRepository, $organizationRepository);
43
    }
44
45
    function it_is_initializable()
46
    {
47
        $this->shouldHaveType(ChangeParentTaskHandler::class);
48
    }
49
50
    function it_changes_a_parent_task(
51
        ChangeParentTaskCommand $command,
52
        TaskRepository $repository,
53
        Task $task,
54
        TaskId $taskId,
55
        Task $parent,
56
        ProjectId $projectId,
57
        ProjectRepository $projectRepository,
58
        Project $project,
59
        OrganizationRepository $organizationRepository,
60
        Organization $organization,
61
        OrganizationId $organizationId,
62
        TaskId $parentId
63
    ) {
64
        $command->id()->shouldBeCalled()->willReturn('task-id');
65
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
66
        $command->parentId()->shouldBeCalled()->willReturn('parent-id');
67
        $task->id()->shouldBeCalled()->willReturn($taskId);
68
        $taskId->equals(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(false);
69
        $repository->taskOfId(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn($parent);
70
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
71
        $parent->projectId()->shouldBeCalled()->willReturn($projectId);
72
        $projectId->equals($projectId)->shouldBeCalled()->willReturn(true);
73
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
74
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
75
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
76
        $command->changerId()->shouldBeCalled()->willReturn('changer-id');
77
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
78
        $organization->isMember(Argument::type(MemberId::class))->shouldBeCalled()->willReturn(true);
79
        $parent->id()->shouldBeCalled()->willReturn($parentId);
80
        $task->changeParent($parentId)->shouldBeCalled();
81
        $repository->persist($task)->shouldBeCalled();
82
        $this->__invoke($command);
83
    }
84
85
    function it_removes_parent_task(
86
        ChangeParentTaskCommand $command,
87
        TaskRepository $repository,
88
        Task $task,
89
        ProjectId $projectId,
90
        ProjectRepository $projectRepository,
91
        Project $project,
92
        OrganizationRepository $organizationRepository,
93
        Organization $organization,
94
        OrganizationId $organizationId
95
    ) {
96
        $command->id()->shouldBeCalled()->willReturn('task-id');
97
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
98
        $command->parentId()->shouldBeCalled()->willReturn(null);
99
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
100
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
101
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
102
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
103
        $command->changerId()->shouldBeCalled()->willReturn('changer-id');
104
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
105
        $organization->isMember(Argument::type(MemberId::class))->shouldBeCalled()->willReturn(true);
106
        $task->changeParent(null)->shouldBeCalled();
107
        $repository->persist($task)->shouldBeCalled();
108
        $this->__invoke($command);
109
    }
110
111 View Code Duplication
    function it_does_not_change_parent_task_because_task_does_not_exist(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
112
        ChangeParentTaskCommand $command,
113
        TaskRepository $repository
114
    ) {
115
        $command->id()->shouldBeCalled()->willReturn('task-id');
116
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn(null);
117
        $this->shouldThrow(TaskDoesNotExistException::class)->during__invoke($command);
118
    }
119
120 View Code Duplication
    function it_does_not_change_parent_task_because_task_and_parent_task_are_the_same(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
121
        ChangeParentTaskCommand $command,
122
        TaskRepository $repository,
123
        Task $task,
124
        TaskId $taskId
125
    ) {
126
        $command->id()->shouldBeCalled()->willReturn('task-id');
127
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
128
        $command->parentId()->shouldBeCalled()->willReturn('task-id');
129
        $task->id()->shouldBeCalled()->willReturn($taskId);
130
        $taskId->equals(TaskId::generate('task-id'))->shouldBeCalled()->willReturn(true);
131
132
        $this->shouldThrow(TaskAndTaskParentCannotBeTheSameException::class)->during__invoke($command);
133
    }
134
135
    function it_does_not_change_parent_task_because_parent_task_does_not_exist(
136
        ChangeParentTaskCommand $command,
137
        TaskRepository $repository,
138
        Task $task,
139
        TaskId $taskId
140
    ) {
141
        $command->id()->shouldBeCalled()->willReturn('task-id');
142
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
143
        $command->parentId()->shouldBeCalled()->willReturn('parent-id');
144
        $task->id()->shouldBeCalled()->willReturn($taskId);
145
        $taskId->equals(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(false);
146
        $repository->taskOfId(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(null);
147
        $this->shouldThrow(TaskParentDoesNotExistException::class)->during__invoke($command);
148
    }
149
150
    function it_does_not_change_parent_task_because_task_and_parent_task_projects_are_different(
151
        ChangeParentTaskCommand $command,
152
        TaskRepository $repository,
153
        Task $task,
154
        TaskId $taskId,
155
        Task $parent,
156
        ProjectId $projectId,
157
        ProjectId $parentProjectId
158
    ) {
159
        $command->id()->shouldBeCalled()->willReturn('task-id');
160
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
161
        $command->parentId()->shouldBeCalled()->willReturn('parent-id');
162
        $task->id()->shouldBeCalled()->willReturn($taskId);
163
        $taskId->equals(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(false);
164
        $repository->taskOfId(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn($parent);
165
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
166
        $parent->projectId()->shouldBeCalled()->willReturn($parentProjectId);
167
        $projectId->equals($parentProjectId)->shouldBeCalled()->willReturn(false);
168
        $this->shouldThrow(TaskParentCannotBelongToOtherProjectException::class)->during__invoke($command);
169
    }
170
171
    function it_does_not_change_parent_task_because_task_action_is_not_allowed(
172
        ChangeParentTaskCommand $command,
173
        TaskRepository $repository,
174
        Task $task,
175
        TaskId $taskId,
176
        Task $parent,
177
        ProjectId $projectId,
178
        ProjectRepository $projectRepository,
179
        Project $project,
180
        OrganizationRepository $organizationRepository,
181
        Organization $organization,
182
        OrganizationId $organizationId
183
    ) {
184
        $command->id()->shouldBeCalled()->willReturn('task-id');
185
        $repository->taskOfId(TaskId::generate('task-id'))->shouldBeCalled()->willReturn($task);
186
        $command->parentId()->shouldBeCalled()->willReturn('parent-id');
187
        $task->id()->shouldBeCalled()->willReturn($taskId);
188
        $taskId->equals(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn(false);
189
        $repository->taskOfId(TaskId::generate('parent-id'))->shouldBeCalled()->willReturn($parent);
190
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
191
        $parent->projectId()->shouldBeCalled()->willReturn($projectId);
192
        $projectId->equals($projectId)->shouldBeCalled()->willReturn(true);
193
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
194
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
195
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
196
        $command->changerId()->shouldBeCalled()->willReturn('changer-id');
197
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
198
        $organization->isMember(Argument::type(MemberId::class))->shouldBeCalled()->willReturn(false);
199
        $this->shouldThrow(UnauthorizedTaskActionException::class)->during__invoke($command);
200
    }
201
}
202