|
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\Organization; |
|
14
|
|
|
|
|
15
|
|
|
use Kreta\SharedKernel\Domain\Model\Identity\Slug; |
|
16
|
|
|
use Kreta\TaskManager\Application\DataTransformer\Organization\OrganizationDataTransformer; |
|
17
|
|
|
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfSlugHandler; |
|
18
|
|
|
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfSlugQuery; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
24
|
|
|
use PhpSpec\ObjectBehavior; |
|
25
|
|
|
use Prophecy\Argument; |
|
26
|
|
|
|
|
27
|
|
|
class OrganizationOfSlugHandlerSpec extends ObjectBehavior |
|
28
|
|
|
{ |
|
29
|
|
|
function let(OrganizationRepository $repository, OrganizationDataTransformer $dataTransformer) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->beConstructedWith($repository, $dataTransformer); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
function it_is_initializable() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->shouldHaveType(OrganizationOfSlugHandler::class); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
function it_serializes_organization( |
|
|
|
|
|
|
40
|
|
|
OrganizationOfSlugQuery $query, |
|
41
|
|
|
OrganizationRepository $repository, |
|
42
|
|
|
Organization $organization, |
|
43
|
|
|
OrganizationDataTransformer $dataTransformer |
|
44
|
|
|
) { |
|
45
|
|
|
$query->organizationSlug()->shouldBeCalled()->willReturn('organization-slug'); |
|
46
|
|
|
$repository->organizationOfSlug(Argument::type(Slug::class))->shouldBeCalled()->willReturn($organization); |
|
47
|
|
|
$query->userId()->shouldBeCalled()->willReturn('user-id'); |
|
48
|
|
|
$organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(true); |
|
49
|
|
|
$dataTransformer->write($organization)->shouldBeCalled(); |
|
50
|
|
|
$dataTransformer->read()->shouldBeCalled(); |
|
51
|
|
|
$this->__invoke($query); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function it_does_not_serialize_organization_because_the_organization_does_not_exist( |
|
55
|
|
|
OrganizationOfSlugQuery $query, |
|
56
|
|
|
OrganizationRepository $repository |
|
57
|
|
|
) { |
|
58
|
|
|
$query->organizationSlug()->shouldBeCalled()->willReturn('organization-slug'); |
|
59
|
|
|
$repository->organizationOfSlug(Argument::type(Slug::class))->shouldBeCalled()->willReturn(null); |
|
60
|
|
|
$this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($query); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function it_does_not_serialize_organization_when_the_user_does_not_allow_to_perform_this_action( |
|
64
|
|
|
OrganizationOfSlugQuery $query, |
|
65
|
|
|
OrganizationRepository $repository, |
|
66
|
|
|
Organization $organization |
|
67
|
|
|
) { |
|
68
|
|
|
$query->organizationSlug()->shouldBeCalled()->willReturn('organization-slug'); |
|
69
|
|
|
$repository->organizationOfSlug(Argument::type(Slug::class))->shouldBeCalled()->willReturn($organization); |
|
70
|
|
|
$query->userId()->shouldBeCalled()->willReturn('user-id'); |
|
71
|
|
|
$organization->isOrganizationMember(UserId::generate('user-id'))->shouldBeCalled()->willReturn(false); |
|
72
|
|
|
$this->shouldThrow(UnauthorizedOrganizationActionException::class)->during__invoke($query); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
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.