Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jun 20, 2023
1 parent 5b300e8 commit 5222493
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/MessageSourceFallbackBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 'class' => yii1tech\i18n\fallback\MessageSourceFallbackBehavior::class,
* 'fallbackMessageSource' => [
* 'class' => CPhpMessageSource::class,
* 'forceTranslation' => true,
* ],
* ],
* ],
Expand Down
54 changes: 54 additions & 0 deletions tests/MessageSourceFallbackBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace yii1tech\i18n\fallback\test;

use CPhpMessageSource;
use Yii;
use yii1tech\i18n\fallback\MessageSourceFallbackBehavior;

class MessageSourceFallbackBehaviorTest extends TestCase
{
/**
* @param \yii1tech\i18n\fallback\MessageSourceFallbackBehavior|array $behavior
* @return void
*/
protected function attachMessageSourceBehavior($behavior): void
{
Yii::app()->getComponent('messages')->attachBehavior('fallbackBehavior', $behavior);
}

public function testFallbackToDifferentSource(): void
{
$this->attachMessageSourceBehavior([
'class' => MessageSourceFallbackBehavior::class,
'fallbackMessageSource' => [
'class' => CPhpMessageSource::class,
'basePath' => __DIR__ . '/messages/fallback',
'forceTranslation' => true,
],
]);

$this->assertSame('title-main-en_us', Yii::t('content', 'title'));
$this->assertSame('header-fallback-en_us', Yii::t('content', 'header'));
}

public function testFallbackToDifferentLanguage(): void
{
$messageSource = Yii::app()->getComponent('messages');

$this->attachMessageSourceBehavior([
'class' => MessageSourceFallbackBehavior::class,
'fallbackLanguage' => 'en_us',
'fallbackMessageSource' => [
'class' => get_class($messageSource),
'basePath' => $messageSource->basePath,
'forceTranslation' => true,
],
]);

Yii::app()->setLanguage('es');

$this->assertSame('title-main-es', Yii::t('content', 'title'));
$this->assertSame('description-main-en_us', Yii::t('content', 'description'));
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function mockApplication($config = [], $appClass = \CWebApplication::c
'messages' => [
'class' => CPhpMessageSource::class,
'basePath' => __DIR__ . '/messages/main',
'forceTranslation' => true,
],
],
], $config));
Expand Down
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// ensure we get report on all possible php errors
error_reporting(-1);

define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_ENABLE_EXCEPTION_HANDLER', false);
define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii/framework/yii.php');
6 changes: 6 additions & 0 deletions tests/messages/fallback/en_us/content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'title' => 'title-fallback-en_us',
'header' => 'header-fallback-en_us',
];
6 changes: 6 additions & 0 deletions tests/messages/main/en_us/content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'title' => 'title-main-en_us',
'description' => 'description-main-en_us',
];
6 changes: 6 additions & 0 deletions tests/messages/main/es/content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'title' => 'title-main-es',
'keywords' => 'keywords-main-es',
];

0 comments on commit 5222493

Please sign in to comment.