|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Commands\Utilities; |
| 15 | + |
| 16 | +use Closure; |
| 17 | +use CodeIgniter\Autoloader\FileLocator; |
| 18 | +use CodeIgniter\Autoloader\FileLocatorCached; |
| 19 | +use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; |
| 20 | +use CodeIgniter\Test\CIUnitTestCase; |
| 21 | +use CodeIgniter\Test\ReflectionHelper; |
| 22 | +use CodeIgniter\Test\StreamFilterTrait; |
| 23 | +use Config\Services; |
| 24 | +use PHPUnit\Framework\Attributes\Group; |
| 25 | + |
| 26 | +/** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | +#[Group('Others')] |
| 30 | +final class OptimizeTest extends CIUnitTestCase |
| 31 | +{ |
| 32 | + use ReflectionHelper; |
| 33 | + use StreamFilterTrait; |
| 34 | + |
| 35 | + private FileVarExportHandler $handler; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + $this->handler = new FileVarExportHandler(); |
| 42 | + $this->handler->delete('FileLocatorCache'); |
| 43 | + } |
| 44 | + |
| 45 | + protected function tearDown(): void |
| 46 | + { |
| 47 | + parent::tearDown(); |
| 48 | + |
| 49 | + $this->handler->delete('FileLocatorCache'); |
| 50 | + Services::resetSingle('locator'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return Closure(): void |
| 55 | + */ |
| 56 | + private function getClearCache(): Closure |
| 57 | + { |
| 58 | + return self::getPrivateMethodInvoker(new Optimize(service('logger'), service('commands')), 'clearCache'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testClearCacheDiscardsSharedLocatorCache(): void |
| 62 | + { |
| 63 | + $locator = new FileLocatorCached(new FileLocator(service('autoloader')), $this->handler); |
| 64 | + $locator->search('Config/App'); |
| 65 | + Services::injectMock('locator', $locator); |
| 66 | + |
| 67 | + ($this->getClearCache())(); |
| 68 | + |
| 69 | + $locator->search('Config/Cache'); |
| 70 | + $locator->__destruct(); |
| 71 | + |
| 72 | + $cached = $this->handler->get('FileLocatorCache'); |
| 73 | + |
| 74 | + $this->assertArrayNotHasKey('Config/App', $cached['search']); |
| 75 | + $this->assertArrayHasKey('Config/Cache', $cached['search']); |
| 76 | + $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testClearCacheDeletesTheFileWithoutSharedLocatorCache(): void |
| 80 | + { |
| 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()); |
| 87 | + } |
| 88 | +} |
0 commit comments