Completed
Push — master ( 0bb8f9...15e42a )
by Beñat
06:24 queued 02:36
created

EditOrganizationHandlerSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Command\Organization;
14
15
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
16
use Kreta\TaskManager\Application\Command\Organization\EditOrganizationCommand;
17
use Kreta\TaskManager\Application\Command\Organization\EditOrganizationHandler;
18
use Kreta\TaskManager\Domain\Model\Organization\Organization;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
22
use Kreta\TaskManager\Domain\Model\Organization\OrganizationName;
23
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
24
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedEditOrganizationException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
use PhpSpec\ObjectBehavior;
27
use Prophecy\Argument;
28
29
class EditOrganizationHandlerSpec extends ObjectBehavior
30
{
31
    private $slug;
32
    private $name;
33
    private $organizationId;
34
    private $editorId;
35
36 View Code Duplication
    function let(OrganizationRepository $repository, EditOrganizationCommand $command)
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...
37
    {
38
        $this->beConstructedWith($repository);
39
40
        $command->name()->shouldBeCalled()->willReturn('Organization name');
41
        $command->slug()->shouldBeCalled()->willReturn('organization-slug');
42
        $command->id()->shouldBeCalled()->willReturn('organization-id');
43
        $command->editorId()->shouldBeCalled()->willReturn('editor-id');
44
45
        $this->slug = new Slug('organization-slug');
46
        $this->name = new OrganizationName('Organization name');
47
        $this->organizationId = OrganizationId::generate('organization-id');
48
        $this->editorId = UserId::generate('editor-id');
49
    }
50
51
    function it_edits_an_organization(
52
        EditOrganizationCommand $command,
53
        OrganizationRepository $repository,
54
        Organization $organization
55
    ) {
56
        $this->shouldHaveType(EditOrganizationHandler::class);
57
58
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
59
        $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);
60
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn(null);
61
        $organization->edit($this->name, $this->slug)->shouldBeCalled();
62
        $repository->persist(Argument::type(Organization::class))->shouldBeCalled();
63
        $this->__invoke($command);
64
    }
65
66
    function it_edits_an_organization_with_the_same_slug(
67
        EditOrganizationCommand $command,
68
        OrganizationRepository $repository,
69
        Organization $organization,
70
        Organization $organization2
71
    ) {
72
        $this->shouldHaveType(EditOrganizationHandler::class);
73
74
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
75
        $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);
76
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization2);
77
        $organization2->id()->shouldBeCalled()->willReturn($this->organizationId);
78
        $organization->edit($this->name, $this->slug)->shouldBeCalled();
79
        $repository->persist(Argument::type(Organization::class))->shouldBeCalled();
80
        $this->__invoke($command);
81
    }
82
83
    function it_edits_an_organization_without_slug(
84
        EditOrganizationCommand $command,
85
        OrganizationRepository $repository,
86
        Organization $organization
87
    ) {
88
        $command->slug()->shouldBeCalled()->willReturn(null);
89
        $slug = new Slug('Organization name');
90
91
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
92
        $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);
93
        $repository->organizationOfSlug($slug)->shouldBeCalled()->willReturn(null);
94
        $organization->edit($this->name, $slug)->shouldBeCalled();
95
        $repository->persist(Argument::type(Organization::class))->shouldBeCalled();
96
        $this->__invoke($command);
97
    }
98
99
    function it_does_not_edits_an_organization_because_the_organization_does_not_exist(
100
        EditOrganizationCommand $command,
101
        OrganizationRepository $repository
102
    ) {
103
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);
104
        $this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($command);
105
    }
106
107
    function it_does_not_edits_an_organization_because_the_organization_slug_already_exists(
108
        EditOrganizationCommand $command,
109
        OrganizationRepository $repository,
110
        Organization $organization,
111
        Organization $organization2
112
    ) {
113
        $organizationId2 = OrganizationId::generate('organization-id-2');
114
115
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
116
        $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(true);
117
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization2);
118
        $organization2->id()->shouldBeCalled()->willReturn($organizationId2);
119
        $this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command);
120
    }
121
122 View Code Duplication
    function it_does_not_edits_an_organization_because_the_owner_does_not_authorized(
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...
123
        EditOrganizationCommand $command,
124
        OrganizationRepository $repository,
125
        Organization $organization
126
    ) {
127
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
128
        $organization->isOwner($this->editorId)->shouldBeCalled()->willReturn(false);
129
        $this->shouldThrow(UnauthorizedEditOrganizationException::class)->during__invoke($command);
130
    }
131
}
132