TYPO3 CMS  TYPO3_8-7
TransientMemoryBackendTest.php
Go to the documentation of this file.
1 <?php
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 
22 class TransientMemoryBackendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
28  {
29  $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
30  $this->expectExceptionCode(1238244992);
31 
32  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
33  $data = 'Some data';
34  $identifier = 'MyIdentifier';
35  $backend->set($identifier, $data);
36  }
37 
42  {
43  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
44  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
45  $backend->setCache($cache);
46  $data = 'Some data';
47  $identifier = 'MyIdentifier';
48  $backend->set($identifier, $data);
49  $inCache = $backend->has($identifier);
50  $this->assertTrue($inCache);
51  }
52 
56  public function itIsPossibleToSetAndGetEntry()
57  {
58  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
59  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
60  $backend->setCache($cache);
61  $data = 'Some data';
62  $identifier = 'MyIdentifier';
63  $backend->set($identifier, $data);
64  $fetchedData = $backend->get($identifier);
65  $this->assertEquals($data, $fetchedData);
66  }
67 
72  {
73  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
74  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
75  $backend->setCache($cache);
76  $data = 'Some data';
77  $identifier = 'MyIdentifier';
78  $backend->set($identifier, $data);
79  $backend->remove($identifier);
80  $inCache = $backend->has($identifier);
81  $this->assertFalse($inCache);
82  }
83 
88  {
89  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
90  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
91  $backend->setCache($cache);
92  $data = 'Some data';
93  $identifier = 'MyIdentifier';
94  $backend->set($identifier, $data);
95  $otherData = 'some other data';
96  $backend->set($identifier, $otherData);
97  $fetchedData = $backend->get($identifier);
98  $this->assertEquals($otherData, $fetchedData);
99  }
100 
105  {
106  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
107  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
108  $backend->setCache($cache);
109  $data = 'Some data';
110  $entryIdentifier = 'MyIdentifier';
111  $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
112  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
113  $this->assertEquals($entryIdentifier, $retrieved[0]);
114  $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
115  $this->assertEquals($entryIdentifier, $retrieved[0]);
116  }
117 
122  {
123  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
124  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
125  $backend->setCache($cache);
126  $identifier = 'NonExistingIdentifier';
127  $inCache = $backend->has($identifier);
128  $this->assertFalse($inCache);
129  }
130 
135  {
136  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
137  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
138  $backend->setCache($cache);
139  $identifier = 'NonExistingIdentifier';
140  $inCache = $backend->remove($identifier);
141  $this->assertFalse($inCache);
142  }
143 
148  {
149  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
150  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
151  $backend->setCache($cache);
152  $data = 'some data' . microtime();
153  $backend->set('TransientMemoryBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
154  $backend->set('TransientMemoryBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
155  $backend->set('TransientMemoryBackendTest3', $data, ['UnitTestTag%test']);
156  $backend->flushByTag('UnitTestTag%special');
157  $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
158  $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
159  $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
160  }
161 
166  {
167  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
168  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
169  $backend->setCache($cache);
170  $data = 'some data' . microtime();
171  $backend->set('TransientMemoryBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
172  $backend->set('TransientMemoryBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
173  $backend->set('TransientMemoryBackendTest3', $data, ['UnitTestTag%test']);
174  $backend->flushByTags(['UnitTestTag%special', 'UnitTestTag%boring']);
175  $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
176  $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
177  $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
178  }
179 
183  public function flushRemovesAllCacheEntries()
184  {
185  $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
186  $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
187  $backend->setCache($cache);
188  $data = 'some data' . microtime();
189  $backend->set('TransientMemoryBackendTest1', $data);
190  $backend->set('TransientMemoryBackendTest2', $data);
191  $backend->set('TransientMemoryBackendTest3', $data);
192  $backend->flush();
193  $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
194  $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
195  $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
196  }
197 }