|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\DependencyInjection; |
| 4 | + |
| 5 | +use Override; |
| 6 | +use PHPStan\File\FileHelper; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use function count; |
| 9 | +use function getenv; |
| 10 | +use function glob; |
| 11 | +use function is_dir; |
| 12 | +use function md5; |
| 13 | +use function putenv; |
| 14 | +use function rmdir; |
| 15 | +use function sprintf; |
| 16 | +use function sys_get_temp_dir; |
| 17 | +use function uniqid; |
| 18 | +use function unlink; |
| 19 | +use const DIRECTORY_SEPARATOR; |
| 20 | + |
| 21 | +final class ContainerCacheKeyEnvTest extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + private const ENV_VAR = 'PHPSTAN_TEST_W1_CACHE_KEY'; |
| 25 | + |
| 26 | + private string $tmpDir; |
| 27 | + |
| 28 | + private string|false $envBackup; |
| 29 | + |
| 30 | + #[Override] |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->envBackup = getenv(self::ENV_VAR); |
| 34 | + $this->tmpDir = sys_get_temp_dir() . '/phpstan-container-cache-key-' . md5(uniqid()); |
| 35 | + $this->removeDirectory($this->tmpDir); |
| 36 | + } |
| 37 | + |
| 38 | + #[Override] |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + if ($this->envBackup === false) { |
| 42 | + putenv(self::ENV_VAR); |
| 43 | + } else { |
| 44 | + putenv(sprintf('%s=%s', self::ENV_VAR, $this->envBackup)); |
| 45 | + } |
| 46 | + |
| 47 | + $this->removeDirectory($this->tmpDir); |
| 48 | + } |
| 49 | + |
| 50 | + public function testEnvironmentIsDroppedFromCacheKeyWhenNoConfigReferencesIt(): void |
| 51 | + { |
| 52 | + // No loaded config uses %env.*%, so an unrelated env change must NOT recompile the container. |
| 53 | + putenv(sprintf('%s=first', self::ENV_VAR)); |
| 54 | + $this->buildContainer(__DIR__ . '/containerCacheKey-noenv.neon'); |
| 55 | + |
| 56 | + putenv(sprintf('%s=second', self::ENV_VAR)); |
| 57 | + $this->buildContainer(__DIR__ . '/containerCacheKey-noenv.neon'); |
| 58 | + |
| 59 | + $this->assertSame( |
| 60 | + 1, |
| 61 | + $this->countCompiledContainers(), |
| 62 | + 'Changing an unrelated environment variable should reuse the cached container.', |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function testEnvironmentStaysInCacheKeyWhenAConfigReferencesIt(): void |
| 67 | + { |
| 68 | + // A loaded config uses %env.*%, so the environment is relevant and a change must recompile. |
| 69 | + putenv(sprintf('%s=first', self::ENV_VAR)); |
| 70 | + $this->buildContainer(__DIR__ . '/containerCacheKey-withenv.neon'); |
| 71 | + |
| 72 | + putenv(sprintf('%s=second', self::ENV_VAR)); |
| 73 | + $this->buildContainer(__DIR__ . '/containerCacheKey-withenv.neon'); |
| 74 | + |
| 75 | + $this->assertSame( |
| 76 | + 2, |
| 77 | + $this->countCompiledContainers(), |
| 78 | + 'When a config references %env.*%, changing it must recompile the container.', |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function testDashedEnvVariableReferenceStaysInCacheKey(): void |
| 83 | + { |
| 84 | + // %env.MY-VAR% is a valid Nette reference (grammar %([\w.-]*)%): extraction must keep dashed |
| 85 | + // names too, otherwise changing the referenced var would reuse a stale container. |
| 86 | + $envName = 'PHPSTAN_TEST_W1-DASH'; |
| 87 | + $backup = getenv($envName); |
| 88 | + try { |
| 89 | + putenv(sprintf('%s=first', $envName)); |
| 90 | + $this->buildContainer(__DIR__ . '/containerCacheKey-withenv-dashed.neon'); |
| 91 | + |
| 92 | + putenv(sprintf('%s=second', $envName)); |
| 93 | + $this->buildContainer(__DIR__ . '/containerCacheKey-withenv-dashed.neon'); |
| 94 | + |
| 95 | + $this->assertSame( |
| 96 | + 2, |
| 97 | + $this->countCompiledContainers(), |
| 98 | + 'A %env.* reference whose name contains a dash must keep that var in the cache key.', |
| 99 | + ); |
| 100 | + } finally { |
| 101 | + if ($backup === false) { |
| 102 | + putenv($envName); |
| 103 | + } else { |
| 104 | + putenv(sprintf('%s=%s', $envName, $backup)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public function testBuildTimeEnvVariableStaysInCacheKey(): void |
| 110 | + { |
| 111 | + // FnsrExtension reads getenv('PHPSTAN_FNSR') at build time and rewires the container, so it |
| 112 | + // depends on the var even with no %env.*% in config; toggling it must recompile. |
| 113 | + $backup = getenv('PHPSTAN_FNSR'); |
| 114 | + try { |
| 115 | + putenv('PHPSTAN_FNSR=0'); |
| 116 | + $this->buildContainer(__DIR__ . '/containerCacheKey-noenv.neon'); |
| 117 | + |
| 118 | + putenv('PHPSTAN_FNSR=1'); |
| 119 | + $this->buildContainer(__DIR__ . '/containerCacheKey-noenv.neon'); |
| 120 | + |
| 121 | + $this->assertSame( |
| 122 | + 2, |
| 123 | + $this->countCompiledContainers(), |
| 124 | + 'Changing PHPSTAN_FNSR (read at build time by FnsrExtension) must recompile the container.', |
| 125 | + ); |
| 126 | + } finally { |
| 127 | + if ($backup === false) { |
| 128 | + putenv('PHPSTAN_FNSR'); |
| 129 | + } else { |
| 130 | + putenv(sprintf('PHPSTAN_FNSR=%s', $backup)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private function buildContainer(string $additionalConfigFile): void |
| 136 | + { |
| 137 | + $rootDir = __DIR__ . '/../../..'; |
| 138 | + $fileHelper = new FileHelper($rootDir); |
| 139 | + $rootDir = $fileHelper->normalizePath($rootDir, '/'); |
| 140 | + $containerFactory = new ContainerFactory($rootDir); |
| 141 | + $containerFactory->create( |
| 142 | + $this->tmpDir, |
| 143 | + [ |
| 144 | + $containerFactory->getConfigDirectory() . '/config.level8.neon', |
| 145 | + $additionalConfigFile, |
| 146 | + ], |
| 147 | + [], |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + private function countCompiledContainers(): int |
| 152 | + { |
| 153 | + $containers = glob($this->tmpDir . '/cache/nette.configurator/Container_*.php'); |
| 154 | + |
| 155 | + return $containers === false ? 0 : count($containers); |
| 156 | + } |
| 157 | + |
| 158 | + private function removeDirectory(string $directory): void |
| 159 | + { |
| 160 | + if (!is_dir($directory)) { |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + $entries = glob($directory . DIRECTORY_SEPARATOR . '*'); |
| 165 | + foreach ($entries === false ? [] : $entries as $entry) { |
| 166 | + if (is_dir($entry)) { |
| 167 | + $this->removeDirectory($entry); |
| 168 | + } else { |
| 169 | + @unlink($entry); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @rmdir($directory); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments