‪TYPO3CMS  11.5
VariableFrontendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪VariableFrontendTest extends UnitTestCase
28 {
32  public function ‪setChecksIfTheIdentifierIsValid(): void
33  {
34  $this->expectException(\InvalidArgumentException::class);
35  $this->expectExceptionCode(1233058264);
36 
37  $cache = $this->getMockBuilder(VariableFrontend::class)
38  ->onlyMethods(['isValidEntryIdentifier'])
39  ->disableOriginalConstructor()
40  ->getMock();
41  $cache->expects(self::once())->method('isValidEntryIdentifier')->with('foo')->willReturn(false);
42  $cache->set('foo', 'bar');
43  }
44 
48  public function ‪setPassesSerializedStringToBackend(): void
49  {
50  $theString = 'Just some value';
51  $backend = $this->getMockBuilder(AbstractBackend::class)
52  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
53  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
54  ->disableOriginalConstructor()
55  ->getMock();
56  $backend->expects(self::once())->method('set')->with(self::equalTo('VariableCacheTest'), self::equalTo(serialize($theString)));
57 
58  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
59  $cache->set('VariableCacheTest', $theString);
60  }
61 
65  public function ‪setPassesSerializedArrayToBackend(): void
66  {
67  $theArray = ['Just some value', 'and another one.'];
68  $backend = $this->getMockBuilder(AbstractBackend::class)
69  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
70  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
71  ->disableOriginalConstructor()
72  ->getMock();
73  $backend->expects(self::once())->method('set')->with(self::equalTo('VariableCacheTest'), self::equalTo(serialize($theArray)));
74 
75  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
76  $cache->set('VariableCacheTest', $theArray);
77  }
78 
82  public function ‪setPassesLifetimeToBackend(): void
83  {
84  $theString = 'Just some value';
85  $theLifetime = 1234;
86  $backend = $this->getMockBuilder(AbstractBackend::class)
87  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
88  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
89  ->disableOriginalConstructor()
90  ->getMock();
91  $backend->expects(self::once())->method('set')->with(self::equalTo('VariableCacheTest'), self::equalTo(serialize($theString)), self::equalTo([]), self::equalTo($theLifetime));
92 
93  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
94  $cache->set('VariableCacheTest', $theString, [], $theLifetime);
95  }
96 
100  public function ‪getFetchesStringValueFromBackend(): void
101  {
102  $backend = $this->getMockBuilder(AbstractBackend::class)
103  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
104  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
105  ->disableOriginalConstructor()
106  ->getMock();
107  $backend->expects(self::once())->method('get')->willReturn(serialize('Just some value'));
108 
109  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
110  self::assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
111  }
112 
116  public function ‪getFetchesArrayValueFromBackend(): void
117  {
118  $theArray = ['Just some value', 'and another one.'];
119  $backend = $this->getMockBuilder(AbstractBackend::class)
120  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
121  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
122  ->disableOriginalConstructor()
123  ->getMock();
124  $backend->expects(self::once())->method('get')->willReturn(serialize($theArray));
125 
126  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
127  self::assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
128  }
129 
134  {
135  $backend = $this->getMockBuilder(AbstractBackend::class)
136  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
137  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
138  ->disableOriginalConstructor()
139  ->getMock();
140  $backend->expects(self::once())->method('get')->willReturn(serialize(false));
141 
142  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
143  self::assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
144  }
145 
149  public function ‪hasReturnsResultFromBackend(): void
150  {
151  $backend = $this->getMockBuilder(AbstractBackend::class)
152  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
153  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
154  ->disableOriginalConstructor()
155  ->getMock();
156  $backend->expects(self::once())->method('has')->with(self::equalTo('VariableCacheTest'))->willReturn(true);
157 
158  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
159  self::assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
160  }
161 
165  public function ‪removeCallsBackend(): void
166  {
167  $cacheIdentifier = 'someCacheIdentifier';
168  $backend = $this->getMockBuilder(AbstractBackend::class)
169  ->onlyMethods(['get', 'set', 'has', 'remove', 'flush', 'collectGarbage'])
170  ->addMethods(['findIdentifiersByTag', 'flushByTag'])
171  ->disableOriginalConstructor()
172  ->getMock();
173 
174  $backend->expects(self::once())->method('remove')->with(self::equalTo($cacheIdentifier))->willReturn(true);
175 
176  $cache = new ‪VariableFrontend('VariableFrontend', $backend);
177  self::assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
178  }
179 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\getFetchesArrayValueFromBackend
‪getFetchesArrayValueFromBackend()
Definition: VariableFrontendTest.php:116
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\setPassesSerializedStringToBackend
‪setPassesSerializedStringToBackend()
Definition: VariableFrontendTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend
Definition: AbstractFrontendTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\getFetchesFalseBooleanValueFromBackend
‪getFetchesFalseBooleanValueFromBackend()
Definition: VariableFrontendTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\removeCallsBackend
‪removeCallsBackend()
Definition: VariableFrontendTest.php:165
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\getFetchesStringValueFromBackend
‪getFetchesStringValueFromBackend()
Definition: VariableFrontendTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\setPassesSerializedArrayToBackend
‪setPassesSerializedArrayToBackend()
Definition: VariableFrontendTest.php:65
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\setChecksIfTheIdentifierIsValid
‪setChecksIfTheIdentifierIsValid()
Definition: VariableFrontendTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\hasReturnsResultFromBackend
‪hasReturnsResultFromBackend()
Definition: VariableFrontendTest.php:149
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend
Definition: AbstractBackend.php:28
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest\setPassesLifetimeToBackend
‪setPassesLifetimeToBackend()
Definition: VariableFrontendTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\VariableFrontendTest
Definition: VariableFrontendTest.php:28