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\ChannelBundle\Context\FakeChannel; |
15
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProviderInterface; |
19
|
|
|
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelPersister; |
20
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
24
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
25
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Kamil Kokot <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class FakeChannelPersisterSpec extends ObjectBehavior |
31
|
|
|
{ |
32
|
|
|
function let(FakeChannelCodeProviderInterface $fakeHostnameProvider): void |
33
|
|
|
{ |
34
|
|
|
$this->beConstructedWith($fakeHostnameProvider); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function it_applies_only_to_master_requests(FilterResponseEvent $filterResponseEvent): void |
38
|
|
|
{ |
39
|
|
|
$filterResponseEvent->getRequestType()->willReturn(HttpKernelInterface::SUB_REQUEST); |
40
|
|
|
|
41
|
|
|
$filterResponseEvent->getRequest()->shouldNotBeCalled(); |
42
|
|
|
$filterResponseEvent->getResponse()->shouldNotBeCalled(); |
43
|
|
|
|
44
|
|
|
$this->onKernelResponse($filterResponseEvent); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function it_applies_only_for_request_with_fake_channel_code( |
48
|
|
|
FakeChannelCodeProviderInterface $fakeHostnameProvider, |
49
|
|
|
FilterResponseEvent $filterResponseEvent, |
50
|
|
|
Request $request |
51
|
|
|
): void { |
52
|
|
|
$filterResponseEvent->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST); |
53
|
|
|
$filterResponseEvent->getRequest()->willReturn($request); |
54
|
|
|
|
55
|
|
|
$fakeHostnameProvider->getCode($request)->willReturn(null); |
56
|
|
|
|
57
|
|
|
$filterResponseEvent->getResponse()->shouldNotBeCalled(); |
58
|
|
|
|
59
|
|
|
$this->onKernelResponse($filterResponseEvent); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function it_persists_fake_channel_codes_in_a_cookie( |
63
|
|
|
FakeChannelCodeProviderInterface $fakeHostnameProvider, |
64
|
|
|
FilterResponseEvent $filterResponseEvent, |
65
|
|
|
Request $request, |
66
|
|
|
Response $response, |
67
|
|
|
ResponseHeaderBag $responseHeaderBag |
68
|
|
|
): void { |
69
|
|
|
$filterResponseEvent->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST); |
70
|
|
|
$filterResponseEvent->getRequest()->willReturn($request); |
71
|
|
|
|
72
|
|
|
$fakeHostnameProvider->getCode($request)->willReturn('fake_channel_code'); |
73
|
|
|
|
74
|
|
|
$filterResponseEvent->getResponse()->willReturn($response); |
75
|
|
|
|
76
|
|
|
$response->headers = $responseHeaderBag; |
77
|
|
|
$responseHeaderBag |
78
|
|
|
->setCookie(Argument::that(function (Cookie $cookie) { |
79
|
|
|
if ($cookie->getName() !== '_channel_code') { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($cookie->getValue() !== 'fake_channel_code') { |
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
})) |
89
|
|
|
->shouldBeCalled() |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this->onKernelResponse($filterResponseEvent); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|