Skip to content

Commit

Permalink
Fix #79: Improve configs and add tests (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Oct 26, 2022
1 parent 8716af3 commit 8602823
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -26,6 +26,7 @@ jobs:
uses: yiisoft/actions/.github/workflows/phpunit.yml@master
with:
extensions: :intl
coverage: xdebug
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
Expand All @@ -35,6 +36,7 @@ jobs:
with:
phpunitConfig: phpunit-with-intl.xml
extensions: intl
coverage: xdebug
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
- Chg #81: Make category parameter in `TranslatorInterface::addCategorySources()` variadic, and remove
`TranslatorInterface::addCategorySource()` method (@vjik)
- New #82: Add parameter `$defaultCategory` to `Translator` constructor (@vjik)
- Chg #87: Fix package configuration, remove default category source, change default locale to `en_US` (@vjik)
- Chg #84: In `TranslatorInterface` rename method `withCategory()` to `withDefaultCategory()` (@vjik)

## 1.1.1 September 09, 2022
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -29,7 +29,8 @@
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18"
"vimeo/psalm": "^4.18",
"yiisoft/di": "^1.1"
},
"suggest": {
"ext-intl": "Allows using intl message formatter",
Expand Down
31 changes: 10 additions & 21 deletions config/common.php
Expand Up @@ -4,45 +4,34 @@

use Psr\EventDispatcher\EventDispatcherInterface;
use Yiisoft\Definitions\Reference;
use Yiisoft\Translator\MessageReaderInterface;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Translator\Translator;
use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\MessageFormatterInterface;
use Yiisoft\Translator\SimpleMessageFormatter;

/** @var array $params */

return [
// Configure application CategorySource
// ApplicationCategorySource::class => [
// 'class' => CategorySource::class,
// '__construct()' => [
// 'name' => $params['yiisoft/translator']['defaultCategory'],
// ],
// ],

'DefaultCategorySource' => static function (Psr\Container\ContainerInterface $container) use ($params) {
return new CategorySource(
$params['yiisoft/translator']['defaultCategory'],
$container->get(MessageReaderInterface::class),
$container->has(MessageFormatterInterface::class)
? $container->get(MessageFormatterInterface::class)
: new SimpleMessageFormatter(),
);
},
// 'translator.app' => static function (\Yiisoft\Translator\IntlMessageFormatter $formatter) use ($params) {
// return new \Yiisoft\Translator\CategorySource(
// $params['yiisoft/translator']['defaultCategory'],
// new \Yiisoft\Translator\Message\Php\MessageSource('/path/to/messages'),
// $formatter
// );
// },

TranslatorInterface::class => [
'class' => Translator::class,
'__construct()' => [
$params['yiisoft/translator']['locale'],
$params['yiisoft/translator']['fallbackLocale'],
Reference::to(EventDispatcherInterface::class),
$params['yiisoft/translator']['defaultCategory'],
Reference::optional(EventDispatcherInterface::class),
],
'addCategorySources()' => [
...$params['yiisoft/translator']['categorySources'],
],
'reset' => function () use ($params) {
/** @var Translator $this */
$this->setLocale($params['yiisoft/translator']['locale']);
},
],
Expand Down
11 changes: 4 additions & 7 deletions config/params.php
Expand Up @@ -2,18 +2,15 @@

declare(strict_types=1);

use Yiisoft\Definitions\Reference;

return [
'yiisoft/translator' => [
'locale' => 'en-US',
'locale' => 'en_US',
'fallbackLocale' => null,
'defaultCategory' => 'app',
'categorySources' => [
Reference::to('DefaultCategorySource'),
// You can add categories from your application and additional modules using `Reference::to` below
// Reference::to(ApplicationCategorySource::class),
// Reference::to(MyModuleCategorySource::class),
// You can add categories from your application and additional modules using `Reference::to()` below
// \Yiisoft\Definitions\Reference\Reference::to('translator.app'),
// \Yiisoft\Definitions\Reference\Reference::to('translator.moduleA'),
],
],
];
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -27,6 +27,7 @@
<coverage>
<include>
<directory>./src</directory>
<directory>./config</directory>
</include>
</coverage>
</phpunit>
58 changes: 58 additions & 0 deletions tests/ConfigTest.php
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Translator\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Di\StateResetter;
use Yiisoft\Translator\Translator;
use Yiisoft\Translator\TranslatorInterface;

final class ConfigTest extends TestCase
{
public function testBase(): void
{
$container = $this->createContainer();

$translator = $container->get(TranslatorInterface::class);

$this->assertInstanceOf(Translator::class, $translator);
}

public function testReset(): void
{
$container = $this->createContainer();

$translator = $container->get(TranslatorInterface::class);
$translator->setLocale('ru_RU');

$container->get(StateResetter::class)->reset();

$this->assertSame('en_US', $translator->getLocale());
}

private function createContainer(?array $params = null): Container
{
return new Container(
ContainerConfig::create()->withDefinitions(
$this->getCommonDefinitions($params)
)
);
}

private function getCommonDefinitions(?array $params = null): array
{
if ($params === null) {
$params = $this->getParams();
}
return require dirname(__DIR__) . '/config/common.php';
}

private function getParams(): array
{
return require dirname(__DIR__) . '/config/params.php';
}
}

0 comments on commit 8602823

Please sign in to comment.