Completed
Push — master ( cdb55b...3f24cf )
by Gorka
9s
created

it_counts_without_project_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 7
rs 9.4285
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\Project\Task;
14
15
use Kreta\TaskManager\Application\Query\Project\Task\CountTasksHandler;
16
use Kreta\TaskManager\Application\Query\Project\Task\CountTasksQuery;
17
use Kreta\TaskManager\Domain\Model\Organization\Organization;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
20
use Kreta\TaskManager\Domain\Model\Project\Project;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
22
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskSpecificationFactory;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
25
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
26
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
27
use Kreta\TaskManager\Domain\Model\User\UserId;
28
use PhpSpec\ObjectBehavior;
29
use Prophecy\Argument;
30
31
class CountTasksHandlerSpec extends ObjectBehavior
32
{
33
    function let(
34
        ProjectRepository $projectRepository,
35
        ProjectSpecificationFactory $projectSpecificationFactory,
36
        OrganizationRepository $organizationRepository,
37
        TaskRepository $repository,
38
        TaskSpecificationFactory $specificationFactory
39
    ) {
40
        $this->beConstructedWith(
41
            $projectRepository,
42
            $projectSpecificationFactory,
43
            $organizationRepository,
44
            $repository,
45
            $specificationFactory
46
        );
47
    }
48
49
    function it_is_initializable()
50
    {
51
        $this->shouldHaveType(CountTasksHandler::class);
52
    }
53
54
    function it_counts_tasks(
55
        CountTasksQuery $query,
56
        ProjectRepository $projectRepository,
57
        OrganizationRepository $organizationRepository,
58
        TaskRepository $repository,
59
        Organization $organization,
60
        Project $project,
61
        OrganizationId $organizationId
62
    ) {
63
        $query->userId()->shouldBeCalled()->willReturn('user-id');
64
        $query->projectId()->shouldBeCalled()->willReturn('project-id');
65
        $projectRepository->projectOfId(ProjectId::generate('project-id'))
66
            ->shouldBeCalled()->willReturn($project);
67
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
68
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
69
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
70
        $query->title()->shouldBeCalled()->willReturn('task title');
71
        $repository->count(Argument::any())->shouldBeCalled()->willReturn(2);
72
        $this->__invoke($query)->shouldReturn(2);
73
    }
74
75
    function it_counts_without_task_title(
76
        CountTasksQuery $query,
77
        ProjectRepository $projectRepository,
78
        OrganizationRepository $organizationRepository,
79
        TaskRepository $repository,
80
        Organization $organization,
81
        Project $project,
82
        OrganizationId $organizationId
83
    ) {
84
        $query->userId()->shouldBeCalled()->willReturn('user-id');
85
        $query->projectId()->shouldBeCalled()->willReturn('project-id');
86
        $projectRepository->projectOfId(ProjectId::generate('project-id'))
87
            ->shouldBeCalled()->willReturn($project);
88
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
89
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
90
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
91
        $query->title()->shouldBeCalled();
92
        $repository->count(Argument::any())->shouldBeCalled()->willReturn(3);
93
        $this->__invoke($query)->shouldReturn(3);
94
    }
95
96
    function it_counts_without_project_id(
97
        CountTasksQuery $query,
98
        ProjectRepository $projectRepository,
99
        OrganizationRepository $organizationRepository,
100
        TaskRepository $repository,
101
        Organization $organization,
102
        Project $project,
103
        OrganizationId $organizationId
104
    ) {
105
        $query->userId()->shouldBeCalled()->willReturn('user-id');
106
        $query->projectId()->shouldBeCalled();
107
        $projectRepository->projectOfId(Argument::type(ProjectId::class))
108
            ->shouldBeCalled()->willReturn($project);
109
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
110
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
111
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true);
112
        $query->title()->shouldBeCalled()->willReturn('task title');
113
        $repository->count(Argument::any())->shouldBeCalled()->willReturn(3);
114
        $this->__invoke($query)->shouldReturn(3);
115
    }
116
117 View Code Duplication
    function it_does_not_count_when_user_does_not_allow_to_perform_this_action(
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...
118
        CountTasksQuery $query,
119
        ProjectRepository $projectRepository,
120
        OrganizationRepository $organizationRepository,
121
        Organization $organization,
122
        Project $project,
123
        OrganizationId $organizationId
124
    ) {
125
        $query->userId()->shouldBeCalled()->willReturn('user-id');
126
        $query->projectId()->shouldBeCalled()->willReturn('project-id');
127
        $projectRepository->projectOfId(Argument::type(ProjectId::class))
128
            ->shouldBeCalled()->willReturn($project);
129
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
130
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
131
        $organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(false);
132
        $this->shouldThrow(UnauthorizedProjectResourceException::class)->during__invoke($query);
133
    }
134
}
135