[TASK] Use name-resolution instead of strings where possible: 2
[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 * @author Ingo Renner <ingo@typo3.org>
23 */
24 class TransientMemoryBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @expectedException \TYPO3\CMS\Core\Cache\Exception
28 * @test
29 */
30 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
31 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
32 $data = 'Some data';
33 $identifier = 'MyIdentifier';
34 $backend->set($identifier, $data);
35 }
36
37 /**
38 * @test
39 */
40 public function itIsPossibleToSetAndCheckExistenceInCache() {
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 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
56 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
57 $backend->setCache($cache);
58 $data = 'Some data';
59 $identifier = 'MyIdentifier';
60 $backend->set($identifier, $data);
61 $fetchedData = $backend->get($identifier);
62 $this->assertEquals($data, $fetchedData);
63 }
64
65 /**
66 * @test
67 */
68 public function itIsPossibleToRemoveEntryFromCache() {
69 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
70 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
71 $backend->setCache($cache);
72 $data = 'Some data';
73 $identifier = 'MyIdentifier';
74 $backend->set($identifier, $data);
75 $backend->remove($identifier);
76 $inCache = $backend->has($identifier);
77 $this->assertFalse($inCache);
78 }
79
80 /**
81 * @test
82 */
83 public function itIsPossibleToOverwriteAnEntryInTheCache() {
84 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
85 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
86 $backend->setCache($cache);
87 $data = 'Some data';
88 $identifier = 'MyIdentifier';
89 $backend->set($identifier, $data);
90 $otherData = 'some other data';
91 $backend->set($identifier, $otherData);
92 $fetchedData = $backend->get($identifier);
93 $this->assertEquals($otherData, $fetchedData);
94 }
95
96 /**
97 * @test
98 */
99 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
100 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
101 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
102 $backend->setCache($cache);
103 $data = 'Some data';
104 $entryIdentifier = 'MyIdentifier';
105 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
106 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
107 $this->assertEquals($entryIdentifier, $retrieved[0]);
108 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
109 $this->assertEquals($entryIdentifier, $retrieved[0]);
110 }
111
112 /**
113 * @test
114 */
115 public function hasReturnsFalseIfTheEntryDoesntExist() {
116 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
117 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
118 $backend->setCache($cache);
119 $identifier = 'NonExistingIdentifier';
120 $inCache = $backend->has($identifier);
121 $this->assertFalse($inCache);
122 }
123
124 /**
125 * @test
126 */
127 public function removeReturnsFalseIfTheEntryDoesntExist() {
128 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
129 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
130 $backend->setCache($cache);
131 $identifier = 'NonExistingIdentifier';
132 $inCache = $backend->remove($identifier);
133 $this->assertFalse($inCache);
134 }
135
136 /**
137 * @test
138 */
139 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
140 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
141 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
142 $backend->setCache($cache);
143 $data = 'some data' . microtime();
144 $backend->set('TransientMemoryBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
145 $backend->set('TransientMemoryBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
146 $backend->set('TransientMemoryBackendTest3', $data, array('UnitTestTag%test'));
147 $backend->flushByTag('UnitTestTag%special');
148 $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
149 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
150 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
151 }
152
153 /**
154 * @test
155 */
156 public function flushRemovesAllCacheEntries() {
157 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', FALSE);
158 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
159 $backend->setCache($cache);
160 $data = 'some data' . microtime();
161 $backend->set('TransientMemoryBackendTest1', $data);
162 $backend->set('TransientMemoryBackendTest2', $data);
163 $backend->set('TransientMemoryBackendTest3', $data);
164 $backend->flush();
165 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
166 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
167 $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
168 }
169
170 }