Completed
Pull Request — master (#212)
by Beñat
06:38 queued 02:56
created

OwnerOfIdHandlerSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 4 4 1
A it_is_initializable() 4 4 1
B it_serializes_owner() 24 24 1
A it_does_not_serialize_owner_because_the_organization_does_not_exist() 10 10 1
A it_does_not_serialize_owner_because_it_does_not_exist() 17 17 1
A it_does_not_serialize_owner_when_the_user_does_not_allow_to_perform_this_action() 13 13 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\Query\Organization;
14
15
use Kreta\TaskManager\Application\DataTransformer\Organization\MemberDataTransformer;
16
use Kreta\TaskManager\Application\Query\Organization\OwnerOfIdHandler;
17
use Kreta\TaskManager\Application\Query\Organization\OwnerOfIdQuery;
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\Organization\Owner;
23
use Kreta\TaskManager\Domain\Model\Organization\OwnerDoesNotExistException;
24
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
use PhpSpec\ObjectBehavior;
27
28 View Code Duplication
class OwnerOfIdHandlerSpec extends ObjectBehavior
0 ignored issues
show
Duplication introduced by
This class 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...
29
{
30
    function let(OrganizationRepository $repository, MemberDataTransformer $dataTransformer)
31
    {
32
        $this->beConstructedWith($repository, $dataTransformer);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType(OwnerOfIdHandler::class);
38
    }
39
40
    function it_serializes_owner(
41
        OwnerOfIdQuery $query,
42
        OrganizationRepository $repository,
43
        Organization $organization,
44
        MemberDataTransformer $dataTransformer,
45
        Owner $owner
46
    ) {
47
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
48
        $repository->organizationOfId(
49
            OrganizationId::generate('organization-id')
50
        )->shouldBeCalled()->willReturn($organization);
51
        $query->userId()->shouldBeCalled()->willReturn('user-id');
52
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
53
        $query->ownerId()->shouldBeCalled()->willReturn('owner-id');
54
        $organization->isOwner(
55
            UserId::generate('owner-id')
56
        )->shouldBeCalled()->willReturn(true);
57
        $organization->owner(UserId::generate('owner-id'))
58
            ->shouldBeCalled()->willReturn($owner);
59
60
        $dataTransformer->write($owner)->shouldBeCalled();
61
        $dataTransformer->read()->shouldBeCalled();
62
        $this->__invoke($query);
63
    }
64
65
    function it_does_not_serialize_owner_because_the_organization_does_not_exist(
66
        OwnerOfIdQuery $query,
67
        OrganizationRepository $repository
68
    ) {
69
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
70
        $repository->organizationOfId(
71
            OrganizationId::generate('organization-id')
72
        )->shouldBeCalled()->willReturn(null);
73
        $this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($query);
74
    }
75
76
    function it_does_not_serialize_owner_because_it_does_not_exist(
77
        OwnerOfIdQuery $query,
78
        OrganizationRepository $repository,
79
        Organization $organization
80
    ) {
81
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
82
        $repository->organizationOfId(
83
            OrganizationId::generate('organization-id')
84
        )->shouldBeCalled()->willReturn($organization);
85
        $query->userId()->shouldBeCalled()->willReturn('user-id');
86
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
87
        $query->ownerId()->shouldBeCalled()->willReturn('owner-id');
88
        $organization->isOwner(
89
            UserId::generate('owner-id')
90
        )->shouldBeCalled()->willReturn(false);
91
        $this->shouldThrow(OwnerDoesNotExistException::class)->during__invoke($query);
92
    }
93
94
    function it_does_not_serialize_owner_when_the_user_does_not_allow_to_perform_this_action(
95
        OwnerOfIdQuery $query,
96
        OrganizationRepository $repository,
97
        Organization $organization
98
    ) {
99
        $query->organizationId()->shouldBeCalled()->willReturn('organization-id');
100
        $repository->organizationOfId(
101
            OrganizationId::generate('organization-id')
102
        )->shouldBeCalled()->willReturn($organization);
103
        $query->userId()->shouldBeCalled()->willReturn('user-id');
104
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(false);
105
        $this->shouldThrow(UnauthorizedOrganizationActionException::class)->during__invoke($query);
106
    }
107
}
108