TYPO3 CMS  TYPO3_7-6
VariableFrontendTest.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 
23 {
29  {
30  $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, ['isValidEntryIdentifier'], [], '', false);
31  $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(false));
32  $cache->set('foo', 'bar');
33  }
34 
39  {
40  $theString = 'Just some value';
41  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
42  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
43 
44  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
45  $cache->set('VariableCacheTest', $theString);
46  }
47 
52  {
53  $theArray = ['Just some value', 'and another one.'];
54  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
55  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
56 
57  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
58  $cache->set('VariableCacheTest', $theArray);
59  }
60 
64  public function setPassesLifetimeToBackend()
65  {
66  $theString = 'Just some value';
67  $theLifetime = 1234;
68  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
69  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo([]), $this->equalTo($theLifetime));
70 
71  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
72  $cache->set('VariableCacheTest', $theString, [], $theLifetime);
73  }
74 
79  {
80  if (!extension_loaded('igbinary')) {
81  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
82  }
83 
84  $theString = 'Just some value';
85  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
86  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(igbinary_serialize($theString)));
87 
88  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
89  $cache->initializeObject();
90  $cache->set('VariableCacheTest', $theString);
91  }
92 
97  {
98  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
99  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
100 
101  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
102  $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
103  }
104 
109  {
110  $theArray = ['Just some value', 'and another one.'];
111  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
112  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
113 
114  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
115  $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
116  }
117 
122  {
123  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
124  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(false)));
125 
126  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
127  $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
128  }
129 
133  public function getUsesIgBinaryIfAvailable()
134  {
135  if (!extension_loaded('igbinary')) {
136  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
137  }
138 
139  $theArray = ['Just some value', 'and another one.'];
140  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
141  $backend->expects($this->once())->method('get')->will($this->returnValue(igbinary_serialize($theArray)));
142 
143  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
144  $cache->initializeObject();
145 
146  $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
147  }
148 
152  public function hasReturnsResultFromBackend()
153  {
154  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
155  $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(true));
156 
157  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
158  $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
159  }
160 
164  public function removeCallsBackend()
165  {
166  $cacheIdentifier = 'someCacheIdentifier';
167  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
168 
169  $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
170 
171  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
172  $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
173  }
174 
179  public function getByTagRejectsInvalidTags()
180  {
181  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\BackendInterface::class, [], [], '', false);
182  $backend->expects($this->never())->method('getByTag');
183 
184  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
185  $cache->getByTag('SomeInvalid\Tag');
186  }
187 
191  public function getByTagCallsBackend()
192  {
193  $tag = 'sometag';
194  $identifiers = ['one', 'two'];
195  $entries = ['one value', 'two value'];
196  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
197 
198  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
199  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
200 
201  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
202  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
203  }
204 
209  {
210  if (!extension_loaded('igbinary')) {
211  $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
212  }
213 
214  $tag = 'sometag';
215  $identifiers = ['one', 'two'];
216  $entries = ['one value', 'two value'];
217  $backend = $this->getMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'], [], '', false);
218 
219  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
220  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(igbinary_serialize('one value'), igbinary_serialize('two value')));
221 
222  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
223  $cache->initializeObject();
224  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
225  }
226 }