Skip to content

Commit 81e51c8

Browse files
committed
minor #2870 [Translator][Tests] Minor fixes in tests context (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [Translator][Tests] Minor fixes in tests context | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Cherry-picked from #2867, the following fixes only apply inside tests context, it does not have impact in real-life scenarios: 1. The static variable `$alreadyGenerated` now becomes a private property `alreadyGeneratedConstants. When tests were executed randomly in #2867, it was impossible to assert on generated translations files because constants names were kept in static cache even when the class `TranslationsDumper` was initialized for each test. 2. `self::$cacheDir` is a directory, so using `rmdir` is the best way to go instead of `unlink` Commits ------- 6e58d54 [Translator] Move already generated constants from static var to class property
2 parents 18baea3 + 6e58d54 commit 81e51c8

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/Translator/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/composer.lock
44
/phpunit.xml
55
/.phpunit.result.cache
6+
7+
/var

src/Translator/src/TranslationsDumper.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TranslationsDumper
3434
{
3535
private array $excludedDomains = [];
3636
private array $includedDomains = [];
37+
private array $alreadyGeneratedConstants = [];
3738

3839
public function __construct(
3940
private string $dumpDir,
@@ -184,16 +185,14 @@ private function getLocaleFallbacks(MessageCatalogueInterface ...$catalogues): a
184185

185186
private function generateConstantName(string $translationId): string
186187
{
187-
static $alreadyGenerated = [];
188-
189188
$translationId = s($translationId)->ascii()->snake()->upper()->replaceMatches('/^(\d)/', '_$1')->toString();
190189
$prefix = 0;
191190
do {
192191
$constantName = $translationId.($prefix > 0 ? '_'.$prefix : '');
193192
++$prefix;
194-
} while ($alreadyGenerated[$constantName] ?? false);
193+
} while ($this->alreadyGeneratedConstants[$constantName] ?? false);
195194

196-
$alreadyGenerated[$constantName] = true;
195+
$this->alreadyGeneratedConstants[$constantName] = true;
197196

198197
return $constantName;
199198
}

src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function setUpBeforeClass(): void
2828

2929
public static function tearDownAfterClass(): void
3030
{
31-
@unlink(self::$cacheDir);
31+
@rmdir(self::$cacheDir);
3232
}
3333

3434
public function test()

0 commit comments

Comments
 (0)