|
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\OrganizationMemberOfIdHandler; |
|
17
|
|
|
use Kreta\TaskManager\Application\Query\Organization\OrganizationMemberOfIdQuery; |
|
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\OrganizationMember; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationMemberDoesNotExistException; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
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 OrganizationMemberOfIdHandlerSpec extends ObjectBehavior |
|
|
|
|
|
|
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(OrganizationMemberOfIdHandler::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function it_serializes_organization_member( |
|
41
|
|
|
OrganizationMemberOfIdQuery $query, |
|
42
|
|
|
OrganizationRepository $repository, |
|
43
|
|
|
Organization $organization, |
|
44
|
|
|
MemberDataTransformer $dataTransformer, |
|
45
|
|
|
OrganizationMember $organizationMember |
|
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( |
|
53
|
|
|
UserId::generate('user-id') |
|
54
|
|
|
)->shouldBeCalled()->willReturn(true); |
|
55
|
|
|
$query->memberId()->shouldBeCalled()->willReturn('member-id'); |
|
56
|
|
|
$organization->isOrganizationMember( |
|
57
|
|
|
UserId::generate('member-id') |
|
58
|
|
|
)->shouldBeCalled()->willReturn(true); |
|
59
|
|
|
$organization->organizationMember(UserId::generate('member-id')) |
|
60
|
|
|
->shouldBeCalled()->willReturn($organizationMember); |
|
61
|
|
|
$dataTransformer->write($organizationMember)->shouldBeCalled(); |
|
62
|
|
|
$dataTransformer->read()->shouldBeCalled(); |
|
63
|
|
|
$this->__invoke($query); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function it_does_not_serialize_organization_member_because_the_organization_does_not_exist( |
|
67
|
|
|
OrganizationMemberOfIdQuery $query, |
|
68
|
|
|
OrganizationRepository $repository |
|
69
|
|
|
) { |
|
70
|
|
|
$query->organizationId()->shouldBeCalled()->willReturn('organization-id'); |
|
71
|
|
|
$repository->organizationOfId( |
|
72
|
|
|
OrganizationId::generate('organization-id') |
|
73
|
|
|
)->shouldBeCalled()->willReturn(null); |
|
74
|
|
|
$this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($query); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
function it_does_not_serialize_organization_member_because_it_does_not_exist( |
|
78
|
|
|
OrganizationMemberOfIdQuery $query, |
|
79
|
|
|
OrganizationRepository $repository, |
|
80
|
|
|
Organization $organization |
|
81
|
|
|
) { |
|
82
|
|
|
$query->organizationId()->shouldBeCalled()->willReturn('organization-id'); |
|
83
|
|
|
$repository->organizationOfId( |
|
84
|
|
|
OrganizationId::generate('organization-id') |
|
85
|
|
|
)->shouldBeCalled()->willReturn($organization); |
|
86
|
|
|
$query->userId()->shouldBeCalled()->willReturn('user-id'); |
|
87
|
|
|
$organization->isOrganizationMember( |
|
88
|
|
|
UserId::generate('user-id') |
|
89
|
|
|
)->shouldBeCalled()->willReturn(true); |
|
90
|
|
|
$query->memberId()->shouldBeCalled()->willReturn('member-id'); |
|
91
|
|
|
$organization->isOrganizationMember( |
|
92
|
|
|
UserId::generate('member-id') |
|
93
|
|
|
)->shouldBeCalled()->willReturn(false); |
|
94
|
|
|
$this->shouldThrow(OrganizationMemberDoesNotExistException::class)->during__invoke($query); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function it_does_not_serialize_organization_member_when_the_user_does_not_allow_to_perform_this_action( |
|
98
|
|
|
OrganizationMemberOfIdQuery $query, |
|
99
|
|
|
OrganizationRepository $repository, |
|
100
|
|
|
Organization $organization |
|
101
|
|
|
) { |
|
102
|
|
|
$query->organizationId()->shouldBeCalled()->willReturn('organization-id'); |
|
103
|
|
|
$repository->organizationOfId( |
|
104
|
|
|
OrganizationId::generate('organization-id') |
|
105
|
|
|
)->shouldBeCalled()->willReturn($organization); |
|
106
|
|
|
$query->userId()->shouldBeCalled()->willReturn('user-id'); |
|
107
|
|
|
$organization->isOrganizationMember( |
|
108
|
|
|
UserId::generate('user-id') |
|
109
|
|
|
)->shouldBeCalled()->willReturn(false); |
|
110
|
|
|
$this->shouldThrow(UnauthorizedOrganizationActionException::class)->during__invoke($query); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.