A free open source IT asset/license management system
Fix optimize test stepping on other workers
Marcus Mooreb7b1a1fd2bd0
diff --git a/tests/Feature/Console/OptimizeTest.php b/tests/Feature/Console/OptimizeTest.phpindex 8dabe8bcce..6f76572a8b 100644--- a/tests/Feature/Console/OptimizeTest.php+++ b/tests/Feature/Console/OptimizeTest.php@@ -2,18 +2,85 @@namespace Tests\Feature\Console;+use Illuminate\Support\Facades\File;use Tests\TestCase;class OptimizeTest extends TestCase{- public function test_optimize_succeeds()+ private const RELOCATABLE_CACHES = [+ 'APP_CONFIG_CACHE' => 'config.php',+ 'APP_EVENTS_CACHE' => 'events.php',+ 'APP_ROUTES_CACHE' => 'routes-v7.php',+ ];++ private array $originalCacheEnv = [];++ private string $relocatedCacheDirectory;++ protected function setUp(): void{+ parent::setUp();++ $this->relocatedCacheDirectory = storage_path('framework/testing/optimize-'.getmypid());++ $this->snapshotCachePathEnv();+ $this->relocateFrameworkCaches();+$this->beforeApplicationDestroyed(function () {- $this->artisan('config:clear');- $this->artisan('route:clear');- $this->artisan('view:clear');+ File::deleteDirectory($this->relocatedCacheDirectory);+ $this->restoreCachePathEnv();});+ }+ public function test_optimize_succeeds(): void+ {$this->artisan('optimize')->assertSuccessful();}++ /**+ * Record every cache path env var as it stands before this test+ * relocates it.+ */+ private function snapshotCachePathEnv(): void+ {+ foreach (array_keys(self::RELOCATABLE_CACHES) as $key) {+ $this->originalCacheEnv[$key] = [+ 'env' => $_ENV[$key] ?? false,+ 'server' => $_SERVER[$key] ?? false,+ ];+ }+ }++ /**+ * Point the three relocatable caches at an isolated+ * directory to avoid workers stepping on each other.+ */+ private function relocateFrameworkCaches(): void+ {+ File::ensureDirectoryExists($this->relocatedCacheDirectory);++ foreach (self::RELOCATABLE_CACHES as $key => $filename) {+ $path = $this->relocatedCacheDirectory.'/'.$filename;++ $_ENV[$key] = $_SERVER[$key] = $path;+ }+ }++ /**+ * Put every cache path env var back exactly as it was before this test+ * ran, so a relocated path can't leak into the next test's config boot.+ */+ private function restoreCachePathEnv(): void+ {+ foreach ($this->originalCacheEnv as $key => $original) {+ unset($_ENV[$key], $_SERVER[$key]);++ if ($original['env'] !== false) {+ $_ENV[$key] = $original['env'];+ }+ if ($original['server'] !== false) {+ $_SERVER[$key] = $original['server'];+ }+ }+ }}