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

it_creates_an_organization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 4
dl 0
loc 14
rs 9.4285
c 1
b 0
f 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\CreateOrganizationCommand;
17
use Kreta\TaskManager\Application\Command\Organization\CreateOrganizationHandler;
18
use Kreta\TaskManager\Domain\Model\Organization\Organization;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationAlreadyExistsException;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
22
use Kreta\TaskManager\Domain\Model\User\User;
23
use Kreta\TaskManager\Domain\Model\User\UserDoesNotExistException;
24
use Kreta\TaskManager\Domain\Model\User\UserId;
25
use Kreta\TaskManager\Domain\Model\User\UserRepository;
26
use PhpSpec\ObjectBehavior;
27
use Prophecy\Argument;
28
29
class CreateOrganizationHandlerSpec extends ObjectBehavior
30
{
31
    private $organizationId;
32
    private $slug;
33
    private $creatorId;
34
35 View Code Duplication
    function let(
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...
36
        OrganizationRepository $repository,
37
        UserRepository $userRepository,
38
        CreateOrganizationCommand $command
39
    ) {
40
        $this->beConstructedWith($repository, $userRepository);
41
42
        $command->name()->shouldBeCalled()->willReturn('organization name');
43
        $command->slug()->shouldBeCalled()->willReturn('organization-slug');
44
        $command->id()->shouldBeCalled()->willReturn('organization-id');
45
        $command->creatorId()->shouldBeCalled()->willReturn('user-id');
46
47
        $this->organizationId = OrganizationId::generate('organization-id');
48
        $this->slug = new Slug('organization-slug');
49
        $this->creatorId = UserId::generate('user-id');
50
    }
51
52
    function it_creates_an_organization(
53
        CreateOrganizationCommand $command,
54
        OrganizationRepository $repository,
55
        UserRepository $userRepository,
56
        User $user
57
    ) {
58
        $this->shouldHaveType(CreateOrganizationHandler::class);
59
60
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);
61
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn(null);
62
        $userRepository->userOfId($this->creatorId)->shouldBeCalled()->willReturn($user);
63
        $repository->persist(Argument::type(Organization::class))->shouldBeCalled();
64
        $this->__invoke($command);
65
    }
66
67
    function it_creates_an_organization_without_slug(
68
        CreateOrganizationCommand $command,
69
        OrganizationRepository $repository,
70
        UserRepository $userRepository,
71
        User $user
72
    ) {
73
        $command->slug()->shouldBeCalled()->willReturn(null);
74
        $slug = new Slug('organization name');
75
76
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);
77
        $repository->organizationOfSlug($slug)->shouldBeCalled()->willReturn(null);
78
        $userRepository->userOfId($this->creatorId)->shouldBeCalled()->willReturn($user);
79
        $repository->persist(Argument::type(Organization::class))->shouldBeCalled();
80
        $this->__invoke($command);
81
    }
82
83
    function it_does_not_create_an_organization_because_already_exists_an_organization_with_id(
84
        CreateOrganizationCommand $command,
85
        OrganizationRepository $repository,
86
        Organization $organization
87
    ) {
88
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization);
89
        $this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command);
90
    }
91
92 View Code Duplication
    function it_does_not_create_an_organization_because_already_exists_an_organization_with_slug(
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...
93
        CreateOrganizationCommand $command,
94
        OrganizationRepository $repository,
95
        Organization $organization
96
    ) {
97
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);
98
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn($organization);
99
        $this->shouldThrow(OrganizationAlreadyExistsException::class)->during__invoke($command);
100
    }
101
102
    function it_does_not_create_an_organization_because_creator_does_not_exist(
103
        CreateOrganizationCommand $command,
104
        OrganizationRepository $repository,
105
        UserRepository $userRepository
106
    ) {
107
        $repository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null);
108
        $repository->organizationOfSlug($this->slug)->shouldBeCalled()->willReturn(null);
109
        $userRepository->userOfId(Argument::type(UserId::class))->shouldBeCalled()->willReturn(null);
110
        $this->shouldThrow(UserDoesNotExistException::class)->during__invoke($command);
111
    }
112
}
113