Skip to content

Commit

Permalink
Add test for custom strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
crosa7 committed Oct 13, 2023
1 parent 071ed8a commit e35c1fd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/Plugins/Locale/TestCustomLocaleStrategyPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);


namespace LeafOmniglot\Plugins\Locale;


use LeafOmniglot\Plugins\Locale\LocaleStrategyPluginInterface;

class TestCustomLocaleStrategyPlugin implements LocaleStrategyPluginInterface
{
/**
* @param string $locale
*
* @return void
*/
public function setCurrentLocale(string $locale): void
{
// Not implemented
}

/**
* @return string|null
*/
public function getCurrentLocale(): ?string
{
return 'custom';
}
}
20 changes: 18 additions & 2 deletions tests/Feature/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
expect(omniglot()->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'
Expand All @@ -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 () {
Expand All @@ -44,5 +58,7 @@
});

afterEach(function () {
session_destroy();
if (session_status() === PHP_SESSION_ACTIVE) {
session_destroy();
}
});
3 changes: 3 additions & 0 deletions tests/data/locales/custom.locale.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome.page.title": "Hello Custom"
}

0 comments on commit e35c1fd

Please sign in to comment.