|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.