TYPO3 CMS  TYPO3_8-7
StringFrontendTest.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  */
17 
23 class StringFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
29  {
30  $this->expectException(\InvalidArgumentException::class);
31  $this->expectExceptionCode(1233057566);
32 
33  $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class)
34  ->setMethods(['isValidEntryIdentifier'])
35  ->disableOriginalConstructor()
36  ->getMock();
37  $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(false));
38  $cache->set('foo', 'bar');
39  }
40 
44  public function setPassesStringToBackend()
45  {
46  $theString = 'Just some value';
47  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
48  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
49  ->disableOriginalConstructor()
50  ->getMock();
51  $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString));
52  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
53  $cache->set('StringCacheTest', $theString);
54  }
55 
59  public function setPassesLifetimeToBackend()
60  {
61  $theString = 'Just some value';
62  $theLifetime = 1234;
63  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
64  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
65  ->disableOriginalConstructor()
66  ->getMock();
67  $backend->expects($this->once())->method('set')->with($this->equalTo('StringCacheTest'), $this->equalTo($theString), $this->equalTo([]), $this->equalTo($theLifetime));
68  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
69  $cache->set('StringCacheTest', $theString, [], $theLifetime);
70  }
71 
76  {
77  $this->expectException(InvalidDataException::class);
78  $this->expectExceptionCode(1222808333);
79 
80  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
81  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
82  ->disableOriginalConstructor()
83  ->getMock();
84  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
85  $cache->set('StringCacheTest', []);
86  }
87 
92  {
93  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
94  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
95  ->disableOriginalConstructor()
96  ->getMock();
97  $backend->expects($this->once())->method('get')->will($this->returnValue('Just some value'));
98  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
99  $this->assertEquals('Just some value', $cache->get('StringCacheTest'), 'The returned value was not the expected string.');
100  }
101 
105  public function hasReturnsResultFromBackend()
106  {
107  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
108  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
109  ->disableOriginalConstructor()
110  ->getMock();
111  $backend->expects($this->once())->method('has')->with($this->equalTo('StringCacheTest'))->will($this->returnValue(true));
112  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
113  $this->assertTrue($cache->has('StringCacheTest'), 'has() did not return TRUE.');
114  }
115 
119  public function removeCallsBackend()
120  {
121  $cacheIdentifier = 'someCacheIdentifier';
122  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
123  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
124  ->disableOriginalConstructor()
125  ->getMock();
126  $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
127  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
128  $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
129  }
130 
134  public function getByTagRejectsInvalidTags()
135  {
136  $this->expectException(\InvalidArgumentException::class);
137  $this->expectExceptionCode(1233057772);
138 
139  $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class);
140  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
141  $cache->getByTag('SomeInvalid\\Tag');
142  }
143 
147  public function getByTagCallsBackend()
148  {
149  $tag = 'sometag';
150  $identifiers = ['one', 'two'];
151  $entries = ['one value', 'two value'];
152  $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
153  ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
154  ->disableOriginalConstructor()
155  ->getMock();
156  $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
157  $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls('one value', 'two value'));
158  $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
159  $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
160  }
161 }