[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / Typo3DatabaseBackendTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
20 use TYPO3\CMS\Core\Cache\Exception;
21 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
22 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23 use TYPO3\CMS\Core\Database\Connection;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 /**
29 * Test case
30 */
31 class Typo3DatabaseBackendTest extends UnitTestCase
32 {
33 /**
34 * @test
35 */
36 public function setCacheCalculatesCacheTableName()
37 {
38 $frontendProphecy = $this->prophesize(FrontendInterface::class);
39 $frontendProphecy->getIdentifier()->willReturn('cache_test');
40
41 $subject = new Typo3DatabaseBackend('Testing');
42 $subject->setCache($frontendProphecy->reveal());
43
44 $this->assertEquals('cf_cache_test', $subject->getCacheTable());
45 }
46
47 /**
48 * @test
49 */
50 public function setCacheCalculatesTagsTableName()
51 {
52 $frontendProphecy = $this->prophesize(FrontendInterface::class);
53 $frontendProphecy->getIdentifier()->willReturn('cache_test');
54
55 $subject = new Typo3DatabaseBackend('Testing');
56 $subject->setCache($frontendProphecy->reveal());
57
58 $this->assertEquals('cf_cache_test_tags', $subject->getTagsTable());
59 }
60
61 /**
62 * @test
63 */
64 public function setThrowsExceptionIfFrontendWasNotSet()
65 {
66 $subject = new Typo3DatabaseBackend('Testing');
67 $this->expectException(Exception::class);
68 $this->expectExceptionCode(1236518288);
69 $subject->set('identifier', 'data');
70 }
71
72 /**
73 * @test
74 */
75 public function setThrowsExceptionIfDataIsNotAString()
76 {
77 $frontendProphecy = $this->prophesize(FrontendInterface::class);
78 $frontendProphecy->getIdentifier()->willReturn('cache_test');
79
80 $subject = new Typo3DatabaseBackend('Testing');
81 $subject->setCache($frontendProphecy->reveal());
82
83 $this->expectException(InvalidDataException::class);
84 $this->expectExceptionCode(1236518298);
85
86 $subject->set('identifier', ['iAmAnArray']);
87 }
88
89 /**
90 * @test
91 */
92 public function getThrowsExceptionIfFrontendWasNotSet()
93 {
94 $subject = new Typo3DatabaseBackend('Testing');
95 $this->expectException(Exception::class);
96 $this->expectExceptionCode(1236518288);
97 $subject->get('identifier');
98 }
99
100 /**
101 * @test
102 */
103 public function hasThrowsExceptionIfFrontendWasNotSet()
104 {
105 $subject = new Typo3DatabaseBackend('Testing');
106 $this->expectException(Exception::class);
107 $this->expectExceptionCode(1236518288);
108 $subject->has('identifier');
109 }
110
111 /**
112 * @test
113 */
114 public function removeThrowsExceptionIfFrontendWasNotSet()
115 {
116 $subject = new Typo3DatabaseBackend('Testing');
117 $this->expectException(Exception::class);
118 $this->expectExceptionCode(1236518288);
119 $subject->remove('identifier');
120 }
121
122 /**
123 * @test
124 */
125 public function collectGarbageThrowsExceptionIfFrontendWasNotSet()
126 {
127 $subject = new Typo3DatabaseBackend('Testing');
128 $this->expectException(Exception::class);
129 $this->expectExceptionCode(1236518288);
130 $subject->collectGarbage();
131 }
132
133 /**
134 * @test
135 */
136 public function findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet()
137 {
138 $subject = new Typo3DatabaseBackend('Testing');
139 $this->expectException(Exception::class);
140 $this->expectExceptionCode(1236518288);
141 $subject->findIdentifiersByTag('identifier');
142 }
143
144 /**
145 * @test
146 */
147 public function flushThrowsExceptionIfFrontendWasNotSet()
148 {
149 $subject = new Typo3DatabaseBackend('Testing');
150 $this->expectException(Exception::class);
151 $this->expectExceptionCode(1236518288);
152 $subject->flush();
153 }
154
155 /**
156 * @test
157 */
158 public function flushRemovesAllCacheEntries()
159 {
160 $frontendProphecy = $this->prophesize(FrontendInterface::class);
161 $frontendProphecy->getIdentifier()->willReturn('cache_test');
162
163 $subject = new Typo3DatabaseBackend('Testing');
164 $subject->setCache($frontendProphecy->reveal());
165
166 $connectionProphet = $this->prophesize(Connection::class);
167 $connectionProphet->truncate('cf_cache_test')->shouldBeCalled()->willReturn(0);
168 $connectionProphet->truncate('cf_cache_test_tags')->shouldBeCalled()->willReturn(0);
169
170 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
171 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
172
173 // Two instances are required as there are different tables being cleared
174 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
175 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
176
177 $subject->flush();
178 }
179
180 public function flushByTagCallsDeleteOnConnection()
181 {
182 $frontendProphecy = $this->prophesize(FrontendInterface::class);
183 $frontendProphecy->getIdentifier()->willReturn('cache_test');
184
185 $subject = new Typo3DatabaseBackend('Testing');
186 $subject->setCache($frontendProphecy->reveal());
187
188 $connectionProphet = $this->prophesize(Connection::class);
189 $connectionProphet->delete('cf_cache_test')->shouldBeCalled()->willReturn(0);
190 $connectionProphet->delete('cf_cache_test_tags')->shouldBeCalled()->willReturn(0);
191
192 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
193 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
194
195 // Two instances are required as there are different tables being cleared
196 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
197 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
198
199 $subject->flushByTag('Tag');
200 }
201
202 public function flushByTagsCallsDeleteOnConnection()
203 {
204 $frontendProphecy = $this->prophesize(FrontendInterface::class);
205 $frontendProphecy->getIdentifier()->willReturn('cache_test');
206
207 $subject = new Typo3DatabaseBackend('Testing');
208 $subject->setCache($frontendProphecy->reveal());
209
210 $connectionProphet = $this->prophesize(Connection::class);
211 $connectionProphet->delete('cf_cache_test')->shouldBeCalled()->willReturn(0);
212 $connectionProphet->delete('cf_cache_test_tags')->shouldBeCalled()->willReturn(0);
213
214 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
215 $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
216
217 // Two instances are required as there are different tables being cleared
218 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
219 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
220
221 $subject->flushByTag(['Tag1', 'Tag2']);
222 }
223
224 /**
225 * @test
226 */
227 public function flushByTagThrowsExceptionIfFrontendWasNotSet()
228 {
229 $subject = new Typo3DatabaseBackend('Testing');
230 $this->expectException(Exception::class);
231 $this->expectExceptionCode(1236518288);
232 $subject->flushByTag('Tag');
233 }
234 /**
235 * @test
236 */
237 public function flushByTagsThrowsExceptionIfFrontendWasNotSet()
238 {
239 $subject = new Typo3DatabaseBackend('Testing');
240 $this->expectException(Exception::class);
241 $this->expectExceptionCode(1236518288);
242 $subject->flushByTags([]);
243 }
244 }