Passed
Push — master ( a219dd...5b184c )
by Benjamin
01:55
created

testSetInvalidatesLifetimeForRedis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qbus\SkipPageIsBeingGenerated\Tests\Unit;
3
4
use Qbus\SkipPageIsBeingGenerated\Hooks\SetPageCacheHook;
5
use TYPO3\CMS\Core\Cache\Backend\RedisBackend;
6
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
7
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
8
//use TYPO3\CMS\Core\Utility\GeneralUtility;
9
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
10
11
/**
12
 * SetPageCacheHookTest
13
 *
14
 * @author Benjamin Franzke <[email protected]>
15
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
16
 */
17
class SetPageCacheHookTest extends UnitTestCase
18
{
19
    /**
20
     * @var ObjectProphecy
0 ignored issues
show
Bug introduced by
The type Qbus\SkipPageIsBeingGene...sts\Unit\ObjectProphecy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     */
22
    protected $cacheFrontendProphecy;
23
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $cacheBackendProphecy;
28
29
    /**
30
     * @var params
31
     */
32
33
    /**
34
     * @var SetPageCacheHook
35
     */
36
    protected $hook;
37
38
    /**
39
     * @var string
40
     */
41
    protected $entryIdentifier;
42
43
    /**
44
     * @var array|bool
45
     */
46
    protected $variable;
47
48
    /**
49
     * @var int
50
     */
51
    protected $lifetime;
52
53
    public function setUp()
54
    {
55
        $cacheBackendProphecy = $this->prophesize();
56
        $cacheBackendProphecy->willImplement(BackendInterface::class);
57
58
        $cacheFrontendProphecy = $this->prophesize();
59
        $cacheFrontendProphecy->willImplement(FrontendInterface::class);
60
        $cacheFrontendProphecy->getIdentifier()->willReturn('cache_pages');
61
        $cacheFrontendProphecy->getBackend()->will(function () use ($cacheBackendProphecy) {
62
            return $cacheBackendProphecy->reveal();
63
        });
64
65
        $this->cacheBackendProphecy = $cacheBackendProphecy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheBackendProphecy of type Prophecy\Prophecy\ObjectProphecy is incompatible with the declared type Qbus\SkipPageIsBeingGene...sts\Unit\ObjectProphecy of property $cacheBackendProphecy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
        $this->cacheFrontendProphecy = $cacheFrontendProphecy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheFrontendProphecy of type Prophecy\Prophecy\ObjectProphecy is incompatible with the declared type Qbus\SkipPageIsBeingGene...sts\Unit\ObjectProphecy of property $cacheFrontendProphecy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
        $this->hook = new SetpageCacheHook;
68
69
        $this->entryIdentifier = md5('foo');
70
        $this->lifetime = 30;
71
        $this->variable = [
72
            'foo' => 'bar',
73
            'temp_content' => true,
74
        ];
75
        $tags = [];
76
77
        $this->params = [
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
            'entryIdentifier' => &$this->entryIdentifier,
79
            'variable' => &$this->variable,
80
            'lifetime' => &$this->lifetime,
81
            'tags' => &$tags,
82
        ];
83
    }
84
85
    public function testSetInvalidatesLifetime()
86
    {
87
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
88
89
        $this->assertEquals(-1, $this->lifetime);
90
    }
91
92
    public function testSetInvalidatesLifetimeForRedis()
93
    {
94
        $this->cacheBackendProphecy->willExtend(RedisBackend::class);
95
96
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
97
98
        $this->assertEquals(1, $this->lifetime);
99
        $this->assertFalse($this->variable);
100
    }
101
102
    public function testSetDoesNothingForNonTemporaryPageCache()
103
    {
104
        unset($this->variable['temp_content']);
105
106
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
107
108
        $this->assertEquals(30, $this->lifetime);
109
    }
110
111
    public function testSetDoesNothingForNonPageCache()
112
    {
113
        $this->cacheFrontendProphecy->getIdentifier()->willReturn('cache_pagesection');
114
115
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
116
117
        $this->assertEquals(30, $this->lifetime);
118
    }
119
120
    public function testSetDoesNothingForRedirectsPageCache()
121
    {
122
        $this->entryIdentifier = 'redirects';
123
124
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
125
126
        $this->assertEquals(30, $this->lifetime);
127
    }
128
129
    public function testSetDoesNothingForTitleTagPageCache()
130
    {
131
        $this->entryIdentifier .= '-titleTag-record';
132
133
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
134
135
        $this->assertEquals(30, $this->lifetime);
136
    }
137
138
    public function testSetDoesNothingForMetatagPageCache()
139
    {
140
        $this->entryIdentifier .= '-metatag-html5';
141
142
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
143
144
        $this->assertEquals(30, $this->lifetime);
145
    }
146
}
147