Completed
Push — master ( 4e6953...230277 )
by Kamil
26:55
created

CachedPerRequestChannelContextSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 3
dl 0
loc 78
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_implements_channel_context_interface() 0 4 1
A it_caches_channels_for_the_same_request() 0 13 1
A it_does_not_cache_channels_for_different_requests() 0 15 1
A it_caches_channels_for_the_same_request_even_if_there_are_other_request_in_between() 0 16 1
A it_does_not_cache_results_while_there_are_no_master_requests() 0 13 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
namespace spec\Sylius\Component\Channel\Context;
13
14
use PhpSpec\ObjectBehavior;
15
use Sylius\Component\Channel\Context\CachedPerRequestChannelContext;
16
use Sylius\Component\Channel\Context\ChannelContextInterface;
17
use Sylius\Component\Channel\Model\ChannelInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
/**
22
 * @mixin CachedPerRequestChannelContext
23
 *
24
 * @author Kamil Kokot <[email protected]>
25
 */
26
class CachedPerRequestChannelContextSpec extends ObjectBehavior
27
{
28
    function let(ChannelContextInterface $decoratedChannelContext, RequestStack $requestStack)
29
    {
30
        $this->beConstructedWith($decoratedChannelContext, $requestStack);
31
    }
32
33
    function it_is_initializable()
34
    {
35
        $this->shouldHaveType('Sylius\Component\Channel\Context\CachedPerRequestChannelContext');
36
    }
37
38
    function it_implements_channel_context_interface()
39
    {
40
        $this->shouldImplement(ChannelContextInterface::class);
41
    }
42
43
    function it_caches_channels_for_the_same_request(
44
        ChannelContextInterface $decoratedChannelContext,
45
        RequestStack $requestStack,
46
        Request $request,
47
        ChannelInterface $channel
48
    ) {
49
        $requestStack->getMasterRequest()->willReturn($request, $request);
50
51
        $decoratedChannelContext->getChannel()->willReturn($channel)->shouldBeCalledTimes(1);
52
53
        $this->getChannel()->shouldReturn($channel);
54
        $this->getChannel()->shouldReturn($channel);
55
    }
56
57
    function it_does_not_cache_channels_for_different_requests(
58
        ChannelContextInterface $decoratedChannelContext,
59
        RequestStack $requestStack,
60
        Request $firstRequest,
61
        Request $secondRequest,
62
        ChannelInterface $firstChannel,
63
        ChannelInterface $secondChannel
64
    ) {
65
        $requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest);
66
67
        $decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel);
68
69
        $this->getChannel()->shouldReturn($firstChannel);
70
        $this->getChannel()->shouldReturn($secondChannel);
71
    }
72
73
    function it_caches_channels_for_the_same_request_even_if_there_are_other_request_in_between(
74
        ChannelContextInterface $decoratedChannelContext,
75
        RequestStack $requestStack,
76
        Request $firstRequest,
77
        Request $secondRequest,
78
        ChannelInterface $firstChannel,
79
        ChannelInterface $secondChannel
80
    ) {
81
        $requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest, $firstRequest);
82
83
        $decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2);
84
85
        $this->getChannel()->shouldReturn($firstChannel);
86
        $this->getChannel()->shouldReturn($secondChannel);
87
        $this->getChannel()->shouldReturn($firstChannel);
88
    }
89
90
    function it_does_not_cache_results_while_there_are_no_master_requests(
91
        ChannelContextInterface $decoratedChannelContext,
92
        RequestStack $requestStack,
93
        ChannelInterface $firstChannel,
94
        ChannelInterface $secondChannel
95
    ) {
96
        $requestStack->getMasterRequest()->willReturn(null, null);
97
98
        $decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2);
99
100
        $this->getChannel()->shouldReturn($firstChannel);
101
        $this->getChannel()->shouldReturn($secondChannel);
102
    }
103
}
104