Skip to content

Commit e57b241

Browse files
author
Cat
committed
test: add i18n service tests
1 parent 6e6d6f7 commit e57b241

File tree

6 files changed

+74
-18
lines changed

6 files changed

+74
-18
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"require-dev": {
6565
"dg/bypass-finals": "^1",
6666
"nunomaduro/phpinsights": "*",
67-
"phpunit/phpunit": "^10"
67+
"phpunit/phpunit": "^10|^11"
6868
},
6969
"scripts": {
7070
"update-dev-windows": [

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controllers/User/OrderController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function create(ServerRequest $request, Response $response, array $args):
5959
$product_id = $this->antiXss->xss_clean($request->getQueryParams()['product_id']) ?? null;
6060
$redir = Cookie::get('redir');
6161

62-
if ($redir !== null) {
62+
if ($redir !== '') {
6363
Cookie::set(['redir' => ''], time() - 1);
6464
}
6565

src/Services/I18n.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Symfony\Component\Translation\Loader\PhpFileLoader;
88
use Symfony\Component\Translation\Translator;
9+
use function basename;
10+
use function glob;
911
use const BASE_PATH;
1012

1113
final class I18n
@@ -20,12 +22,14 @@ public static function trans(string $key, string $lang = 'en_US'): string
2022

2123
public static function getLocaleList(): array
2224
{
23-
return [
24-
'en_US',
25-
'ja_JP',
26-
'zh_CN',
27-
'zh_TW',
28-
];
25+
$locales = [];
26+
$files = glob(BASE_PATH . '/resources/locale/*.php');
27+
28+
foreach ($files as $file) {
29+
$locales[] = basename($file, '.php');
30+
}
31+
32+
return $locales;
2933
}
3034

3135
public static function getTranslator($lang = 'en_US'): Translator

src/Utils/Cookie.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,20 @@ final class Cookie
88
{
99
public static function set(array $arg, int $time): void
1010
{
11-
$developmentMode = self::isDevelopmentMode();
1211
foreach ($arg as $key => $value) {
13-
setcookie($key, $value, $time, path: '/', secure: ! $developmentMode, httponly: true);
12+
setcookie($key, $value, $time, path: '/', secure: true, httponly: true);
1413
}
1514
}
1615

1716
public static function setWithDomain(array $arg, int $time, string $domain): void
1817
{
19-
$developmentMode = self::isDevelopmentMode();
2018
foreach ($arg as $key => $value) {
21-
setcookie($key, $value, $time, path: '/', domain: $domain, secure: ! $developmentMode, httponly: true);
19+
setcookie($key, $value, $time, path: '/', domain: $domain, secure: true, httponly: true);
2220
}
2321
}
2422

2523
public static function get(string $key): string
2624
{
2725
return $_COOKIE[$key] ?? '';
2826
}
29-
30-
private static function isDevelopmentMode(): bool
31-
{
32-
return ($_ENV['debug'] || str_contains($_ENV['baseUrl'], '.test')) && str_contains($_ENV['baseUrl'], 'http://');
33-
}
3427
}

tests/App/Services/I18nTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Services;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Translation\Translator;
9+
10+
require_once __DIR__ . '/../../../app/predefine.php';
11+
12+
final class I18nTest extends TestCase
13+
{
14+
/**
15+
* @covers App\Services\I18n::trans
16+
*/
17+
public function testTrans(): void
18+
{
19+
// exsisting locale
20+
$key = 'lang_name';
21+
$lang = 'en_US';
22+
$expectedTranslation = 'English(Simplified)';
23+
24+
$translation = I18n::trans($key, $lang);
25+
26+
$this->assertSame($expectedTranslation, $translation);
27+
// non-existing locale
28+
$key = 'non_existent_key';
29+
30+
$translation = I18n::trans($key, $lang);
31+
32+
$this->assertSame($key, $translation);
33+
}
34+
35+
/**
36+
* @covers App\Services\I18n::getLocaleList
37+
*/
38+
public function testGetLocaleList(): void
39+
{
40+
$expectedLocales = ['en_US', 'ja_JP', 'zh_CN', 'zh_TW'];
41+
42+
$locales = I18n::getLocaleList();
43+
44+
$this->assertSame($expectedLocales, $locales);
45+
}
46+
47+
/**
48+
* @covers App\Services\I18n::getTranslator
49+
*/
50+
public function testGetTranslatorr(): void
51+
{
52+
$lang = 'en_US';
53+
54+
$translator = I18n::getTranslator($lang);
55+
56+
$this->assertInstanceOf(Translator::class, $translator);
57+
$this->assertSame($lang, $translator->getLocale());
58+
}
59+
}

0 commit comments

Comments
 (0)