Completed
Push — http_cache_mvc ( 64ff00...c340cf )
by
unknown
29:34
created

ConfigurableResponseCacheConfiguratorSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 43.84 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 32
loc 73
rs 10
wmc 9
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_is_initializable() 0 4 1
A it_sets_cache_control_to_public_if_viewcache_is_enabled() 0 7 1
A it_does_not_set_cache_control_if_viewcache_is_disabled() 0 7 1
A it_does_not_set_shared_maxage_if_ttl_cache_is_disabled() 0 7 1
A it_does_not_set_shared_maxage_if_it_is_already_set_in_the_response() 9 9 1
A it_sets_shared_maxage() 9 9 1
A it_does_not_add_tags_if_viewcache_is_disabled() 7 7 1
A it_adds_tags_to_the_xkey_header() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace spec\eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator;
4
5
use eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator\ConfigurableResponseCacheConfigurator;
6
use PhpSpec\ObjectBehavior;
7
use Prophecy\Argument;
8
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
11
12
class ConfigurableResponseCacheConfiguratorSpec extends ObjectBehavior
13
{
14
    function let(Response $response, ResponseHeaderBag $headers)
15
    {
16
        $response->headers = $headers;
17
        $this->beConstructedWith(true, true, 30);
18
    }
19
20
    function it_is_initializable()
21
    {
22
        $this->shouldHaveType(ConfigurableResponseCacheConfigurator::class);
23
    }
24
25
    function it_sets_cache_control_to_public_if_viewcache_is_enabled(Response $response)
26
    {
27
        $this->beConstructedWith(true, false, 0);
28
        $this->enableCache($response);
29
30
        $response->setPublic()->shouldHaveBeenCalled();
31
    }
32
33
    function it_does_not_set_cache_control_if_viewcache_is_disabled(Response $response)
34
    {
35
        $this->beConstructedWith(false, false, 0);
36
        $this->enableCache($response);
37
38
        $response->setPublic()->shouldNotHaveBeenCalled();
39
    }
40
41
    function it_does_not_set_shared_maxage_if_ttl_cache_is_disabled(Response $response)
42
    {
43
        $this->beConstructedWith(true, false, 30);
44
        $this->setSharedMaxAge($response);
45
46
        $response->setSharedMaxAge(30)->shouldNotHaveBeenCalled();
47
    }
48
49 View Code Duplication
    function it_does_not_set_shared_maxage_if_it_is_already_set_in_the_response(Response $response, ResponseHeaderBag $headers)
50
    {
51
        $this->beConstructedWith(true, true, 30);
52
        $headers->hasCacheControlDirective('s-maxage')->willReturn(true);
53
54
        $this->setSharedMaxAge($response);
55
56
        $response->setSharedMaxAge($response, 30)->shouldNotHaveBeenCalled();
57
    }
58
59 View Code Duplication
    function it_sets_shared_maxage(Response $response, ResponseHeaderBag $headers)
60
    {
61
        $this->beConstructedWith(true, true, 30);
62
        $headers->hasCacheControlDirective('s-maxage')->willReturn(false);
63
64
        $this->setSharedMaxAge($response);
65
66
        $response->setSharedMaxAge(30)->shouldHaveBeenCalled();
67
    }
68
69 View Code Duplication
    function it_does_not_add_tags_if_viewcache_is_disabled(Response $response, ResponseHeaderBag $headers)
70
    {
71
        $this->beConstructedWith(false, false, 0);
72
        $this->addTags($response, ['foo-1', 'bar-2']);
73
74
        $headers->set('xkey', ['foo-1', 'bar-2'])->shouldNotHaveBeenCalled();
75
    }
76
77 View Code Duplication
    function it_adds_tags_to_the_xkey_header(Response $response, ResponseHeaderBag $headers)
78
    {
79
        $this->beConstructedWith(false, false, 0);
80
        $this->addTags($response, ['foo-1', 'bar-2']);
81
82
        $headers->set('xkey', ['foo-1', 'bar-2'])->shouldNotHaveBeenCalled();
83
    }
84
}
85