Skip to content

Commit 8cb4384

Browse files
committed
[Translator] Add functional test to ensure only enabled_locales are dumped
1 parent e1b43e7 commit 8cb4384

File tree

8 files changed

+101
-2
lines changed

8 files changed

+101
-2
lines changed

src/Translator/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"require-dev": {
3838
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
3939
"symfony/phpunit-bridge": "^5.2|^6.0|^7.0",
40-
"symfony/var-dumper": "^5.4|^6.0|^7.0"
40+
"symfony/var-dumper": "^5.4|^6.0|^7.0",
41+
"symfony/yaml": "^5.4|^6.0|^7.0"
4142
},
4243
"extra": {
4344
"thanks": {

src/Translator/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
66
colors="true"
7-
bootstrap="vendor/autoload.php"
7+
bootstrap="tests/bootstrap.php"
88
failOnRisky="true"
99
failOnWarning="true"
1010
>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
symfony_ux.great: Symfony UX is awesome
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
symfony_ux.great: Symfony UX es genial
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
symfony_ux.great: Symfony UX est génial
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Translator\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\Translator\Tests\Kernel\FrameworkAppKernel;
16+
17+
class DumpEnabledLocalesTest extends KernelTestCase
18+
{
19+
protected static function getKernelClass(): string
20+
{
21+
return FrameworkAppKernel::class;
22+
}
23+
24+
public function testShouldDumpOnlyEnabledLocales(): void
25+
{
26+
self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.en.yaml');
27+
self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.fr.yaml');
28+
self::assertFileExists(__DIR__.'/../Fixtures/translations/messages.es.yaml');
29+
30+
$enabledLocales = self::getContainer()->getParameter('kernel.enabled_locales');
31+
self::assertNotContains('es', $enabledLocales, 'The "es" locale should not be enabled in this test');
32+
33+
$translationsDumpDir = self::getContainer()->getParameter('kernel.project_dir').'/var/translations';
34+
self::assertDirectoryExists($translationsDumpDir);
35+
36+
self::assertStringEqualsFile(
37+
$translationsDumpDir.'/index.js',
38+
<<<JAVASCRIPT
39+
export const SYMFONY_UX_GREAT = {"id":"symfony_ux.great","translations":{"messages":{"en":"Symfony UX is awesome","fr":"Symfony UX est g\u00e9nial"}}};
40+
41+
JAVASCRIPT
42+
);
43+
self::assertStringEqualsFile(
44+
$translationsDumpDir.'/index.d.ts',
45+
<<<TYPESCRIPT
46+
import { Message, NoParametersType } from '@symfony/ux-translator';
47+
48+
export declare const SYMFONY_UX_GREAT: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
49+
50+
TYPESCRIPT
51+
);
52+
self::assertStringEqualsFile(
53+
$translationsDumpDir.'/configuration.js',
54+
<<<JAVASCRIPT
55+
export const localeFallbacks = {"en":null,"fr":"en"};
56+
57+
JAVASCRIPT
58+
);
59+
self::assertStringEqualsFile(
60+
$translationsDumpDir.'/configuration.d.ts',
61+
<<<TYPESCRIPT
62+
import { LocaleType } from '@symfony/ux-translator';
63+
64+
export declare const localeFallbacks: Record<LocaleType, LocaleType>;
65+
TYPESCRIPT
66+
);
67+
}
68+
}

src/Translator/tests/Kernel/FrameworkAppKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
3939
'test' => true,
4040
'translator' => [
4141
'fallbacks' => ['en'],
42+
'default_path' => '%kernel.project_dir%/tests/Fixtures/translations',
4243
],
44+
'enabled_locales' => ['en', 'fr'],
4345
'http_method_override' => false,
4446
]);
4547
});

src/Translator/tests/bootstrap.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\Console\Application;
13+
use Symfony\Component\Console\Input\StringInput;
14+
use Symfony\Component\Filesystem\Filesystem;
15+
use Symfony\UX\Translator\Tests\Kernel\FrameworkAppKernel;
16+
17+
require __DIR__.'/../vendor/autoload.php';
18+
19+
(new Filesystem())->remove(__DIR__.'/../var');
20+
21+
$kernel = new FrameworkAppKernel('test', true);
22+
$application = new Application($kernel);
23+
24+
// Trigger Symfony Translator and UX Translator cache warmers
25+
$application->run(new StringInput('cache:clear'));

0 commit comments

Comments
 (0)