SetPageCacheHookTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 29
rs 9.584
1
<?php
2
namespace Qbus\SkipPageIsBeingGenerated\Tests\Unit;
3
4
use Prophecy\Prophecy\ObjectProphecy;
5
use Qbus\SkipPageIsBeingGenerated\Hooks\SetPageCacheHook;
6
use TYPO3\CMS\Core\Cache\Backend\RedisBackend;
7
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
8
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
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
21
     */
22
    protected $cacheFrontendProphecy;
23
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $cacheBackendProphecy;
28
29
    /**
30
     * @var array
31
     */
32
    protected $params = [];
33
34
    /**
35
     * @var SetPageCacheHook
36
     */
37
    protected $hook;
38
39
    /**
40
     * @var string
41
     */
42
    protected $entryIdentifier;
43
44
    /**
45
     * @var array|bool
46
     */
47
    protected $variable;
48
49
    /**
50
     * @var int
51
     */
52
    protected $lifetime;
53
54
    public function setUp(): void
55
    {
56
        $cacheBackendProphecy = $this->prophesize();
57
        $cacheBackendProphecy->willImplement(BackendInterface::class);
58
59
        $cacheFrontendProphecy = $this->prophesize();
60
        $cacheFrontendProphecy->willImplement(FrontendInterface::class);
61
        $cacheFrontendProphecy->getIdentifier()->willReturn('cache_pages');
62
        $cacheFrontendProphecy->getBackend()->will(function () use ($cacheBackendProphecy) {
63
            return $cacheBackendProphecy->reveal();
64
        });
65
66
        $this->cacheBackendProphecy = $cacheBackendProphecy;
67
        $this->cacheFrontendProphecy = $cacheFrontendProphecy;
68
        $this->hook = new SetpageCacheHook;
69
70
        $this->entryIdentifier = md5('foo');
71
        $this->lifetime = 30;
72
        $this->variable = [
73
            'foo' => 'bar',
74
            'temp_content' => true,
75
        ];
76
        $tags = [];
77
78
        $this->params = [
79
            'entryIdentifier' => &$this->entryIdentifier,
80
            'variable' => &$this->variable,
81
            'lifetime' => &$this->lifetime,
82
            'tags' => &$tags,
83
        ];
84
    }
85
86
    public function testSetInvalidatesLifetime(): void
87
    {
88
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
89
90
        static::assertEquals(-1, $this->lifetime);
91
    }
92
93
    public function testSetInvalidatesLifetimeForRedis(): void
94
    {
95
        $this->cacheBackendProphecy->willExtend(RedisBackend::class);
96
97
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
98
99
        static::assertEquals(1, $this->lifetime);
100
        static::assertFalse($this->variable);
101
    }
102
103
    public function testSetDoesNothingForNonTemporaryPageCache(): void
104
    {
105
        unset($this->variable['temp_content']);
106
107
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
108
109
        static::assertEquals(30, $this->lifetime);
110
    }
111
112
    public function testSetDoesNothingForNonPageCache(): void
113
    {
114
        $this->cacheFrontendProphecy->getIdentifier()->willReturn('cache_pagesection');
115
116
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
117
118
        static::assertEquals(30, $this->lifetime);
119
    }
120
121
    public function testSetDoesNothingForRedirectsPageCache(): void
122
    {
123
        $this->entryIdentifier = 'redirects';
124
125
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
126
127
        static::assertEquals(30, $this->lifetime);
128
    }
129
130
    public function testSetDoesNothingForTitleTagPageCache(): void
131
    {
132
        $this->entryIdentifier .= '-titleTag-record';
133
134
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
135
136
        static::assertEquals(30, $this->lifetime);
137
    }
138
139
    public function testSetDoesNothingForMetatagPageCache(): void
140
    {
141
        $this->entryIdentifier .= '-metatag-html5';
142
143
        $this->hook->set($this->params, $this->cacheFrontendProphecy->reveal());
144
145
        static::assertEquals(30, $this->lifetime);
146
    }
147
}
148