[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / UnitDeprecated / Cache / Frontend / StringFrontendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\UnitDeprecated\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 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Testcase for the string cache frontend
21 */
22 class StringFrontendTest extends UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function setChecksIfTheIdentifierIsValid()
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
40 /**
41 * @test
42 */
43 public function setPassesStringToBackend()
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('StringCacheTest'), $this->equalTo($theString));
51 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
52 $cache->set('StringCacheTest', $theString);
53 }
54
55 /**
56 * @test
57 */
58 public function setPassesLifetimeToBackend()
59 {
60 $theString = 'Just some value';
61 $theLifetime = 1234;
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('StringCacheTest'), $this->equalTo($theString), $this->equalTo([]), $this->equalTo($theLifetime));
67 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
68 $cache->set('StringCacheTest', $theString, [], $theLifetime);
69 }
70
71 /**
72 * @test
73 */
74 public function setThrowsInvalidDataExceptionOnNonStringValues()
75 {
76 $this->expectException(InvalidDataException::class);
77 $this->expectExceptionCode(1222808333);
78
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 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
84 $cache->set('StringCacheTest', []);
85 }
86
87 /**
88 * @test
89 */
90 public function getFetchesStringValueFromBackend()
91 {
92 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
93 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
94 ->disableOriginalConstructor()
95 ->getMock();
96 $backend->expects($this->once())->method('get')->will($this->returnValue('Just some value'));
97 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
98 $this->assertEquals('Just some value', $cache->get('StringCacheTest'), 'The returned value was not the expected string.');
99 }
100
101 /**
102 * @test
103 */
104 public function hasReturnsResultFromBackend()
105 {
106 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
107 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
108 ->disableOriginalConstructor()
109 ->getMock();
110 $backend->expects($this->once())->method('has')->with($this->equalTo('StringCacheTest'))->will($this->returnValue(true));
111 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
112 $this->assertTrue($cache->has('StringCacheTest'), 'has() did not return TRUE.');
113 }
114
115 /**
116 * @test
117 */
118 public function removeCallsBackend()
119 {
120 $cacheIdentifier = 'someCacheIdentifier';
121 $backend = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class)
122 ->setMethods(['get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'])
123 ->disableOriginalConstructor()
124 ->getMock();
125 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(true));
126 $cache = new \TYPO3\CMS\Core\Cache\Frontend\StringFrontend('StringFrontend', $backend);
127 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
128 }
129 }