Repository navigation
fix(Cache): isolate the factories cache files used by the tests #10647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gr8man
wants to merge
3
commits into
codeigniter4:develop
Choose a base branch
from
gr8man:fix/random-tests-locator-cache-isolation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Tests\Support\Cache; | ||
|
|
||
| use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; | ||
|
|
||
| /** | ||
| * Gives the test class its own directory for the files created | ||
| * by the FileVarExportHandler. | ||
| * | ||
| * The handler stores its files in the shared "writable/cache" directory by | ||
| * default. Since the random test execution runs the components in parallel, | ||
| * one component may delete the cache file that another component is | ||
| * asserting on. A dedicated directory per test class avoids that. | ||
| */ | ||
| trait IsolatedCacheTrait | ||
| { | ||
| /** | ||
| * The directory used to store the cache files of the test class. | ||
| */ | ||
| private static string $isolatedCachePath; | ||
|
|
||
| public static function setUpBeforeClassIsolatedCache(): void | ||
| { | ||
| helper('filesystem'); | ||
|
|
||
| self::$isolatedCachePath = WRITEPATH . 'cache_test_' . str_replace('\\', '_', static::class); | ||
|
|
||
| // Remove the files left over by a previous run. | ||
| if (is_dir(self::$isolatedCachePath)) { | ||
| delete_files(self::$isolatedCachePath, true); | ||
| @rmdir(self::$isolatedCachePath); | ||
| } | ||
| } | ||
|
|
||
| public static function tearDownAfterClassIsolatedCache(): void | ||
| { | ||
| if (! isset(self::$isolatedCachePath) || ! is_dir(self::$isolatedCachePath)) { | ||
| return; | ||
| } | ||
|
|
||
| delete_files(self::$isolatedCachePath, true); | ||
| @rmdir(self::$isolatedCachePath); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a handler that stores its files in the isolated directory. | ||
| */ | ||
| protected function getIsolatedCacheHandler(): FileVarExportHandler | ||
| { | ||
| return new FileVarExportHandler(self::$isolatedCachePath); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path of the isolated cache directory. | ||
| */ | ||
| protected static function getIsolatedCachePath(): string | ||
| { | ||
| return self::$isolatedCachePath; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,34 +15,52 @@ | |
|
|
||
| use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use Tests\Support\Cache\IsolatedCacheTrait; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase | ||
| { | ||
| use IsolatedCacheTrait; | ||
|
|
||
| public static function setUpBeforeClass(): void | ||
| { | ||
| parent::setUpBeforeClass(); | ||
|
|
||
| self::setUpBeforeClassIsolatedCache(); | ||
|
|
||
| helper('filesystem'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed as the trait now loads it. |
||
| } | ||
|
|
||
| public static function tearDownAfterClass(): void | ||
| { | ||
| self::tearDownAfterClassIsolatedCache(); | ||
|
|
||
| parent::tearDownAfterClass(); | ||
| } | ||
|
|
||
| protected function createFactoriesCache(): void | ||
| { | ||
| $this->handler = new FileVarExportHandler(); | ||
| $this->handler = $this->getIsolatedCacheHandler(); | ||
| $this->cache = new FactoriesCache($this->handler); | ||
| } | ||
|
|
||
| public function testUsesTheWritableCacheDirectoryByDefault(): void | ||
| { | ||
| $handler = new FileVarExportHandler(); | ||
|
|
||
| $this->assertSame(WRITEPATH . 'cache', $this->getPrivateProperty($handler, 'path')); | ||
| } | ||
|
|
||
| public function testSaveCreatesDirectoryAndFileWithCorrectPermissions(): void | ||
| { | ||
| $dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true); | ||
| $oldUmask = umask(0000); | ||
|
|
||
| try { | ||
| $handler = new FileVarExportHandler(); | ||
| $this->setPrivateProperty($handler, 'path', $dir); | ||
| $handler = new FileVarExportHandler($dir); | ||
|
|
||
| $handler->save('test_key', ['data']); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,23 +22,39 @@ | |
| use CodeIgniter\Test\StreamFilterTrait; | ||
| use Config\Services; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use Tests\Support\Cache\IsolatedCacheTrait; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class OptimizeTest extends CIUnitTestCase | ||
| { | ||
| use IsolatedCacheTrait; | ||
| use ReflectionHelper; | ||
| use StreamFilterTrait; | ||
|
|
||
| private FileVarExportHandler $handler; | ||
|
|
||
| public static function setUpBeforeClass(): void | ||
| { | ||
| parent::setUpBeforeClass(); | ||
|
|
||
| self::setUpBeforeClassIsolatedCache(); | ||
| } | ||
|
|
||
| public static function tearDownAfterClass(): void | ||
| { | ||
| self::tearDownAfterClassIsolatedCache(); | ||
|
|
||
| parent::tearDownAfterClass(); | ||
| } | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->handler = new FileVarExportHandler(); | ||
| $this->handler = $this->getIsolatedCacheHandler(); | ||
| $this->handler->delete('FileLocatorCache'); | ||
| } | ||
|
|
||
|
|
@@ -74,15 +90,52 @@ public function testClearCacheDiscardsSharedLocatorCache(): void | |
| $this->assertArrayNotHasKey('Config/App', $cached['search']); | ||
| $this->assertArrayHasKey('Config/Cache', $cached['search']); | ||
| $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); | ||
|
|
||
| // Prevent the destructor from writing the cache file | ||
| // when the locator is destroyed. | ||
| $locator->deleteCache(); | ||
| } | ||
|
|
||
| public function testClearCacheDeletesTheFileWithoutSharedLocatorCache(): void | ||
| { | ||
| $this->handler->save('FileLocatorCache', ['search' => []]); | ||
|
|
||
| ($this->getClearCache())(); | ||
|
|
||
| $this->assertFalse($this->handler->get('FileLocatorCache')); | ||
| $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); | ||
| $writePath = rtrim(sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'ci_test_optimize_' . uniqid('', true) . DIRECTORY_SEPARATOR; | ||
| mkdir($writePath . 'cache', 0755, true); | ||
|
|
||
| $escapedWritePath = var_export($writePath, true); | ||
| $escapedBootstrap = var_export(SYSTEMPATH . 'Test/bootstrap.php', true); | ||
|
|
||
| try { | ||
| $code = <<<PHP | ||
| define('WRITEPATH', {$escapedWritePath}); | ||
| require {$escapedBootstrap}; | ||
|
|
||
| use CodeIgniter\\Cache\\FactoriesCache\\FileVarExportHandler; | ||
| use CodeIgniter\\Commands\\Utilities\\Optimize; | ||
| use CodeIgniter\\Test\\ReflectionHelper; | ||
|
|
||
| \$handler = new FileVarExportHandler(); | ||
| \$handler->save('FileLocatorCache', ['search' => []]); | ||
|
|
||
| \$clearCache = ReflectionHelper::getPrivateMethodInvoker( | ||
| new Optimize(service('logger'), service('commands')), | ||
| 'clearCache' | ||
| ); | ||
| \$clearCache(); | ||
|
|
||
| if (\$handler->get('FileLocatorCache') !== false) { | ||
| exit(1); | ||
| } | ||
| PHP; | ||
|
|
||
| $cmd = sprintf('%s -r %s 2>&1', escapeshellcmd(PHP_BINARY), escapeshellarg($code)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
| exec($cmd, $output, $status); | ||
|
|
||
| $this->assertSame(0, $status, implode(PHP_EOL, $output)); | ||
| $this->assertStringContainsString('Removed FileLocatorCache.', implode(PHP_EOL, $output)); | ||
| } finally { | ||
| helper('filesystem'); | ||
| delete_files($writePath, true); | ||
| @rmdir($writePath); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is already redundant since
self::tearDownAfterClassIsolatedCache()deletes the cache