[Translator] Add functional test to ensure only enabled_locales are dumped

This commit is contained in:
Hugo Alliaume
2025-07-27 10:20:18 +02:00
parent db4efd01ab
commit 22ad68d3a3
8 changed files with 101 additions and 2 deletions

View File

@@ -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": {

View File

@@ -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"
>

View File

@@ -0,0 +1 @@
symfony_ux.great: Symfony UX is awesome

View File

@@ -0,0 +1 @@
symfony_ux.great: Symfony UX es genial

View File

@@ -0,0 +1 @@
symfony_ux.great: Symfony UX est génial

View File

@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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',
<<<JAVASCRIPT
export const SYMFONY_UX_GREAT = {"id":"symfony_ux.great","translations":{"messages":{"en":"Symfony UX is awesome","fr":"Symfony UX est g\u00e9nial"}}};
JAVASCRIPT
);
self::assertStringEqualsFile(
$translationsDumpDir.'/index.d.ts',
<<<TYPESCRIPT
import { Message, NoParametersType } from '@symfony/ux-translator';
export declare const SYMFONY_UX_GREAT: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
TYPESCRIPT
);
self::assertStringEqualsFile(
$translationsDumpDir.'/configuration.js',
<<<JAVASCRIPT
export const localeFallbacks = {"en":null,"fr":"en"};
JAVASCRIPT
);
self::assertStringEqualsFile(
$translationsDumpDir.'/configuration.d.ts',
<<<TYPESCRIPT
import { LocaleType } from '@symfony/ux-translator';
export declare const localeFallbacks: Record<LocaleType, LocaleType>;
TYPESCRIPT
);
}
}

View File

@@ -39,7 +39,9 @@ class FrameworkAppKernel extends Kernel
'test' => true,
'translator' => [
'fallbacks' => ['en'],
'default_path' => '%kernel.project_dir%/tests/Fixtures/translations',
],
'enabled_locales' => ['en', 'fr'],
'http_method_override' => false,
]);
});

25
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'));