‪TYPO3CMS  ‪main
PhpFrontendTest.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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 final class ‪PhpFrontendTest extends UnitTestCase
30 {
31  public static function ‪constructAcceptsValidIdentifiersDataProvider(): array
32  {
33  return [
34  ['x'],
35  ['someValue'],
36  ['123fivesixseveneight'],
37  ['some&'],
38  ['ab_cd%'],
39  [rawurlencode('resource://some/äöü$&% sadf')],
40  [str_repeat('x', 250)],
41  ];
42  }
43 
44  #[Test]
45  #[DataProvider('constructAcceptsValidIdentifiersDataProvider')]
47  {
48  new ‪PhpFrontend(‪$identifier, $this->createMock(PhpCapableBackendInterface::class));
49  }
50 
51  public static function ‪constructRejectsInvalidIdentifiersDataProvider(): array
52  {
53  return [
54  [''],
55  ['abc def'],
56  ['foo!'],
57  ['bar:'],
58  ['some/'],
59  ['bla*'],
60  ['one+'],
61  ['äöü'],
62  [str_repeat('x', 251)],
63  ['x$'],
64  ['\\a'],
65  ['b#'],
66  ];
67  }
68 
69  #[Test]
70  #[DataProvider('constructRejectsInvalidIdentifiersDataProvider')]
72  {
73  $this->expectException(\InvalidArgumentException::class);
74  $this->expectExceptionCode(1203584729);
75  new ‪PhpFrontend(‪$identifier, $this->createMock(PhpCapableBackendInterface::class));
76  }
77 
78  #[Test]
79  public function ‪flushCallsBackend(): void
80  {
81  $backend = $this->createMock(PhpCapableBackendInterface::class);
82  $backend->expects(self::once())->method('flush');
83  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
84  $cache->flush();
85  }
86 
87  #[Test]
88  public function ‪flushByTagRejectsInvalidTags(): void
89  {
90  $this->expectException(\InvalidArgumentException::class);
91  $this->expectExceptionCode(1233057359);
92  $backend = $this->createMock(FileBackend::class);
93  $backend->expects(self::never())->method('flushByTag');
94  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
95  $cache->flushByTag('SomeInvalid\\Tag');
96  }
97 
98  #[Test]
100  {
101  $tag = 'someTag';
102  $backend = $this->createMock(FileBackend::class);
103  $backend->expects(self::once())->method('flushByTag')->with($tag);
104  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
105  $cache->flushByTag($tag);
106  }
107 
108  #[Test]
110  {
111  $tag = 'someTag';
112  $backend = $this->createMock(FileBackend::class);
113  $backend->expects(self::once())->method('flushByTags')->with([$tag]);
114  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
115  $cache->flushByTags([$tag]);
116  }
117 
118  #[Test]
119  public function ‪collectGarbageCallsBackend(): void
120  {
121  $backend = $this->createMock(PhpCapableBackendInterface::class);
122  $backend->expects(self::once())->method('collectGarbage');
123  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
124  $cache->collectGarbage();
125  }
126 
128  {
129  return [
130  [''],
131  ['abc def'],
132  ['foo!'],
133  ['bar:'],
134  ['some/'],
135  ['bla*'],
136  ['one+'],
137  ['äöü'],
138  [str_repeat('x', 251)],
139  ['x$'],
140  ['\\a'],
141  ['b#'],
142  ];
143  }
144 
145  #[Test]
146  #[DataProvider('isValidEntryIdentifierReturnsFalseWithValidIdentifierDataProvider')]
148  {
149  $backend = $this->createMock(PhpCapableBackendInterface::class);
150  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
151  self::assertFalse($cache->isValidEntryIdentifier(‪$identifier));
152  }
153 
155  {
156  return [
157  ['_'],
158  ['abcdef'],
159  ['foo'],
160  ['bar123'],
161  ['3some'],
162  ['_bl_a'],
163  ['some&'],
164  ['one%TWO'],
165  [str_repeat('x', 250)],
166  ];
167  }
168 
169  #[Test]
170  #[DataProvider('isValidEntryIdentifierReturnsTrueWithValidIdentifierDataProvider')]
172  {
173  $backend = $this->createMock(PhpCapableBackendInterface::class);
174  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
175  self::assertTrue($cache->isValidEntryIdentifier(‪$identifier));
176  }
177 
179  {
180  return [
181  [''],
182  ['abc def'],
183  ['foo!'],
184  ['bar:'],
185  ['some/'],
186  ['bla*'],
187  ['one+'],
188  ['äöü'],
189  [str_repeat('x', 251)],
190  ['x$'],
191  ['\\a'],
192  ['b#'],
193  ];
194  }
195 
196  #[Test]
197  #[DataProvider('isValidTagReturnsFalseWithInvalidTagDataProvider')]
198  public function ‪isValidTagReturnsFalseWithInvalidTag(string $tag): void
199  {
200  $backend = $this->createMock(PhpCapableBackendInterface::class);
201  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
202  self::assertFalse($cache->isValidTag($tag));
203  }
204 
205  public static function ‪isValidTagReturnsTrueWithValidTagDataProvider(): array
206  {
207  return [
208  ['abcdef'],
209  ['foo-bar'],
210  ['foo_baar'],
211  ['bar123'],
212  ['3some'],
213  ['file%Thing'],
214  ['some&'],
215  ['%x%'],
216  [str_repeat('x', 250)],
217  ];
218  }
219 
220  #[Test]
221  #[DataProvider('isValidTagReturnsTrueWithValidTagDataProvider')]
222  public function ‪isValidTagReturnsTrueWithValidTag(string $tag): void
223  {
224  $backend = $this->createMock(PhpCapableBackendInterface::class);
225  $cache = new ‪PhpFrontend('someCacheIdentifier', $backend);
226  self::assertTrue($cache->isValidTag($tag));
227  }
228 
229  #[Test]
230  public function ‪setChecksIfTheIdentifierIsValid(): void
231  {
232  $this->expectException(\InvalidArgumentException::class);
233  $this->expectExceptionCode(1264023823);
234  $cache = new ‪PhpFrontend('someCacheIdentifier', $this->createMock(PhpCapableBackendInterface::class));
235  $cache->set('invalid identifier', 'bar');
236  }
237 
238  #[Test]
240  {
241  $originalSourceCode = 'return "hello world!";';
242  $modifiedSourceCode = '<?php' . chr(10) . $originalSourceCode . chr(10) . '#';
243  $mockBackend = $this->createMock(PhpCapableBackendInterface::class);
244  $mockBackend->expects(self::once())->method('set')->with('Foo-Bar', $modifiedSourceCode, ['tags'], 1234);
245  $cache = new ‪PhpFrontend('someCacheIdentifier', $mockBackend);
246  $cache->set('Foo-Bar', $originalSourceCode, ['tags'], 1234);
247  }
248 
249  #[Test]
251  {
252  $this->expectException(InvalidDataException::class);
253  $this->expectExceptionCode(1264023824);
254  $cache = new ‪PhpFrontend('someCacheIdentifier', $this->createMock(PhpCapableBackendInterface::class));
255  $cache->set('Foo-Bar', []);
256  }
257 
258  #[Test]
260  {
261  $mockBackend = $this->createMock(PhpCapableBackendInterface::class);
262  $mockBackend->expects(self::once())->method('requireOnce')->with('Foo-Bar')->willReturn('hello world!');
263  $cache = new ‪PhpFrontend('someCacheIdentifier', $mockBackend);
264  $result = $cache->requireOnce('Foo-Bar');
265  self::assertSame('hello world!', $result);
266  }
267 
268  #[Test]
270  {
271  $mockBackend = $this->createMock(SimpleFileBackend::class);
272  $mockBackend->expects(self::once())->method('require')->with('Foo-Bar')->willReturn('hello world!');
273  $cache = new ‪PhpFrontend('someCacheIdentifier', $mockBackend);
274  $result = $cache->require('Foo-Bar');
275  self::assertSame('hello world!', $result);
276  }
277 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidEntryIdentifierReturnsFalseWithValidIdentifier
‪isValidEntryIdentifierReturnsFalseWithValidIdentifier(string $identifier)
Definition: PhpFrontendTest.php:147
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\flushByTagRejectsInvalidTags
‪flushByTagRejectsInvalidTags()
Definition: PhpFrontendTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\constructRejectsInvalidIdentifiersDataProvider
‪static constructRejectsInvalidIdentifiersDataProvider()
Definition: PhpFrontendTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\setPassesPhpSourceCodeTagsAndLifetimeToBackend
‪setPassesPhpSourceCodeTagsAndLifetimeToBackend()
Definition: PhpFrontendTest.php:239
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidTagReturnsTrueWithValidTag
‪isValidTagReturnsTrueWithValidTag(string $tag)
Definition: PhpFrontendTest.php:222
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\collectGarbageCallsBackend
‪collectGarbageCallsBackend()
Definition: PhpFrontendTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend
Definition: PhpFrontendTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidEntryIdentifierReturnsTrueWithValidIdentifierDataProvider
‪static isValidEntryIdentifierReturnsTrueWithValidIdentifierDataProvider()
Definition: PhpFrontendTest.php:154
‪TYPO3\CMS\Core\Cache\Backend\FileBackend
Definition: FileBackend.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\flushByTagsCallsBackendIfItIsATaggableBackend
‪flushByTagsCallsBackendIfItIsATaggableBackend()
Definition: PhpFrontendTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\setThrowsInvalidDataExceptionOnNonStringValues
‪setThrowsInvalidDataExceptionOnNonStringValues()
Definition: PhpFrontendTest.php:250
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\requireOnceCallsTheBackendsRequireOnceMethod
‪requireOnceCallsTheBackendsRequireOnceMethod()
Definition: PhpFrontendTest.php:259
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\requireCallsTheBackendsRequireMethod
‪requireCallsTheBackendsRequireMethod()
Definition: PhpFrontendTest.php:269
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidEntryIdentifierReturnsTrueWithValidIdentifier
‪isValidEntryIdentifierReturnsTrueWithValidIdentifier(string $identifier)
Definition: PhpFrontendTest.php:171
‪TYPO3\CMS\Core\Cache\Backend\PhpCapableBackendInterface
Definition: PhpCapableBackendInterface.php:23
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidTagReturnsFalseWithInvalidTagDataProvider
‪static isValidTagReturnsFalseWithInvalidTagDataProvider()
Definition: PhpFrontendTest.php:178
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\flushByTagCallsBackendIfItIsATaggableBackend
‪flushByTagCallsBackendIfItIsATaggableBackend()
Definition: PhpFrontendTest.php:99
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\constructAcceptsValidIdentifiersDataProvider
‪static constructAcceptsValidIdentifiersDataProvider()
Definition: PhpFrontendTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidEntryIdentifierReturnsFalseWithValidIdentifierDataProvider
‪static isValidEntryIdentifierReturnsFalseWithValidIdentifierDataProvider()
Definition: PhpFrontendTest.php:127
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest
Definition: PhpFrontendTest.php:30
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidTagReturnsTrueWithValidTagDataProvider
‪static isValidTagReturnsTrueWithValidTagDataProvider()
Definition: PhpFrontendTest.php:205
‪TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend
Definition: SimpleFileBackend.php:33
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\isValidTagReturnsFalseWithInvalidTag
‪isValidTagReturnsFalseWithInvalidTag(string $tag)
Definition: PhpFrontendTest.php:198
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\constructRejectsInvalidIdentifiers
‪constructRejectsInvalidIdentifiers(string $identifier)
Definition: PhpFrontendTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\setChecksIfTheIdentifierIsValid
‪setChecksIfTheIdentifierIsValid()
Definition: PhpFrontendTest.php:230
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\constructAcceptsValidIdentifiers
‪constructAcceptsValidIdentifiers(string $identifier)
Definition: PhpFrontendTest.php:46
‪TYPO3\CMS\Core\Tests\Unit\Cache\Frontend\PhpFrontendTest\flushCallsBackend
‪flushCallsBackend()
Definition: PhpFrontendTest.php:79
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37