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