|
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\DataProvider; |
|
15
|
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
|
17
|
|
|
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; |
|
18
|
|
|
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys; |
|
19
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
20
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
21
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
22
|
|
|
use Sylius\Component\Customer\Context\CustomerContextInterface; |
|
23
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
|
24
|
|
|
use Sylius\Component\User\Model\UserInterface; |
|
25
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
26
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
27
|
|
|
|
|
28
|
|
|
final class TaxonCollectionDataProviderSpec extends ObjectBehavior |
|
29
|
|
|
{ |
|
30
|
|
|
function let(TaxonRepositoryInterface $taxonRepository, UserContextInterface $userContext): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->beConstructedWith($taxonRepository, $userContext); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
function it_supports_only_taxons(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->supports(TaxonInterface::class, 'get')->shouldReturn(true); |
|
38
|
|
|
$this->supports(ProductInterface::class, 'get')->shouldReturn(false); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function it_throws_an_exception_if_context_has_not_channel(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this |
|
44
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
|
45
|
|
|
->during('getCollection', [TaxonInterface::class, 'get_from_menu_taxon', []]) |
|
46
|
|
|
; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_provides_taxon_from_channel_menu_taxon_if_logged_in_user_is_not_admin( |
|
50
|
|
|
TaxonRepositoryInterface $taxonRepository, |
|
51
|
|
|
UserContextInterface $userContext, |
|
52
|
|
|
TaxonInterface $menuTaxon, |
|
53
|
|
|
TaxonInterface $firstTaxon, |
|
54
|
|
|
TaxonInterface $secondTaxon, |
|
55
|
|
|
ChannelInterface $channel, |
|
56
|
|
|
UserInterface $user |
|
57
|
|
|
): void { |
|
58
|
|
|
$userContext->getUser()->willReturn($user); |
|
59
|
|
|
$user->getRoles()->willReturn(['ROLE_USER']); |
|
60
|
|
|
|
|
61
|
|
|
$channel->getMenuTaxon()->willReturn($menuTaxon); |
|
62
|
|
|
$taxonRepository->findChildrenByChannelMenuTaxon($menuTaxon)->willReturn([$firstTaxon, $secondTaxon]); |
|
63
|
|
|
|
|
64
|
|
|
$this |
|
65
|
|
|
->getCollection(TaxonInterface::class, 'get', [ContextKeys::CHANNEL => $channel]) |
|
66
|
|
|
->shouldReturn([$firstTaxon, $secondTaxon]) |
|
67
|
|
|
; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function it_provides_taxon_from_channel_menu_taxon_if_there_is_no_logged_in_user( |
|
71
|
|
|
TaxonRepositoryInterface $taxonRepository, |
|
72
|
|
|
UserContextInterface $userContext, |
|
73
|
|
|
TaxonInterface $menuTaxon, |
|
74
|
|
|
TaxonInterface $firstTaxon, |
|
75
|
|
|
TaxonInterface $secondTaxon, |
|
76
|
|
|
ChannelInterface $channel |
|
77
|
|
|
): void { |
|
78
|
|
|
$userContext->getUser()->willReturn(null); |
|
79
|
|
|
|
|
80
|
|
|
$channel->getMenuTaxon()->willReturn($menuTaxon); |
|
81
|
|
|
$taxonRepository->findChildrenByChannelMenuTaxon($menuTaxon)->willReturn([$firstTaxon, $secondTaxon]); |
|
82
|
|
|
|
|
83
|
|
|
$this |
|
84
|
|
|
->getCollection(TaxonInterface::class, 'get', [ContextKeys::CHANNEL => $channel]) |
|
85
|
|
|
->shouldReturn([$firstTaxon, $secondTaxon]) |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
function it_provides_all_taxons_if_logged_in_user_is_admin( |
|
90
|
|
|
TaxonRepositoryInterface $taxonRepository, |
|
91
|
|
|
UserContextInterface $userContext, |
|
92
|
|
|
TaxonInterface $menuTaxon, |
|
93
|
|
|
TaxonInterface $firstTaxon, |
|
94
|
|
|
TaxonInterface $secondTaxon, |
|
95
|
|
|
TaxonInterface $thirdTaxon, |
|
96
|
|
|
ChannelInterface $channel, |
|
97
|
|
|
UserInterface $user |
|
98
|
|
|
): void { |
|
99
|
|
|
$userContext->getUser()->willReturn($user); |
|
100
|
|
|
$user->getRoles()->willReturn(['ROLE_API_ACCESS']); |
|
101
|
|
|
|
|
102
|
|
|
$channel->getMenuTaxon()->willReturn($menuTaxon); |
|
103
|
|
|
$taxonRepository->findAll()->willReturn([$firstTaxon, $secondTaxon, $thirdTaxon]); |
|
104
|
|
|
|
|
105
|
|
|
$this |
|
106
|
|
|
->getCollection(TaxonInterface::class, 'get', [ContextKeys::CHANNEL => $channel]) |
|
107
|
|
|
->shouldReturn([$firstTaxon, $secondTaxon, $thirdTaxon]) |
|
108
|
|
|
; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|