|
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\Domain\Model\Organization; |
|
14
|
|
|
|
|
15
|
|
|
use Kreta\SharedKernel\Domain\Model\AggregateRoot; |
|
16
|
|
|
use Kreta\SharedKernel\Domain\Model\CollectionElementAlreadyAddedException; |
|
17
|
|
|
use Kreta\SharedKernel\Domain\Model\CollectionElementAlreadyRemovedException; |
|
18
|
|
|
use Kreta\SharedKernel\Domain\Model\Identity\Slug; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Member; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\MemberAdded; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\MemberId; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\MemberRemoved; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationCreated; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationEdited; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; |
|
27
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationName; |
|
28
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Owner; |
|
29
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OwnerAdded; |
|
30
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OwnerId; |
|
31
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OwnerRemoved; |
|
32
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedRemoveOwnerException; |
|
33
|
|
|
use PhpSpec\ObjectBehavior; |
|
34
|
|
|
|
|
35
|
|
|
class OrganizationSpec extends ObjectBehavior |
|
36
|
|
|
{ |
|
37
|
|
|
function let(OrganizationId $id, OrganizationName $name, Slug $slug, Owner $owner, OwnerId $ownerId) |
|
38
|
|
|
{ |
|
39
|
|
|
$id->id()->willReturn('organization-id'); |
|
40
|
|
|
$owner->id()->willReturn($ownerId); |
|
41
|
|
|
$this->beConstructedWith($id, $name, $slug, $owner); |
|
42
|
|
|
$this->shouldHavePublished(OrganizationCreated::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function it_can_be_created(Owner $owner, OrganizationName $name, Slug $slug) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->shouldHaveType(Organization::class); |
|
48
|
|
|
$this->shouldHaveType(AggregateRoot::class); |
|
49
|
|
|
$this->id()->shouldReturnAnInstanceOf(OrganizationId::class); |
|
50
|
|
|
$this->__toString()->shouldReturn('organization-id'); |
|
51
|
|
|
$this->name()->shouldReturn($name); |
|
52
|
|
|
$this->slug()->shouldReturn($slug); |
|
53
|
|
|
$this->createdOn()->shouldReturnAnInstanceOf(\DateTimeImmutable::class); |
|
54
|
|
|
$this->updatedOn()->shouldReturnAnInstanceOf(\DateTimeImmutable::class); |
|
55
|
|
|
$this->owners()->shouldReturnCollection([$owner]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
function it_can_be_edited(OrganizationName $name, OrganizationName $name2, Slug $slug, Slug $slug2) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->name()->shouldReturn($name); |
|
61
|
|
|
$this->slug()->shouldReturn($slug); |
|
62
|
|
|
$this->edit($name2, $slug2); |
|
63
|
|
|
$this->shouldHavePublished(OrganizationEdited::class); |
|
64
|
|
|
|
|
65
|
|
|
$this->name()->shouldReturn($name2); |
|
66
|
|
|
$this->slug()->shouldReturn($slug2); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
function it_allows_adding_a_new_owner(Owner $owner, Owner $owner2, OwnerId $ownerId) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$this->owners()->shouldReturnCollection([$owner]); |
|
72
|
|
|
$owner2->id()->shouldBeCalled()->willReturn($ownerId); |
|
73
|
|
|
$this->addOwner($owner2); |
|
74
|
|
|
$this->shouldHavePublished(OwnerAdded::class); |
|
75
|
|
|
$this->owners()->shouldReturnCollection([$owner, $owner2]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
function it_does_not_allow_to_add_existing_owner(Owner $owner) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->shouldThrow(CollectionElementAlreadyAddedException::class)->during('addOwner', [$owner]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
function it_allows_removing_an_owner(Owner $owner, Owner $owner2, OwnerId $ownerId) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$owner2->id()->shouldBeCalled()->willReturn($ownerId); |
|
86
|
|
|
$this->addOwner($owner2); |
|
87
|
|
|
$this->shouldHavePublished(OwnerAdded::class); |
|
88
|
|
|
$this->removeOwner($owner2); |
|
89
|
|
|
$this->shouldHavePublished(OwnerRemoved::class); |
|
90
|
|
|
$this->owners()->shouldReturnCollection([$owner]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
function it_does_not_allow_removing_unexistent_owner(Owner $owner2, Owner $owner3, OwnerId $ownerId) |
|
94
|
|
|
{ |
|
95
|
|
|
$owner2->id()->shouldBeCalled()->willReturn($ownerId); |
|
96
|
|
|
$this->addOwner($owner2); |
|
97
|
|
|
$this->shouldThrow(CollectionElementAlreadyRemovedException::class)->during('removeOwner', [$owner3]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function it_allows_adding_a_new_member(Member $member, MemberId $memberId) |
|
101
|
|
|
{ |
|
102
|
|
|
$member->id()->shouldBeCalled()->willReturn($memberId); |
|
103
|
|
|
$this->addMember($member); |
|
104
|
|
|
$this->shouldHavePublished(MemberAdded::class); |
|
105
|
|
|
$this->members()->shouldReturnCollection([$member]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
function it_does_not_allow_to_add_existing_member(Member $member, MemberId $memberId) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$member->id()->shouldBeCalled()->willReturn($memberId); |
|
111
|
|
|
$this->addMember($member); |
|
112
|
|
|
$this->shouldHavePublished(MemberAdded::class); |
|
113
|
|
|
$this->shouldThrow(CollectionElementAlreadyAddedException::class)->during('addMember', [$member]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
function it_allows_removing_a_member(Member $member, MemberId $memberId) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$member->id()->shouldBeCalled()->willReturn($memberId); |
|
119
|
|
|
$this->addMember($member); |
|
120
|
|
|
$this->shouldHavePublished(MemberAdded::class); |
|
121
|
|
|
$this->removeMember($member); |
|
122
|
|
|
$this->shouldHavePublished(MemberRemoved::class); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
function it_does_not_allow_removing_unexistent_member(Member $member) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->shouldThrow(CollectionElementAlreadyRemovedException::class)->during('removeMember', [$member]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
function it_does_not_remove_owner_because_all_the_organizations_need_one_owner_at_least(Owner $owner) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->shouldThrow(UnauthorizedRemoveOwnerException::class)->during('removeOwner', [$owner]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
function it_checks_if_it_is_owner(OwnerId $ownerId, Owner $owner, OwnerId $ownerId2) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->owners()->shouldReturnCollection([$owner]); |
|
138
|
|
|
$owner->id()->shouldBeCalled()->willReturn($ownerId); |
|
139
|
|
|
$ownerId->equals($ownerId)->shouldBeCalled()->willReturn(true); |
|
140
|
|
|
$this->isOwner($ownerId)->shouldReturn(true); |
|
141
|
|
|
|
|
142
|
|
|
$this->owners()->shouldReturnCollection([$owner]); |
|
143
|
|
|
$owner->id()->shouldBeCalled()->willReturn($ownerId2); |
|
144
|
|
|
$ownerId->equals($ownerId2)->shouldBeCalled()->willReturn(false); |
|
145
|
|
|
$this->isOwner($ownerId)->shouldReturn(false); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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.