From 8cb4384ba7ec4c5119c5d0bd457a1875bdb48464 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Sun, 27 Jul 2025 10:20:18 +0200 Subject: [PATCH] [Translator] Add functional test to ensure only enabled_locales are dumped --- src/Translator/composer.json | 3 +- src/Translator/phpunit.xml.dist | 2 +- .../Fixtures/translations/messages.en.yaml | 1 + .../Fixtures/translations/messages.es.yaml | 1 + .../Fixtures/translations/messages.fr.yaml | 1 + .../Functional/DumpEnabledLocalesTest.php | 68 +++++++++++++++++++ .../tests/Kernel/FrameworkAppKernel.php | 2 + src/Translator/tests/bootstrap.php | 25 +++++++ 8 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/Translator/tests/Fixtures/translations/messages.en.yaml create mode 100644 src/Translator/tests/Fixtures/translations/messages.es.yaml create mode 100644 src/Translator/tests/Fixtures/translations/messages.fr.yaml create mode 100644 src/Translator/tests/Functional/DumpEnabledLocalesTest.php create mode 100644 src/Translator/tests/bootstrap.php diff --git a/src/Translator/composer.json b/src/Translator/composer.json index 414769200a2..0ac6e58b869 100644 --- a/src/Translator/composer.json +++ b/src/Translator/composer.json @@ -37,7 +37,8 @@ "require-dev": { "symfony/framework-bundle": "^5.4|^6.0|^7.0", "symfony/phpunit-bridge": "^5.2|^6.0|^7.0", - "symfony/var-dumper": "^5.4|^6.0|^7.0" + "symfony/var-dumper": "^5.4|^6.0|^7.0", + "symfony/yaml": "^5.4|^6.0|^7.0" }, "extra": { "thanks": { diff --git a/src/Translator/phpunit.xml.dist b/src/Translator/phpunit.xml.dist index fa9fbb98fc6..b9b855e238e 100644 --- a/src/Translator/phpunit.xml.dist +++ b/src/Translator/phpunit.xml.dist @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd" colors="true" - bootstrap="vendor/autoload.php" + bootstrap="tests/bootstrap.php" failOnRisky="true" failOnWarning="true" > diff --git a/src/Translator/tests/Fixtures/translations/messages.en.yaml b/src/Translator/tests/Fixtures/translations/messages.en.yaml new file mode 100644 index 00000000000..d1e9ab003e1 --- /dev/null +++ b/src/Translator/tests/Fixtures/translations/messages.en.yaml @@ -0,0 +1 @@ +symfony_ux.great: Symfony UX is awesome diff --git a/src/Translator/tests/Fixtures/translations/messages.es.yaml b/src/Translator/tests/Fixtures/translations/messages.es.yaml new file mode 100644 index 00000000000..e3f9fa9464e --- /dev/null +++ b/src/Translator/tests/Fixtures/translations/messages.es.yaml @@ -0,0 +1 @@ +symfony_ux.great: Symfony UX es genial diff --git a/src/Translator/tests/Fixtures/translations/messages.fr.yaml b/src/Translator/tests/Fixtures/translations/messages.fr.yaml new file mode 100644 index 00000000000..8337a67a478 --- /dev/null +++ b/src/Translator/tests/Fixtures/translations/messages.fr.yaml @@ -0,0 +1 @@ +symfony_ux.great: Symfony UX est génial diff --git a/src/Translator/tests/Functional/DumpEnabledLocalesTest.php b/src/Translator/tests/Functional/DumpEnabledLocalesTest.php new file mode 100644 index 00000000000..35740bfe3f7 --- /dev/null +++ b/src/Translator/tests/Functional/DumpEnabledLocalesTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\Translator\Tests\Functional; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\UX\Translator\Tests\Kernel\FrameworkAppKernel; + +class DumpEnabledLocalesTest extends KernelTestCase +{ + protected static function getKernelClass(): string + { + return FrameworkAppKernel::class; + } + + public function testShouldDumpOnlyEnabledLocales(): void + { + self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.en.yaml'); + self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.fr.yaml'); + self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.es.yaml'); + + $enabledLocales = self::getContainer()->getParameter('kernel.enabled_locales'); + self::assertNotContains('es', $enabledLocales, 'The "es" locale should not be enabled in this test'); + + $translationsDumpDir = self::getContainer()->getParameter('kernel.project_dir').'/var/translations'; + self::assertDirectoryExists($translationsDumpDir); + + self::assertStringEqualsFile( + $translationsDumpDir.'/index.js', + <<; + + TYPESCRIPT + ); + self::assertStringEqualsFile( + $translationsDumpDir.'/configuration.js', + <<; + TYPESCRIPT + ); + } +} diff --git a/src/Translator/tests/Kernel/FrameworkAppKernel.php b/src/Translator/tests/Kernel/FrameworkAppKernel.php index 3e61e1ef9d8..7e6f034145a 100644 --- a/src/Translator/tests/Kernel/FrameworkAppKernel.php +++ b/src/Translator/tests/Kernel/FrameworkAppKernel.php @@ -39,7 +39,9 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'test' => true, 'translator' => [ 'fallbacks' => ['en'], + 'default_path' => '%kernel.project_dir%/tests/Fixtures/translations', ], + 'enabled_locales' => ['en', 'fr'], 'http_method_override' => false, ]); }); diff --git a/src/Translator/tests/bootstrap.php b/src/Translator/tests/bootstrap.php new file mode 100644 index 00000000000..1172565510f --- /dev/null +++ b/src/Translator/tests/bootstrap.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Input\StringInput; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\UX\Translator\Tests\Kernel\FrameworkAppKernel; + +require __DIR__.'/../vendor/autoload.php'; + +(new Filesystem())->remove(__DIR__.'/../var'); + +$kernel = new FrameworkAppKernel('test', true); +$application = new Application($kernel); + +// Trigger Symfony Translator and UX Translator cache warmers +$application->run(new StringInput('cache:clear'));