Completed
Push — master ( ed41b4...63431b )
by Beñat
14s
created

ChangeTaskPriorityHandlerSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 20.8 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 26
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 7 1
A it_is_initializable() 0 4 1
B it_handles_task_priority_change() 0 31 1
A it_does_not_allow_changing_priority_if_task_does_not_exist() 0 10 1
A it_does_not_allow_changing_priority_if_project_does_not_exist() 0 16 1
A it_does_not_allow_changing_priority_if_organization_does_not_exist() 0 22 1
B it_does_not_allow_changing_priority_if_editor_is_not_organization_member() 26 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ChangeTaskPriorityCommand;
16
use Kreta\TaskManager\Application\Project\Task\ChangeTaskPriorityHandler;
17
use Kreta\TaskManager\Domain\Model\Organization\MemberId;
18
use Kreta\TaskManager\Domain\Model\Organization\Organization;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
22
use Kreta\TaskManager\Domain\Model\Project\Project;
23
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
25
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
26
use Kreta\TaskManager\Domain\Model\Project\Task\Task;
27
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
28
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId;
29
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority;
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 ChangeTaskPriorityHandlerSpec extends ObjectBehavior
36
{
37
    function let(
38
        OrganizationRepository $organizationRepository,
39
        ProjectRepository $projectRepository,
40
        TaskRepository $taskRepository)
41
    {
42
        $this->beConstructedWith($organizationRepository, $projectRepository, $taskRepository);
43
    }
44
45
    function it_is_initializable()
46
    {
47
        $this->shouldHaveType(ChangeTaskPriorityHandler::class);
48
    }
49
50
    function it_handles_task_priority_change(
51
        OrganizationRepository $organizationRepository,
52
        ProjectRepository $projectRepository,
53
        TaskRepository $taskRepository,
54
        ChangeTaskPriorityCommand $command,
55
        Task $task,
56
        Project $project,
57
        ProjectId $projectId,
58
        OrganizationId $organizationId,
59
        Organization $organization
60
    ) {
61
        $command->id()->shouldBeCalled()->willReturn('task-id');
62
        $command->priority()->shouldBeCalled()->willReturn('low');
63
        $command->editorId()->shouldBeCalled()->willReturn('editor-id');
64
65
        $taskRepository->taskOfId(Argument::type(TaskId::class))->shouldBeCalled()->willReturn($task);
66
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
67
68
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
69
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
70
71
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
72
        $organization->isMember(Argument::type(MemberId::class))->shouldBeCalled()->willReturn(true);
73
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
74
75
        $task->changePriority(Argument::type(TaskPriority::class))->shouldBeCalled();
76
77
        $taskRepository->persist(Argument::type(Task::class))->shouldBeCalled();
78
79
        $this->__invoke($command);
80
    }
81
82
    function it_does_not_allow_changing_priority_if_task_does_not_exist(
83
        TaskRepository $taskRepository,
84
        ChangeTaskPriorityCommand $command
85
    ) {
86
        $command->id()->shouldBeCalled()->willReturn('task-id');
87
88
        $taskRepository->taskOfId(Argument::type(TaskId::class))->shouldBeCalled()->willReturn(null);
89
90
        $this->shouldThrow(TaskDoesNotExistException::class)->during('__invoke', [$command]);
91
    }
92
93
    function it_does_not_allow_changing_priority_if_project_does_not_exist(
94
        ProjectRepository $projectRepository,
95
        TaskRepository $taskRepository,
96
        ChangeTaskPriorityCommand $command,
97
        Task $task,
98
        ProjectId $projectId
99
    ) {
100
        $command->id()->shouldBeCalled()->willReturn('task-id');
101
102
        $taskRepository->taskOfId(Argument::type(TaskId::class))->shouldBeCalled()->willReturn($task);
103
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
104
105
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn(null);
106
107
        $this->shouldThrow(ProjectDoesNotExistException::class)->during('__invoke', [$command]);
108
    }
109
110
    function it_does_not_allow_changing_priority_if_organization_does_not_exist(
111
        OrganizationRepository $organizationRepository,
112
        ProjectRepository $projectRepository,
113
        TaskRepository $taskRepository,
114
        ChangeTaskPriorityCommand $command,
115
        Task $task,
116
        Project $project,
117
        ProjectId $projectId,
118
        OrganizationId $organizationId
119
    ) {
120
        $command->id()->shouldBeCalled()->willReturn('task-id');
121
122
        $taskRepository->taskOfId(Argument::type(TaskId::class))->shouldBeCalled()->willReturn($task);
123
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
124
125
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
126
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
127
128
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn(null);
129
130
        $this->shouldThrow(OrganizationDoesNotExistException::class)->during('__invoke', [$command]);
131
    }
132
133 View Code Duplication
    function it_does_not_allow_changing_priority_if_editor_is_not_organization_member(
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...
134
        OrganizationRepository $organizationRepository,
135
        ProjectRepository $projectRepository,
136
        TaskRepository $taskRepository,
137
        ChangeTaskPriorityCommand $command,
138
        Task $task,
139
        Project $project,
140
        ProjectId $projectId,
141
        OrganizationId $organizationId,
142
        Organization $organization
143
    ) {
144
        $command->id()->shouldBeCalled()->willReturn('task-id');
145
        $command->editorId()->shouldBeCalled()->willReturn('editor-id');
146
147
        $taskRepository->taskOfId(Argument::type(TaskId::class))->shouldBeCalled()->willReturn($task);
148
        $task->projectId()->shouldBeCalled()->willReturn($projectId);
149
150
        $projectRepository->projectOfId($projectId)->shouldBeCalled()->willReturn($project);
151
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
152
153
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
154
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
155
        $organization->isMember(Argument::type(MemberId::class))->shouldBeCalled()->willReturn(false);
156
157
        $this->shouldThrow(UnauthorizedTaskActionException::class)->during('__invoke', [$command]);
158
    }
159
}
160