Completed
Push — master ( 1bb311...4030e5 )
by Gorka
11s
created

ProjectOfSlugHandlerSpec   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 71
c 0
b 0
f 0
wmc 4
lcom 1
cbo 10
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 17 1
A it_serializes_project() 0 20 1
A it_does_not_serialize_project_when_the_project_does_not_exist() 0 9 1
A it_does_not_serialize_project_when_the_user_does_not_allowed() 0 15 1
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
declare(strict_types=1);
14
15
namespace Spec\Kreta\TaskManager\Application\Query\Project;
16
17
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
18
use Kreta\TaskManager\Application\DataTransformer\Project\ProjectDataTransformer;
19
use Kreta\TaskManager\Application\Query\Project\ProjectOfSlugHandler;
20
use Kreta\TaskManager\Application\Query\Project\ProjectOfSlugQuery;
21
use Kreta\TaskManager\Domain\Model\Organization\Organization;
22
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
23
use Kreta\TaskManager\Domain\Model\Project\Project;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
25
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
26
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
27
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
28
use Kreta\TaskManager\Domain\Model\User\UserId;
29
use PhpSpec\ObjectBehavior;
30
use Prophecy\Argument;
31
32
class ProjectOfSlugHandlerSpec extends ObjectBehavior
33
{
34
    private $slug;
35
    private $organizationSlug;
36
    private $userId;
37
38
    function let(
39
        ProjectOfSlugQuery $query,
40
        OrganizationRepository $organizationRepository,
41
        ProjectRepository $repository,
42
        ProjectSpecificationFactory $specificationFactory,
43
        ProjectDataTransformer $dataTransformer
44
    ) {
45
        $query->slug()->shouldBeCalled()->willReturn('project-slug');
46
        $query->organizationSlug()->shouldBeCalled()->willReturn('organization-slug');
47
        $query->userId()->shouldBeCalled()->willReturn('user-id');
48
49
        $this->slug = new Slug('project-slug');
50
        $this->organizationSlug = new Slug('organization-slug');
51
        $this->userId = UserId::generate('user-id');
52
53
        $this->beConstructedWith($organizationRepository, $repository, $specificationFactory, $dataTransformer);
54
    }
55
56
    function it_serializes_project(
57
        ProjectOfSlugQuery $query,
58
        ProjectRepository $repository,
59
        Project $project,
60
        OrganizationRepository $organizationRepository,
61
        Organization $organization,
62
        ProjectSpecificationFactory $specificationFactory,
63
        ProjectDataTransformer $dataTransformer
64
    ) {
65
        $this->shouldHaveType(ProjectOfSlugHandler::class);
66
67
        $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled();
68
        $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn($project);
69
        $organizationRepository->organizationOfSlug($this->organizationSlug)
70
            ->shouldBeCalled()->willReturn($organization);
71
        $organization->isOrganizationMember($this->userId)->shouldBeCalled()->willReturn(true);
72
        $dataTransformer->write($project)->shouldBeCalled();
73
        $dataTransformer->read()->shouldBeCalled();
74
        $this->__invoke($query);
75
    }
76
77
    function it_does_not_serialize_project_when_the_project_does_not_exist(
78
        ProjectOfSlugQuery $query,
79
        ProjectRepository $repository,
80
        ProjectSpecificationFactory $specificationFactory
81
    ) {
82
        $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled();
83
        $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn(null);
84
        $this->shouldThrow(ProjectDoesNotExistException::class)->during__invoke($query);
85
    }
86
87
    function it_does_not_serialize_project_when_the_user_does_not_allowed(
88
        ProjectOfSlugQuery $query,
89
        ProjectRepository $repository,
90
        Project $project,
91
        ProjectSpecificationFactory $specificationFactory,
92
        OrganizationRepository $organizationRepository,
93
        Organization $organization
94
    ) {
95
        $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled();
96
        $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn($project);
97
        $organizationRepository->organizationOfSlug($this->organizationSlug)
98
            ->shouldBeCalled()->willReturn($organization);
99
        $organization->isOrganizationMember($this->userId)->shouldBeCalled()->willReturn(false);
100
        $this->shouldThrow(UnauthorizedProjectResourceException::class)->during__invoke($query);
101
    }
102
}
103