TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
VariableFrontendTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Frontend;
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 {
28  {
29  $this->expectException(\InvalidArgumentException::class);
30  $this->expectExceptionCode(1233057566);
31 
32  $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
33  ->setMethods(['isValidEntryIdentifier'])
34  ->disableOriginalConstructor()
35  ->getMock();
36  $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(false));
37  $cache->set('foo', 'bar');
38  }
39 
44  {
45  $theString = 'Just some value';
46  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
47  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
48  ->disableOriginalConstructor()
49  ->getMock();
50  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
51 
52  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
53  $cache->set('VariableCacheTest', $theString);
54  }
55 
60  {
61  $theArray = ['Just some value', 'and another one.'];
62  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
63  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
64  ->disableOriginalConstructor()
65  ->getMock();
66  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
67 
68  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
69  $cache->set('VariableCacheTest', $theArray);
70  }
71 
75  public function setPassesLifetimeToBackend()
76  {
77  $theString = 'Just some value';
78  $theLifetime = 1234;
79  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
80  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
81  ->disableOriginalConstructor()
82  ->getMock();
83  $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo([]), $this->equalTo($theLifetime));
84 
85  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
86  $cache->set('VariableCacheTest', $theString, [], $theLifetime);
87  }
88 
93  {
94  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
95  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
96  ->disableOriginalConstructor()
97  ->getMock();
98  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
99 
100  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
101  $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
102  }
103 
108  {
109  $theArray = ['Just some value', 'and another one.'];
110  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
111  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
112  ->disableOriginalConstructor()
113  ->getMock();
114  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
115 
116  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
117  $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
118  }
119 
124  {
125  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
126  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
127  ->disableOriginalConstructor()
128  ->getMock();
129  $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(false)));
130 
131  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
132  $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
133  }
134 
138  public function hasReturnsResultFromBackend()
139  {
140  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
141  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
142  ->disableOriginalConstructor()
143  ->getMock();
144  $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(true));
145 
146  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
147  $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
148  }
149 
153  public function removeCallsBackend()
154  {
155  $cacheIdentifier = 'someCacheIdentifier';
156  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
157  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
158  ->disableOriginalConstructor()
159  ->getMock();
160 
161  $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
162 
163  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
164  $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
165  }
166 
170  public function getByTagRejectsInvalidTags()
171  {
172  $this->expectException(\InvalidArgumentException::class);
173  $this->expectExceptionCode(1233058312);
174 
175  $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class);
176  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
177  $cache->getByTag('SomeInvalid\Tag');
178  }
179 
183  public function getByTagCallsBackend()
184  {
185  $tag = 'sometag';
186  $identifiers = ['one', 'two'];
187  $entries = ['one value', 'two value'];
188  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
189  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
190  ->disableOriginalConstructor()
191  ->getMock();
192 
193  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
194  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
195 
196  $cache = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
197  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
198  }
199 }