[TASK] Update copyright year to 2013
[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 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the TransientMemory cache backend
29 *
30 * This file is a backport from FLOW3
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 */
34 class TransientMemoryBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * @expectedException \TYPO3\CMS\Core\Cache\Exception
38 * @test
39 */
40 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
41 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
42 $data = 'Some data';
43 $identifier = 'MyIdentifier';
44 $backend->set($identifier, $data);
45 }
46
47 /**
48 * @test
49 */
50 public function itIsPossibleToSetAndCheckExistenceInCache() {
51 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
52 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
53 $backend->setCache($cache);
54 $data = 'Some data';
55 $identifier = 'MyIdentifier';
56 $backend->set($identifier, $data);
57 $inCache = $backend->has($identifier);
58 $this->assertTrue($inCache);
59 }
60
61 /**
62 * @test
63 */
64 public function itIsPossibleToSetAndGetEntry() {
65 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
66 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
67 $backend->setCache($cache);
68 $data = 'Some data';
69 $identifier = 'MyIdentifier';
70 $backend->set($identifier, $data);
71 $fetchedData = $backend->get($identifier);
72 $this->assertEquals($data, $fetchedData);
73 }
74
75 /**
76 * @test
77 */
78 public function itIsPossibleToRemoveEntryFromCache() {
79 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
80 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
81 $backend->setCache($cache);
82 $data = 'Some data';
83 $identifier = 'MyIdentifier';
84 $backend->set($identifier, $data);
85 $backend->remove($identifier);
86 $inCache = $backend->has($identifier);
87 $this->assertFalse($inCache);
88 }
89
90 /**
91 * @test
92 */
93 public function itIsPossibleToOverwriteAnEntryInTheCache() {
94 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
95 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
96 $backend->setCache($cache);
97 $data = 'Some data';
98 $identifier = 'MyIdentifier';
99 $backend->set($identifier, $data);
100 $otherData = 'some other data';
101 $backend->set($identifier, $otherData);
102 $fetchedData = $backend->get($identifier);
103 $this->assertEquals($otherData, $fetchedData);
104 }
105
106 /**
107 * @test
108 */
109 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
110 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
111 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
112 $backend->setCache($cache);
113 $data = 'Some data';
114 $entryIdentifier = 'MyIdentifier';
115 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
116 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
117 $this->assertEquals($entryIdentifier, $retrieved[0]);
118 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
119 $this->assertEquals($entryIdentifier, $retrieved[0]);
120 }
121
122 /**
123 * @test
124 */
125 public function hasReturnsFalseIfTheEntryDoesntExist() {
126 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
127 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
128 $backend->setCache($cache);
129 $identifier = 'NonExistingIdentifier';
130 $inCache = $backend->has($identifier);
131 $this->assertFalse($inCache);
132 }
133
134 /**
135 * @test
136 */
137 public function removeReturnsFalseIfTheEntryDoesntExist() {
138 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
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 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
151 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
152 $backend->setCache($cache);
153 $data = 'some data' . microtime();
154 $backend->set('TransientMemoryBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
155 $backend->set('TransientMemoryBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
156 $backend->set('TransientMemoryBackendTest3', $data, array('UnitTestTag%test'));
157 $backend->flushByTag('UnitTestTag%special');
158 $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
159 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
160 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
161 }
162
163 /**
164 * @test
165 */
166 public function flushRemovesAllCacheEntries() {
167 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
168 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
169 $backend->setCache($cache);
170 $data = 'some data' . microtime();
171 $backend->set('TransientMemoryBackendTest1', $data);
172 $backend->set('TransientMemoryBackendTest2', $data);
173 $backend->set('TransientMemoryBackendTest3', $data);
174 $backend->flush();
175 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
176 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
177 $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
178 }
179
180 }
181
182 ?>