‪TYPO3CMS  ‪main
CacheFlushCommandTest.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\Test;
24 use TYPO3\CMS\Core\DependencyInjection\ContainerBuilder;
25 use TYPO3\CMS\Core\Package\PackageManager;
26 
28 {
30  'SYS' => [
31  'caching' => [
32  'cacheConfigurations' => [
33  // Set pages cache database backend, testing-framework sets this to NullBackend by default.
34  'pages' => [
35  'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
36  ],
37  ],
38  ],
39  ],
40  ];
41 
42  #[Test]
43  public function ‪cachesCanBeFlushed(): void
44  {
45  $containerBuilder = $this->get(ContainerBuilder::class);
46  $packageManager = $this->get(PackageManager::class);
47  $diCacheIdentifier = $containerBuilder->getCacheIdentifier($packageManager);
48  $diCacheFile = ‪Environment::getVarPath() . '/cache/code/di/' . $diCacheIdentifier . '.php';
49 
50  $siteConfiguration = $this->get(SiteConfiguration::class);
51  $pageCache = $this->get(CacheManager::class)->getCache('pages');
52 
53  // fill cache
54  $siteConfiguration->getAllExistingSites();
55  $pageCache->set('dummy-page-cache-hash', ['dummy'], [], 0);
56  // Reset DI cache timestamp
57  touch($diCacheFile, 0);
58 
59  self::assertFileExists(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
60 
61  $result = $this->‪executeConsoleCommand('cache:flush');
62 
63  self::assertEquals(0, $result['status']);
64  self::assertFileDoesNotExist(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
65  self::assertFalse($pageCache->has('dummy-page-cache-hash'));
66 
67  // It's fine that DI cache is available…
68  self::assertFileExists($diCacheFile);
69  // but it must have been renewed (seem behaviour as in installtool).
70  self::assertGreaterThan(0, filemtime($diCacheFile));
71  }
72 
73  #[Test]
74  public function ‪diCachesCanBeFlushed(): void
75  {
76  $containerBuilder = $this->get(ContainerBuilder::class);
77  $packageManager = $this->get(PackageManager::class);
78  $diCacheIdentifier = $containerBuilder->getCacheIdentifier($packageManager);
79  $diCacheFile = ‪Environment::getVarPath() . '/cache/code/di/' . $diCacheIdentifier . '.php';
80 
81  touch($diCacheFile);
82  self::assertFileExists($diCacheFile);
83 
84  $result = $this->‪executeConsoleCommand('cache:flush -g di');
85 
86  self::assertEquals(0, $result['status']);
87  self::assertFileDoesNotExist($diCacheFile);
88  }
89 
90  #[Test]
91  public function ‪systemCachesCanBeFlushed(): void
92  {
93  $containerBuilder = $this->get(ContainerBuilder::class);
94  $packageManager = $this->get(PackageManager::class);
95  $diCacheIdentifier = $containerBuilder->getCacheIdentifier($packageManager);
96 
97  $siteConfiguration = $this->get(SiteConfiguration::class);
98  $pageCache = $this->get(CacheManager::class)->getCache('pages');
99  $diCacheFile = ‪Environment::getVarPath() . '/cache/code/di/' . $diCacheIdentifier . '.php';
100 
101  // fill cache
102  $siteConfiguration->getAllExistingSites();
103  $pageCache->set('dummy-page-cache-hash', ['dummy'], [], 0);
104  // Reset DI cache timestamp
105  touch($diCacheFile, 0);
106 
107  self::assertFileExists(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
108 
109  $result = $this->‪executeConsoleCommand('cache:flush -g system');
110 
111  self::assertEquals(0, $result['status']);
112  // site caches should have been flushed
113  self::assertFileDoesNotExist(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
114  // page caches should stay
115  self::assertTrue($pageCache->has('dummy-page-cache-hash'));
116 
117  // It's fine that DI cache is available…
118  self::assertFileExists($diCacheFile);
119  // but it must have been renewed (seem behaviour as in installtool).
120  self::assertGreaterThan(0, filemtime($diCacheFile));
121  }
122 
123  #[Test]
124  public function ‪pageCachesCanBeFlushed(): void
125  {
126  $containerBuilder = $this->get(ContainerBuilder::class);
127  $packageManager = $this->get(PackageManager::class);
128  $diCacheIdentifier = $containerBuilder->getCacheIdentifier($packageManager);
129 
130  $siteConfiguration = $this->get(SiteConfiguration::class);
131  $pageCache = $this->get(CacheManager::class)->getCache('pages');
132  $diCacheFile = ‪Environment::getVarPath() . '/cache/code/di/' . $diCacheIdentifier . '.php';
133 
134  // fill cache
135  $siteConfiguration->getAllExistingSites();
136  $pageCache->set('dummy-page-cache-hash', ['dummy'], [], 0);
137  // Reset DI cache timestamp
138  touch($diCacheFile, 0);
139 
140  self::assertFileExists(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
141 
142  $result = $this->‪executeConsoleCommand('cache:flush -g pages');
143 
144  self::assertEquals(0, $result['status']);
145  // site cache should stay
146  self::assertFileExists(‪Environment::getVarPath() . '/cache/code/core/sites-configuration.php');
147  // page caches should have been flushed
148  self::assertFalse($pageCache->has('dummy-page-cache-hash'));
149 
150  // DI cache must be available…
151  self::assertFileExists($diCacheFile);
152  // and must not have been renewed
153  self::assertEquals(0, filemtime($diCacheFile));
154  }
155 }
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest\systemCachesCanBeFlushed
‪systemCachesCanBeFlushed()
Definition: CacheFlushCommandTest.php:91
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest\cachesCanBeFlushed
‪cachesCanBeFlushed()
Definition: CacheFlushCommandTest.php:43
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Tests\Functional\Command\AbstractCommandTestCase\executeConsoleCommand
‪executeConsoleCommand(string $cmdline,... $args)
Definition: AbstractCommandTestCase.php:25
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest\diCachesCanBeFlushed
‪diCachesCanBeFlushed()
Definition: CacheFlushCommandTest.php:74
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest\pageCachesCanBeFlushed
‪pageCachesCanBeFlushed()
Definition: CacheFlushCommandTest.php:124
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest
Definition: CacheFlushCommandTest.php:28
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Functional\Command\CacheFlushCommandTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: CacheFlushCommandTest.php:29
‪TYPO3\CMS\Core\Tests\Functional\Command
Definition: AbstractCommandTestCase.php:18
‪TYPO3\CMS\Core\Tests\Functional\Command\AbstractCommandTestCase
Definition: AbstractCommandTestCase.php:24