Browse Source

test: add i18n service tests

Cat 1 year ago
parent
commit
e57b241d86

+ 1 - 1
composer.json

@@ -64,7 +64,7 @@
     "require-dev": {
         "dg/bypass-finals": "^1",
         "nunomaduro/phpinsights": "*",
-        "phpunit/phpunit": "^10"
+        "phpunit/phpunit": "^10|^11"
     },
     "scripts": {
         "update-dev-windows": [

+ 1 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "c26663c49583c6d735452b4e336378f9",
+    "content-hash": "fcf7b22dfa09f43972687553ed5890af",
     "packages": [
         {
             "name": "adbario/php-dot-notation",

+ 1 - 1
src/Controllers/User/OrderController.php

@@ -59,7 +59,7 @@ final class OrderController extends BaseController
         $product_id = $this->antiXss->xss_clean($request->getQueryParams()['product_id']) ?? null;
         $redir = Cookie::get('redir');
 
-        if ($redir !== null) {
+        if ($redir !== '') {
             Cookie::set(['redir' => ''], time() - 1);
         }
 

+ 10 - 6
src/Services/I18n.php

@@ -6,6 +6,8 @@ namespace App\Services;
 
 use Symfony\Component\Translation\Loader\PhpFileLoader;
 use Symfony\Component\Translation\Translator;
+use function basename;
+use function glob;
 use const BASE_PATH;
 
 final class I18n
@@ -20,12 +22,14 @@ final class I18n
 
     public static function getLocaleList(): array
     {
-        return [
-            'en_US',
-            'ja_JP',
-            'zh_CN',
-            'zh_TW',
-        ];
+        $locales = [];
+        $files = glob(BASE_PATH . '/resources/locale/*.php');
+
+        foreach ($files as $file) {
+            $locales[] = basename($file, '.php');
+        }
+
+        return $locales;
     }
 
     public static function getTranslator($lang = 'en_US'): Translator

+ 2 - 9
src/Utils/Cookie.php

@@ -8,17 +8,15 @@ final class Cookie
 {
     public static function set(array $arg, int $time): void
     {
-        $developmentMode = self::isDevelopmentMode();
         foreach ($arg as $key => $value) {
-            setcookie($key, $value, $time, path: '/', secure: ! $developmentMode, httponly: true);
+            setcookie($key, $value, $time, path: '/', secure: true, httponly: true);
         }
     }
 
     public static function setWithDomain(array $arg, int $time, string $domain): void
     {
-        $developmentMode = self::isDevelopmentMode();
         foreach ($arg as $key => $value) {
-            setcookie($key, $value, $time, path: '/', domain: $domain, secure: ! $developmentMode, httponly: true);
+            setcookie($key, $value, $time, path: '/', domain: $domain, secure: true, httponly: true);
         }
     }
 
@@ -26,9 +24,4 @@ final class Cookie
     {
         return $_COOKIE[$key] ?? '';
     }
-
-    private static function isDevelopmentMode(): bool
-    {
-        return ($_ENV['debug'] || str_contains($_ENV['baseUrl'], '.test')) && str_contains($_ENV['baseUrl'], 'http://');
-    }
 }

+ 59 - 0
tests/App/Services/I18nTest.php

@@ -0,0 +1,59 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Translation\Translator;
+
+require_once __DIR__ . '/../../../app/predefine.php';
+
+final class I18nTest extends TestCase
+{
+    /**
+     * @covers App\Services\I18n::trans
+     */
+    public function testTrans(): void
+    {
+        // exsisting locale
+        $key = 'lang_name';
+        $lang = 'en_US';
+        $expectedTranslation = 'English(Simplified)';
+
+        $translation = I18n::trans($key, $lang);
+
+        $this->assertSame($expectedTranslation, $translation);
+        // non-existing locale
+        $key = 'non_existent_key';
+
+        $translation = I18n::trans($key, $lang);
+
+        $this->assertSame($key, $translation);
+    }
+
+    /**
+     * @covers App\Services\I18n::getLocaleList
+     */
+    public function testGetLocaleList(): void
+    {
+        $expectedLocales = ['en_US', 'ja_JP', 'zh_CN', 'zh_TW'];
+
+        $locales = I18n::getLocaleList();
+
+        $this->assertSame($expectedLocales, $locales);
+    }
+
+    /**
+     * @covers App\Services\I18n::getTranslator
+     */
+    public function testGetTranslatorr(): void
+    {
+        $lang = 'en_US';
+
+        $translator = I18n::getTranslator($lang);
+
+        $this->assertInstanceOf(Translator::class, $translator);
+        $this->assertSame($lang, $translator->getLocale());
+    }
+}