|
22 | 22 | use CodeIgniter\Test\StreamFilterTrait; |
23 | 23 | use Config\Services; |
24 | 24 | use PHPUnit\Framework\Attributes\Group; |
| 25 | +use Tests\Support\Cache\IsolatedCacheTrait; |
25 | 26 |
|
26 | 27 | /** |
27 | 28 | * @internal |
28 | 29 | */ |
29 | 30 | #[Group('Others')] |
30 | 31 | final class OptimizeTest extends CIUnitTestCase |
31 | 32 | { |
| 33 | + use IsolatedCacheTrait; |
32 | 34 | use ReflectionHelper; |
33 | 35 | use StreamFilterTrait; |
34 | 36 |
|
35 | 37 | private FileVarExportHandler $handler; |
36 | 38 |
|
| 39 | + public static function setUpBeforeClass(): void |
| 40 | + { |
| 41 | + parent::setUpBeforeClass(); |
| 42 | + |
| 43 | + self::setUpBeforeClassIsolatedCache(); |
| 44 | + } |
| 45 | + |
| 46 | + public static function tearDownAfterClass(): void |
| 47 | + { |
| 48 | + self::tearDownAfterClassIsolatedCache(); |
| 49 | + |
| 50 | + parent::tearDownAfterClass(); |
| 51 | + } |
| 52 | + |
37 | 53 | protected function setUp(): void |
38 | 54 | { |
39 | 55 | parent::setUp(); |
40 | 56 |
|
41 | | - $this->handler = new FileVarExportHandler(); |
| 57 | + $this->handler = $this->getIsolatedCacheHandler(); |
42 | 58 | $this->handler->delete('FileLocatorCache'); |
43 | 59 | } |
44 | 60 |
|
@@ -74,15 +90,52 @@ public function testClearCacheDiscardsSharedLocatorCache(): void |
74 | 90 | $this->assertArrayNotHasKey('Config/App', $cached['search']); |
75 | 91 | $this->assertArrayHasKey('Config/Cache', $cached['search']); |
76 | 92 | $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); |
| 93 | + |
| 94 | + // Prevent the destructor from writing the cache file |
| 95 | + // when the locator is destroyed. |
| 96 | + $locator->deleteCache(); |
77 | 97 | } |
78 | 98 |
|
79 | 99 | public function testClearCacheDeletesTheFileWithoutSharedLocatorCache(): void |
80 | 100 | { |
81 | | - $this->handler->save('FileLocatorCache', ['search' => []]); |
82 | | - |
83 | | - ($this->getClearCache())(); |
84 | | - |
85 | | - $this->assertFalse($this->handler->get('FileLocatorCache')); |
86 | | - $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); |
| 101 | + $writePath = rtrim(sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'ci_test_optimize_' . uniqid('', true) . DIRECTORY_SEPARATOR; |
| 102 | + mkdir($writePath . 'cache', 0755, true); |
| 103 | + |
| 104 | + $escapedWritePath = var_export($writePath, true); |
| 105 | + $escapedBootstrap = var_export(SYSTEMPATH . 'Test/bootstrap.php', true); |
| 106 | + |
| 107 | + try { |
| 108 | + $code = <<<PHP |
| 109 | + define('WRITEPATH', {$escapedWritePath}); |
| 110 | + require {$escapedBootstrap}; |
| 111 | +
|
| 112 | + use CodeIgniter\\Cache\\FactoriesCache\\FileVarExportHandler; |
| 113 | + use CodeIgniter\\Commands\\Utilities\\Optimize; |
| 114 | + use CodeIgniter\\Test\\ReflectionHelper; |
| 115 | +
|
| 116 | + \$handler = new FileVarExportHandler(); |
| 117 | + \$handler->save('FileLocatorCache', ['search' => []]); |
| 118 | +
|
| 119 | + \$clearCache = ReflectionHelper::getPrivateMethodInvoker( |
| 120 | + new Optimize(service('logger'), service('commands')), |
| 121 | + 'clearCache' |
| 122 | + ); |
| 123 | + \$clearCache(); |
| 124 | +
|
| 125 | + if (\$handler->get('FileLocatorCache') !== false) { |
| 126 | + exit(1); |
| 127 | + } |
| 128 | + PHP; |
| 129 | + |
| 130 | + $cmd = sprintf('%s -r %s 2>&1', escapeshellarg(PHP_BINARY), escapeshellarg($code)); |
| 131 | + exec($cmd, $output, $status); |
| 132 | + |
| 133 | + $this->assertSame(0, $status, implode(PHP_EOL, $output)); |
| 134 | + $this->assertStringContainsString('Removed FileLocatorCache.', implode(PHP_EOL, $output)); |
| 135 | + } finally { |
| 136 | + helper('filesystem'); |
| 137 | + delete_files($writePath, true); |
| 138 | + @rmdir($writePath); |
| 139 | + } |
87 | 140 | } |
88 | 141 | } |
0 commit comments