diff --git a/packages/view/src/Initializers/ViewCacheInitializer.php b/packages/view/src/Initializers/ViewCacheInitializer.php index 3fcff7ad7..14befdca9 100644 --- a/packages/view/src/Initializers/ViewCacheInitializer.php +++ b/packages/view/src/Initializers/ViewCacheInitializer.php @@ -28,6 +28,6 @@ private function shouldCacheBeEnabled(): bool return false; } - return (bool) env('VIEW_CACHE', default: Environment::guessFromEnvironment()); + return (bool) env('VIEW_CACHE', default: Environment::guessFromEnvironment()->requiresCaution()); } } diff --git a/tests/Integration/View/ViewCacheTest.php b/tests/Integration/View/ViewCacheTest.php index 86b609b99..a594a6166 100644 --- a/tests/Integration/View/ViewCacheTest.php +++ b/tests/Integration/View/ViewCacheTest.php @@ -4,6 +4,7 @@ namespace Tests\Tempest\Integration\View; +use PHPUnit\Framework\Attributes\Test; use Tempest\View\ViewCache; use Tempest\View\ViewCachePool; use Tests\Tempest\Integration\FrameworkIntegrationTestCase; @@ -42,9 +43,65 @@ protected function tearDown(): void rmdir(self::DIRECTORY); } + putenv('ENVIRONMENT=testing'); + putenv('VIEW_CACHE='); + putenv('INTERNAL_CACHES='); + parent::tearDown(); } + #[Test] + public function enabled_by_default_in_production(): void + { + putenv('ENVIRONMENT=production'); + + $this->container->unregister(ViewCache::class); + + $this->assertTrue($this->container->get(ViewCache::class)->enabled); + } + + #[Test] + public function enabled_by_default_in_staging(): void + { + putenv('ENVIRONMENT=staging'); + + $this->container->unregister(ViewCache::class); + + $this->assertTrue($this->container->get(ViewCache::class)->enabled); + } + + #[Test] + public function disabled_by_default_locally(): void + { + putenv('ENVIRONMENT=local'); + + $this->container->unregister(ViewCache::class); + + $this->assertFalse($this->container->get(ViewCache::class)->enabled); + } + + #[Test] + public function overriden_by_internal_caches_in_production(): void + { + putenv('ENVIRONMENT=production'); + putenv('INTERNAL_CACHES=false'); + + $this->container->unregister(ViewCache::class); + + $this->assertFalse($this->container->get(ViewCache::class)->enabled); + } + + #[Test] + public function overriden_by_view_cache_locally(): void + { + putenv('ENVIRONMENT=local'); + putenv('VIEW_CACHE=true'); + + $this->container->unregister(ViewCache::class); + + $this->assertTrue($this->container->get(ViewCache::class)->enabled); + } + public function test_view_cache(): void { $path = $this->viewCache->getCachedViewPath('path', fn () => 'hi');