FilterProjectsHandlerSpec   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 48.36 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 59
loc 122
rs 10
wmc 7
lcom 1
cbo 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 15 1
A it_is_initializable() 0 4 1
A it_serializes_filtered_projects() 21 21 1
A it_serializes_filtered_projects_without_organizations() 17 17 1
A it_serializes_filtered_projects_without_organization_id() 0 23 1
A it_serializes_filtered_projects_without_project_name() 21 21 1
A it_does_not_serialize_when_user_does_not_allow_to_perform_this_action() 0 12 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
declare(strict_types=1);
14
15
namespace Spec\Kreta\TaskManager\Application\Query\Project;
16
17
use Kreta\TaskManager\Application\DataTransformer\Project\ProjectDataTransformer;
18
use Kreta\TaskManager\Application\Query\Project\FilterProjectsHandler;
19
use Kreta\TaskManager\Application\Query\Project\FilterProjectsQuery;
20
use Kreta\TaskManager\Domain\Model\Organization\Organization;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
22
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
23
use Kreta\TaskManager\Domain\Model\Organization\OrganizationSpecificationFactory;
24
use Kreta\TaskManager\Domain\Model\Project\Project;
25
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
26
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
27
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
28
use Kreta\TaskManager\Domain\Model\User\UserId;
29
use PhpSpec\ObjectBehavior;
30
use Prophecy\Argument;
31
32
class FilterProjectsHandlerSpec extends ObjectBehavior
33
{
34
    function let(
35
        OrganizationRepository $organizationRepository,
36
        OrganizationSpecificationFactory $organizationSpecificationFactory,
37
        ProjectRepository $repository,
38
        ProjectSpecificationFactory $specificationFactory,
39
        ProjectDataTransformer $dataTransformer
40
    ) {
41
        $this->beConstructedWith(
42
            $organizationRepository,
43
            $organizationSpecificationFactory,
44
            $repository,
45
            $specificationFactory,
46
            $dataTransformer
47
        );
48
    }
49
50
    function it_is_initializable()
51
    {
52
        $this->shouldHaveType(FilterProjectsHandler::class);
53
    }
54
55 View Code Duplication
    function it_serializes_filtered_projects(
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...
56
        FilterProjectsQuery $query,
57
        ProjectRepository $repository,
58
        Project $project,
59
        ProjectDataTransformer $dataTransformer,
60
        OrganizationRepository $organizationRepository,
61
        Organization $organization
62
    ) {
63
        $query->userId()->shouldBeCalled()->willReturn('user-id');
64
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
65
        $organizationRepository->organizationOfId(OrganizationId::generate('organization-id'))
66
            ->shouldBeCalled()->willReturn($organization);
67
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
68
        $query->name()->shouldBeCalled()->willReturn('project name');
69
        $query->offset()->shouldBeCalled()->willReturn(0);
70
        $query->limit()->shouldBeCalled()->willReturn(-1);
71
        $repository->query(Argument::any())->shouldBeCalled()->willReturn([$project]);
72
        $dataTransformer->write($project)->shouldBeCalled();
73
        $dataTransformer->read()->shouldBeCalled();
74
        $this->__invoke($query)->shouldBeArray();
75
    }
76
77 View Code Duplication
    function it_serializes_filtered_projects_without_organizations(
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...
78
        FilterProjectsQuery $query,
79
        ProjectRepository $repository,
80
        OrganizationRepository $organizationRepository,
81
        Organization $organization
82
    ) {
83
        $query->userId()->shouldBeCalled()->willReturn('user-id');
84
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
85
        $organizationRepository->organizationOfId(OrganizationId::generate('organization-id'))
86
            ->shouldBeCalled()->willReturn($organization);
87
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
88
        $query->name()->shouldBeCalled()->willReturn('project name');
89
        $query->offset()->shouldBeCalled()->willReturn(0);
90
        $query->limit()->shouldBeCalled()->willReturn(-1);
91
        $repository->query(Argument::any())->shouldBeCalled()->willReturn([]);
92
        $this->__invoke($query)->shouldReturn([]);
93
    }
94
95
    function it_serializes_filtered_projects_without_organization_id(
96
        FilterProjectsQuery $query,
97
        ProjectRepository $repository,
98
        Project $project,
99
        ProjectDataTransformer $dataTransformer,
100
        OrganizationRepository $organizationRepository,
101
        Organization $organization,
102
        OrganizationId $organizationId
103
    ) {
104
        $query->userId()->shouldBeCalled()->willReturn('user-id');
105
        $query->organizationId()->shouldBeCalled()->willReturn(null);
106
        $organizationRepository->organizationOfId(Argument::type(OrganizationId::class))
107
            ->shouldBeCalled()->willReturn(null);
108
        $organizationRepository->query(Argument::any())->shouldBeCalled()->willReturn([$organization]);
109
        $organization->id()->shouldBeCalled()->willReturn($organizationId);
110
        $query->name()->shouldBeCalled()->willReturn('project name');
111
        $query->offset()->shouldBeCalled()->willReturn(0);
112
        $query->limit()->shouldBeCalled()->willReturn(-1);
113
        $repository->query(Argument::any())->shouldBeCalled()->willReturn([$project]);
114
        $dataTransformer->write($project)->shouldBeCalled();
115
        $dataTransformer->read()->shouldBeCalled();
116
        $this->__invoke($query)->shouldBeArray();
117
    }
118
119 View Code Duplication
    function it_serializes_filtered_projects_without_project_name(
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...
120
        FilterProjectsQuery $query,
121
        ProjectRepository $repository,
122
        Project $project,
123
        ProjectDataTransformer $dataTransformer,
124
        OrganizationRepository $organizationRepository,
125
        Organization $organization
126
    ) {
127
        $query->userId()->shouldBeCalled()->willReturn('user-id');
128
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
129
        $organizationRepository->organizationOfId(OrganizationId::generate('organization-id'))
130
            ->shouldBeCalled()->willReturn($organization);
131
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
132
        $query->name()->shouldBeCalled()->willReturn(null);
133
        $query->offset()->shouldBeCalled()->willReturn(0);
134
        $query->limit()->shouldBeCalled()->willReturn(-1);
135
        $repository->query(Argument::any())->shouldBeCalled()->willReturn([$project]);
136
        $dataTransformer->write($project)->shouldBeCalled();
137
        $dataTransformer->read()->shouldBeCalled();
138
        $this->__invoke($query)->shouldBeArray();
139
    }
140
141
    function it_does_not_serialize_when_user_does_not_allow_to_perform_this_action(
142
        FilterProjectsQuery $query,
143
        OrganizationRepository $organizationRepository,
144
        Organization $organization
145
    ) {
146
        $query->userId()->shouldBeCalled()->willReturn('user-id');
147
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
148
        $organizationRepository->organizationOfId(OrganizationId::generate('organization-id'))
149
            ->shouldBeCalled()->willReturn($organization);
150
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(false);
151
        $this->shouldThrow(UnauthorizedProjectResourceException::class)->during__invoke($query);
152
    }
153
}
154