Completed
Push — docs-installation-setup ( 464016 )
by Kamil
05:18
created

ProductsByChannelAndLocaleCodeExtensionSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_does_nothing_if_current_resource_is_not_a_product() 0 11 1
A it_throws_an_exception_if_context_has_no_channel_for_shop_user() 0 14 1
A it_throws_an_exception_if_context_has_no_locale_for_shop_user() 0 15 1
A it_does_nothing_if_current_user_is_an_admin_user() 0 14 1
A it_filters_products_by_channel_and_locale_code_for_shop_user() 0 19 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\ApiBundle\Doctrine\QueryCollectionExtension;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
17
use Doctrine\ORM\QueryBuilder;
18
use PhpSpec\ObjectBehavior;
19
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
20
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
21
use Sylius\Component\Core\Model\ChannelInterface;
22
use Sylius\Component\Core\Model\ProductInterface;
23
use Sylius\Component\Core\Model\TaxonInterface;
24
use Symfony\Component\Security\Core\User\UserInterface;
25
26
class ProductsByChannelAndLocaleCodeExtensionSpec extends ObjectBehavior
27
{
28
    function let(UserContextInterface $userContext): void
29
    {
30
        $this->beConstructedWith($userContext);
31
    }
32
33
    function it_does_nothing_if_current_resource_is_not_a_product(
34
        UserContextInterface $userContext,
35
        QueryBuilder $queryBuilder,
36
        QueryNameGeneratorInterface $queryNameGenerator
37
    ): void {
38
        $userContext->getUser()->shouldNotBeCalled();
39
        $queryBuilder->getRootAliases()->shouldNotBeCalled();
40
        $queryBuilder->addSelect('translation')->shouldNotBeCalled();
41
42
        $this->applyToCollection($queryBuilder, $queryNameGenerator, TaxonInterface::class, 'get', []);
43
    }
44
45
    function it_throws_an_exception_if_context_has_no_channel_for_shop_user(
46
        UserContextInterface $userContext,
47
        UserInterface $user,
48
        QueryBuilder $queryBuilder,
49
        QueryNameGeneratorInterface $queryNameGenerator
50
    ): void {
51
        $userContext->getUser()->willReturn($user);
52
        $user->getRoles()->willReturn([]);
53
54
        $this
55
            ->shouldThrow(\InvalidArgumentException::class)
56
            ->during('applyToCollection', [$queryBuilder, $queryNameGenerator, ProductInterface::class, 'get', []])
57
        ;
58
    }
59
60
    function it_throws_an_exception_if_context_has_no_locale_for_shop_user(
61
        UserContextInterface $userContext,
62
        UserInterface $user,
63
        QueryBuilder $queryBuilder,
64
        QueryNameGeneratorInterface $queryNameGenerator,
65
        ChannelInterface $channel
66
    ): void {
67
        $userContext->getUser()->willReturn($user);
68
        $user->getRoles()->willReturn([]);
69
70
        $this
71
            ->shouldThrow(\InvalidArgumentException::class)
72
            ->during('applyToCollection', [$queryBuilder, $queryNameGenerator, ProductInterface::class, 'get', [ContextKeys::CHANNEL => $channel]])
73
        ;
74
    }
75
76
    function it_does_nothing_if_current_user_is_an_admin_user(
77
        UserContextInterface $userContext,
78
        UserInterface $user,
79
        QueryBuilder $queryBuilder,
80
        QueryNameGeneratorInterface $queryNameGenerator
81
    ): void {
82
        $userContext->getUser()->willReturn($user);
83
        $user->getRoles()->willReturn(['ROLE_API_ACCESS']);
84
85
        $queryBuilder->getRootAliases()->shouldNotBeCalled();
86
        $queryBuilder->addSelect('translation')->shouldNotBeCalled();
87
88
        $this->applyToCollection($queryBuilder, $queryNameGenerator, ProductInterface::class, 'get', []);
89
    }
90
91
    function it_filters_products_by_channel_and_locale_code_for_shop_user(
92
        UserContextInterface $userContext,
93
        UserInterface $user,
94
        QueryBuilder $queryBuilder,
95
        QueryNameGeneratorInterface $queryNameGenerator,
96
        ChannelInterface $channel
97
    ): void {
98
        $userContext->getUser()->willReturn($user);
99
        $user->getRoles()->willReturn([]);
100
101
        $queryBuilder->getRootAliases()->willReturn(['o']);
102
        $queryBuilder->addSelect('translation')->shouldBeCalled()->willReturn($queryBuilder);
103
        $queryBuilder->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')->shouldBeCalled()->willReturn($queryBuilder);
104
        $queryBuilder->andWhere(':channel MEMBER OF o.channels')->shouldBeCalled()->willReturn($queryBuilder);
105
        $queryBuilder->setParameter('channel', $channel)->shouldBeCalled()->willReturn($queryBuilder);
106
        $queryBuilder->setParameter('localeCode', 'en_US')->shouldBeCalled()->willReturn($queryBuilder);
107
108
        $this->applyToCollection($queryBuilder, $queryNameGenerator, ProductInterface::class, 'get', [ContextKeys::CHANNEL => $channel, ContextKeys::LOCALE_CODE => 'en_US']);
109
    }
110
}
111