Completed
Pull Request — master (#170)
by Beñat
10:01 queued 04:43
created

EditProjectHandlerSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 49.41 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 10
dl 42
loc 85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_edits_project() 21 21 1
A it_edits_project_with_default_slug() 21 21 1
A it_does_not_allow_to_edit_project_if_project_does_not_exist() 0 8 1
A it_does_not_allow_to_edit_project_if_editor_is_not_organization_owner() 0 19 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;
14
15
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
16
use Kreta\TaskManager\Application\Project\EditProjectCommand;
17
use Kreta\TaskManager\Application\Project\EditProjectHandler;
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\Organization\OwnerId;
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\ProjectName;
26
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
27
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectActionException;
28
use PhpSpec\ObjectBehavior;
29
use Prophecy\Argument;
30
31
class EditProjectHandlerSpec extends ObjectBehavior
32
{
33
    function let(ProjectRepository $projectRepository, OrganizationRepository $organizationRepository)
34
    {
35
        $this->beConstructedWith($projectRepository, $organizationRepository);
36
    }
37
38
    function it_is_initializable()
39
    {
40
        $this->shouldHaveType(EditProjectHandler::class);
41
    }
42
43 View Code Duplication
    function it_edits_project(
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...
44
        OrganizationRepository $organizationRepository,
45
        ProjectRepository $projectRepository,
46
        EditProjectCommand $command,
47
        Project $project,
48
        Organization $organization,
49
        OrganizationId $organizationId
50
    ) {
51
        $command->id()->willReturn('project-id');
52
        $command->name()->willReturn('Project name');
53
        $command->slug()->willReturn('project-name');
54
        $command->editorId()->willReturn('editor-id');
55
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
56
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
57
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
58
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
59
        $organization->isOwner(Argument::type(OwnerId::class))->shouldBeCalled()->willReturn(true);
60
        $project->edit(new ProjectName('Project name'), new Slug('project-name'))->shouldBeCalled();
61
        $projectRepository->persist(Argument::type(Project::class))->shouldBeCalled();
62
        $this->__invoke($command);
63
    }
64
65 View Code Duplication
    function it_edits_project_with_default_slug(
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...
66
        OrganizationRepository $organizationRepository,
67
        ProjectRepository $projectRepository,
68
        EditProjectCommand $command,
69
        Project $project,
70
        Organization $organization,
71
        OrganizationId $organizationId
72
    ) {
73
        $command->id()->willReturn('project-id');
74
        $command->name()->willReturn('Project name');
75
        $command->slug()->willReturn(null);
76
        $command->editorId()->willReturn('editor-id');
77
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
78
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
79
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
80
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
81
        $organization->isOwner(Argument::type(OwnerId::class))->shouldBeCalled()->willReturn(true);
82
        $project->edit(new ProjectName('Project name'), new Slug('Project name'))->shouldBeCalled();
83
        $projectRepository->persist(Argument::type(Project::class))->shouldBeCalled();
84
        $this->__invoke($command);
85
    }
86
87
    function it_does_not_allow_to_edit_project_if_project_does_not_exist(
88
        ProjectRepository $projectRepository,
89
        EditProjectCommand $command
90
    ) {
91
        $command->id()->willReturn('project-id');
92
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn(null);
93
        $this->shouldThrow(ProjectDoesNotExistException::class)->during__invoke($command);
94
    }
95
96
    function it_does_not_allow_to_edit_project_if_editor_is_not_organization_owner(
97
        OrganizationRepository $organizationRepository,
98
        ProjectRepository $projectRepository,
99
        EditProjectCommand $command,
100
        Project $project,
101
        Organization $organization,
102
        OrganizationId $organizationId
103
    ) {
104
        $command->id()->willReturn('project-id');
105
        $command->name()->willReturn('Project name');
106
        $command->slug()->willReturn(null);
107
        $command->editorId()->willReturn('editor-id');
108
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
109
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
110
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
111
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
112
        $organization->isOwner(Argument::type(OwnerId::class))->shouldBeCalled()->willReturn(false);
113
        $this->shouldThrow(UnauthorizedProjectActionException::class)->during__invoke($command);
114
    }
115
}
116