From e35c1fd1cd521ea41675e72f1e459a5e95c1e7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosa?= Date: Fri, 13 Oct 2023 15:22:34 +0100 Subject: [PATCH] Add test for custom strategy --- .../Locale/TestCustomLocaleStrategyPlugin.php | 30 +++++++++++++++++++ tests/Feature/LocaleTest.php | 20 +++++++++++-- tests/data/locales/custom.locale.json | 3 ++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/Plugins/Locale/TestCustomLocaleStrategyPlugin.php create mode 100644 tests/data/locales/custom.locale.json diff --git a/src/Plugins/Locale/TestCustomLocaleStrategyPlugin.php b/src/Plugins/Locale/TestCustomLocaleStrategyPlugin.php new file mode 100644 index 0000000..179ab2c --- /dev/null +++ b/src/Plugins/Locale/TestCustomLocaleStrategyPlugin.php @@ -0,0 +1,30 @@ +getCurrentLocale())->toBe('pt_PT'); }); +it('tests that getCurrentLocale get locale using custom strategy', function () { + omniglot()->init([ + 'TRANSLATION_FILES_LOCATION' => './tests/data/locales', + 'LOCALE_STRATEGY' => 'custom', + 'CUSTOM_LOCALE_STRATEGY_CLASS_NAME' => \LeafOmniglot\Plugins\Locale\TestCustomLocaleStrategyPlugin::class + ]); + + expect(omniglot()->getCurrentLocale())->toBe('custom'); + expect(tl('welcome.page.title'))->toBe('Hello Custom'); + + // Here because of caching issues with tests + \LeafOmniglot\Reader\ConfigReader::config('LOCALE_STRATEGY', 'session'); +}); + it('tests that setCurrentLocale throws exception if locale on found in files', function () { omniglot()->init([ 'TRANSLATION_FILES_LOCATION' => './tests/data/locales' @@ -28,7 +42,7 @@ 'TRANSLATION_FILES_LOCATION' => './tests/data/locales' ]); - expect(omniglot()->getAvailableLocales())->toBe(['en_US', 'pt_BR', 'pt_PT']); + expect(omniglot()->getAvailableLocales())->toBe(['custom', 'en_US', 'pt_BR', 'pt_PT']); }); it('tests that getDefaultLocale returns configured default locale', function () { @@ -44,5 +58,7 @@ }); afterEach(function () { - session_destroy(); + if (session_status() === PHP_SESSION_ACTIVE) { + session_destroy(); + } }); diff --git a/tests/data/locales/custom.locale.json b/tests/data/locales/custom.locale.json new file mode 100644 index 0000000..61ae533 --- /dev/null +++ b/tests/data/locales/custom.locale.json @@ -0,0 +1,3 @@ +{ + "welcome.page.title": "Hello Custom" +}