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