|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (C) eZ Systems AS. All rights reserved. |
| 4 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 5 | + */ |
| 6 | +namespace spec\EzSystems\PlatformHttpCacheBundle\ProxyClient; |
| 7 | + |
| 8 | +use eZ\Publish\Core\MVC\ConfigResolverInterface; |
| 9 | +use FOS\HttpCache\ProxyClient\Dispatcher; |
| 10 | +use Http\Message\RequestFactory; |
| 11 | +use PhpSpec\ObjectBehavior; |
| 12 | +use Prophecy\Argument; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | + |
| 15 | +class VarnishSpec extends ObjectBehavior |
| 16 | +{ |
| 17 | + private const URI = "/"; |
| 18 | + private const REQUEST_HEADERS = [ |
| 19 | + "X-Some-Header" => "__SOME_HEADER_VALUE__" |
| 20 | + ]; |
| 21 | + |
| 22 | + public function let( |
| 23 | + ConfigResolverInterface $configResolver, |
| 24 | + Dispatcher $httpDispatcher, |
| 25 | + RequestFactory $messageFactory, |
| 26 | + RequestInterface $request |
| 27 | + |
| 28 | + ) { |
| 29 | + $messageFactory->createRequest( |
| 30 | + Argument::any(), |
| 31 | + Argument::any(), |
| 32 | + Argument::any(), |
| 33 | + Argument::any() |
| 34 | + )->willReturn($request); |
| 35 | + |
| 36 | + $this->beConstructedWith($configResolver, $httpDispatcher, [], $messageFactory); |
| 37 | + } |
| 38 | + |
| 39 | + public function it_should_purge_with_additional_token_header_when_configuration_key_with_token_is_not_null( |
| 40 | + ConfigResolverInterface $configResolver, |
| 41 | + RequestFactory $messageFactory |
| 42 | + ) { |
| 43 | + $configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true); |
| 44 | + $configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn('__TOKEN__'); |
| 45 | + |
| 46 | + $this->purge(self::URI, self::REQUEST_HEADERS); |
| 47 | + |
| 48 | + $this->requestShouldHaveBeenCreatedWithHeaders( |
| 49 | + array_merge(self::REQUEST_HEADERS, ["X-Invalidate-Token" => "__TOKEN__"]), |
| 50 | + $messageFactory |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_do_not_exist_in_configuration( |
| 55 | + ConfigResolverInterface $configResolver, |
| 56 | + RequestFactory $messageFactory |
| 57 | + ) { |
| 58 | + $configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(false); |
| 59 | + |
| 60 | + $this->purge(self::URI, self::REQUEST_HEADERS); |
| 61 | + |
| 62 | + $this->requestShouldHaveBeenCreatedWithHeaders( |
| 63 | + self::REQUEST_HEADERS, |
| 64 | + $messageFactory |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function it_should_purge_without_additional_token_header_when_configuration_key_with_token_exists_but_is_null( |
| 69 | + ConfigResolverInterface $configResolver, |
| 70 | + RequestFactory $messageFactory |
| 71 | + ) { |
| 72 | + $configResolver->hasParameter('http_cache.varnish_invalidate_token')->willReturn(true); |
| 73 | + $configResolver->getParameter('http_cache.varnish_invalidate_token')->willReturn(null); |
| 74 | + |
| 75 | + $this->purge(self::URI, self::REQUEST_HEADERS); |
| 76 | + |
| 77 | + $this->requestShouldHaveBeenCreatedWithHeaders( |
| 78 | + self::REQUEST_HEADERS, |
| 79 | + $messageFactory |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + private function requestShouldHaveBeenCreatedWithHeaders($headers, RequestFactory $messageFactory) |
| 84 | + { |
| 85 | + $messageFactory->createRequest( |
| 86 | + 'PURGE', |
| 87 | + self::URI, |
| 88 | + $headers, |
| 89 | + Argument::any() |
| 90 | + )->shouldHaveBeenCalled(); |
| 91 | + } |
| 92 | +} |
0 commit comments