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\Component\Channel\Context; |
15
|
|
|
|
16
|
|
|
use Pamil\ProphecyCommon\Promise\CompositePromise; |
17
|
|
|
use PhpSpec\ObjectBehavior; |
18
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
19
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
20
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Kamil Kokot <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class CachedPerRequestChannelContextSpec extends ObjectBehavior |
28
|
|
|
{ |
29
|
|
|
function let(ChannelContextInterface $decoratedChannelContext, RequestStack $requestStack): void |
30
|
|
|
{ |
31
|
|
|
$this->beConstructedWith($decoratedChannelContext, $requestStack); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function it_implements_channel_context_interface(): void |
35
|
|
|
{ |
36
|
|
|
$this->shouldImplement(ChannelContextInterface::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function it_caches_channels_for_the_same_request( |
40
|
|
|
ChannelContextInterface $decoratedChannelContext, |
41
|
|
|
RequestStack $requestStack, |
42
|
|
|
Request $request, |
43
|
|
|
ChannelInterface $channel |
44
|
|
|
): void { |
45
|
|
|
$requestStack->getMasterRequest()->willReturn($request, $request); |
46
|
|
|
|
47
|
|
|
$decoratedChannelContext->getChannel()->willReturn($channel)->shouldBeCalledTimes(1); |
48
|
|
|
|
49
|
|
|
$this->getChannel()->shouldReturn($channel); |
50
|
|
|
$this->getChannel()->shouldReturn($channel); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function it_does_not_cache_channels_for_different_requests( |
54
|
|
|
ChannelContextInterface $decoratedChannelContext, |
55
|
|
|
RequestStack $requestStack, |
56
|
|
|
Request $firstRequest, |
57
|
|
|
Request $secondRequest, |
58
|
|
|
ChannelInterface $firstChannel, |
59
|
|
|
ChannelInterface $secondChannel |
60
|
|
|
): void { |
61
|
|
|
$requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest); |
62
|
|
|
|
63
|
|
|
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel); |
64
|
|
|
|
65
|
|
|
$this->getChannel()->shouldReturn($firstChannel); |
66
|
|
|
$this->getChannel()->shouldReturn($secondChannel); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function it_caches_channels_for_the_same_request_even_if_there_are_other_request_in_between( |
70
|
|
|
ChannelContextInterface $decoratedChannelContext, |
71
|
|
|
RequestStack $requestStack, |
72
|
|
|
Request $firstRequest, |
73
|
|
|
Request $secondRequest, |
74
|
|
|
ChannelInterface $firstChannel, |
75
|
|
|
ChannelInterface $secondChannel |
76
|
|
|
): void { |
77
|
|
|
$requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest, $firstRequest); |
78
|
|
|
|
79
|
|
|
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2); |
80
|
|
|
|
81
|
|
|
$this->getChannel()->shouldReturn($firstChannel); |
82
|
|
|
$this->getChannel()->shouldReturn($secondChannel); |
83
|
|
|
$this->getChannel()->shouldReturn($firstChannel); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function it_does_not_cache_results_while_there_are_no_master_requests( |
87
|
|
|
ChannelContextInterface $decoratedChannelContext, |
88
|
|
|
RequestStack $requestStack, |
89
|
|
|
ChannelInterface $firstChannel, |
90
|
|
|
ChannelInterface $secondChannel |
91
|
|
|
): void { |
92
|
|
|
$requestStack->getMasterRequest()->willReturn(null, null); |
93
|
|
|
|
94
|
|
|
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2); |
95
|
|
|
|
96
|
|
|
$this->getChannel()->shouldReturn($firstChannel); |
97
|
|
|
$this->getChannel()->shouldReturn($secondChannel); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
function it_caches_channel_not_found_exceptions_for_the_same_request( |
101
|
|
|
ChannelContextInterface $decoratedChannelContext, |
102
|
|
|
RequestStack $requestStack, |
103
|
|
|
Request $request |
104
|
|
|
): void { |
105
|
|
|
$requestStack->getMasterRequest()->willReturn($request, $request); |
106
|
|
|
|
107
|
|
|
$decoratedChannelContext->getChannel()->willThrow(ChannelNotFoundException::class)->shouldBeCalledTimes(1); |
108
|
|
|
|
109
|
|
|
$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel'); |
110
|
|
|
$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function it_does_not_cache_channel_not_found_exceptions_for_null_master_requests( |
114
|
|
|
ChannelContextInterface $decoratedChannelContext, |
115
|
|
|
RequestStack $requestStack, |
116
|
|
|
ChannelInterface $channel |
117
|
|
|
): void { |
118
|
|
|
$requestStack->getMasterRequest()->willReturn(null, null); |
119
|
|
|
|
120
|
|
|
$decoratedChannelContext |
121
|
|
|
->getChannel() |
122
|
|
|
->will(CompositePromise::it()->willThrow(ChannelNotFoundException::class)->andThenReturn($channel)) |
123
|
|
|
->shouldBeCalledTimes(2) |
124
|
|
|
; |
125
|
|
|
|
126
|
|
|
$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel'); |
127
|
|
|
$this->getChannel()->shouldReturn($channel); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|