[BUGFIX] Fallback to empty array if ExportController receives no input
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / TransientMemoryBackendTest.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 TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Testcase for the TransientMemory cache backend
21 */
22 class TransientMemoryBackendTest extends UnitTestCase
23 {
24 protected $resetSingletonInstances = true;
25
26 /**
27 * @test
28 */
29 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
30 {
31 $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
32 $this->expectExceptionCode(1238244992);
33
34 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
35 $data = 'Some data';
36 $identifier = 'MyIdentifier';
37 $backend->set($identifier, $data);
38 }
39
40 /**
41 * @test
42 */
43 public function itIsPossibleToSetAndCheckExistenceInCache()
44 {
45 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
46 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
47 $backend->setCache($cache);
48 $data = 'Some data';
49 $identifier = 'MyIdentifier';
50 $backend->set($identifier, $data);
51 $inCache = $backend->has($identifier);
52 $this->assertTrue($inCache);
53 }
54
55 /**
56 * @test
57 */
58 public function itIsPossibleToSetAndGetEntry()
59 {
60 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
61 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
62 $backend->setCache($cache);
63 $data = 'Some data';
64 $identifier = 'MyIdentifier';
65 $backend->set($identifier, $data);
66 $fetchedData = $backend->get($identifier);
67 $this->assertEquals($data, $fetchedData);
68 }
69
70 /**
71 * @test
72 */
73 public function itIsPossibleToRemoveEntryFromCache()
74 {
75 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
76 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
77 $backend->setCache($cache);
78 $data = 'Some data';
79 $identifier = 'MyIdentifier';
80 $backend->set($identifier, $data);
81 $backend->remove($identifier);
82 $inCache = $backend->has($identifier);
83 $this->assertFalse($inCache);
84 }
85
86 /**
87 * @test
88 */
89 public function itIsPossibleToOverwriteAnEntryInTheCache()
90 {
91 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
92 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
93 $backend->setCache($cache);
94 $data = 'Some data';
95 $identifier = 'MyIdentifier';
96 $backend->set($identifier, $data);
97 $otherData = 'some other data';
98 $backend->set($identifier, $otherData);
99 $fetchedData = $backend->get($identifier);
100 $this->assertEquals($otherData, $fetchedData);
101 }
102
103 /**
104 * @test
105 */
106 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
107 {
108 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
109 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
110 $backend->setCache($cache);
111 $data = 'Some data';
112 $entryIdentifier = 'MyIdentifier';
113 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
114 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
115 $this->assertEquals($entryIdentifier, $retrieved[0]);
116 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
117 $this->assertEquals($entryIdentifier, $retrieved[0]);
118 }
119
120 /**
121 * @test
122 */
123 public function hasReturnsFalseIfTheEntryDoesntExist()
124 {
125 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
126 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
127 $backend->setCache($cache);
128 $identifier = 'NonExistingIdentifier';
129 $inCache = $backend->has($identifier);
130 $this->assertFalse($inCache);
131 }
132
133 /**
134 * @test
135 */
136 public function removeReturnsFalseIfTheEntryDoesntExist()
137 {
138 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
139 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
140 $backend->setCache($cache);
141 $identifier = 'NonExistingIdentifier';
142 $inCache = $backend->remove($identifier);
143 $this->assertFalse($inCache);
144 }
145
146 /**
147 * @test
148 */
149 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
150 {
151 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
152 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
153 $backend->setCache($cache);
154 $data = 'some data' . microtime();
155 $backend->set('TransientMemoryBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
156 $backend->set('TransientMemoryBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
157 $backend->set('TransientMemoryBackendTest3', $data, ['UnitTestTag%test']);
158 $backend->flushByTag('UnitTestTag%special');
159 $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
160 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
161 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
162 }
163
164 /**
165 * @test
166 */
167 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags()
168 {
169 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
170 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
171 $backend->setCache($cache);
172 $data = 'some data' . microtime();
173 $backend->set('TransientMemoryBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
174 $backend->set('TransientMemoryBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
175 $backend->set('TransientMemoryBackendTest3', $data, ['UnitTestTag%test']);
176 $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
177 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
178 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
179 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
180 }
181
182 /**
183 * @test
184 */
185 public function flushRemovesAllCacheEntries()
186 {
187 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
188 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
189 $backend->setCache($cache);
190 $data = 'some data' . microtime();
191 $backend->set('TransientMemoryBackendTest1', $data);
192 $backend->set('TransientMemoryBackendTest2', $data);
193 $backend->set('TransientMemoryBackendTest3', $data);
194 $backend->flush();
195 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
196 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
197 $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
198 }
199 }