SilverFire - Dmitry Naumenko
9 years ago
11 changed files with 396 additions and 75 deletions
@ -0,0 +1,48 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
use yii\db\Migration; |
||||||
|
|
||||||
|
/** |
||||||
|
* Initializes i18n messages tables. |
||||||
|
* |
||||||
|
* @author Dmitry Naumenko <d.naumenko.a@gmail.com> |
||||||
|
* @since 2.0.7 |
||||||
|
*/ |
||||||
|
class m150207_210500_i18n_init extends Migration |
||||||
|
{ |
||||||
|
public function up() |
||||||
|
{ |
||||||
|
$tableOptions = null; |
||||||
|
if ($this->db->driverName === 'mysql') { |
||||||
|
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
||||||
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
||||||
|
} |
||||||
|
|
||||||
|
$this->createTable('source_message', [ |
||||||
|
'id' => $this->primaryKey(), |
||||||
|
'category' => $this->string(), |
||||||
|
'message' => $this->text(), |
||||||
|
], $tableOptions); |
||||||
|
|
||||||
|
$this->createTable('message', [ |
||||||
|
'id' => $this->integer(), |
||||||
|
'language' => $this->string(16), |
||||||
|
'translation' => $this->text(), |
||||||
|
], $tableOptions); |
||||||
|
|
||||||
|
$this->addPrimaryKey('pk_message_id_language', 'message', ['id', 'language']); |
||||||
|
$this->addForeignKey('fk_message_source_message', 'message', 'id', 'source_message', 'id', 'CASCADE', 'RESTRICT'); |
||||||
|
} |
||||||
|
|
||||||
|
public function down() |
||||||
|
{ |
||||||
|
$this->dropForeignKey('fk_message_source_message', 'message'); |
||||||
|
$this->dropTable('message'); |
||||||
|
$this->dropTable('source_message'); |
||||||
|
} |
||||||
|
} |
@ -1,7 +1,8 @@ |
|||||||
<?php |
<?php |
||||||
/** |
/** |
||||||
* |
* Messages are copy-pasted in \yiiunit\framework\i18n\DbMessageSourceTest::setUpBeforeClass() |
||||||
|
* Do not forget to update it in after changing this file! |
||||||
*/ |
*/ |
||||||
return [ |
return [ |
||||||
'The dog runs fast.' => 'Der Hund rennt schell.', |
'The dog runs fast.' => 'The dog runs fast (en-US).', |
||||||
]; |
]; |
||||||
|
@ -0,0 +1,156 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\framework\i18n; |
||||||
|
|
||||||
|
use Yii; |
||||||
|
use yii\base\Event; |
||||||
|
use yii\db\Connection; |
||||||
|
use yii\i18n\DbMessageSource; |
||||||
|
use yii\i18n\I18N; |
||||||
|
use yiiunit\framework\console\controllers\EchoMigrateController; |
||||||
|
|
||||||
|
/** |
||||||
|
* @group i18n |
||||||
|
* @group mysql |
||||||
|
* @author Dmitry Naumenko <d.naumenko.a@gmail.com> |
||||||
|
* @since 2.0.7 |
||||||
|
*/ |
||||||
|
class DbMessageSourceTest extends I18NTest |
||||||
|
{ |
||||||
|
protected static $database; |
||||||
|
protected static $driverName = 'mysql'; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var Connection |
||||||
|
*/ |
||||||
|
protected static $db; |
||||||
|
|
||||||
|
protected function setI18N() |
||||||
|
{ |
||||||
|
$this->i18n = new I18N([ |
||||||
|
'translations' => [ |
||||||
|
'test' => new DbMessageSource([ |
||||||
|
'db' => static::$db, |
||||||
|
]) |
||||||
|
] |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
protected static function runConsoleAction($route, $params = []) |
||||||
|
{ |
||||||
|
if (Yii::$app === null) { |
||||||
|
new \yii\console\Application([ |
||||||
|
'id' => 'Migrator', |
||||||
|
'basePath' => '@yiiunit', |
||||||
|
'controllerMap' => [ |
||||||
|
'migrate' => EchoMigrateController::className(), |
||||||
|
], |
||||||
|
'components' => [ |
||||||
|
'db' => static::getConnection(), |
||||||
|
], |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
ob_start(); |
||||||
|
$result = Yii::$app->runAction($route, $params); |
||||||
|
echo "Result is " . $result; |
||||||
|
if ($result !== \yii\console\Controller::EXIT_CODE_NORMAL) { |
||||||
|
ob_end_flush(); |
||||||
|
} else { |
||||||
|
ob_end_clean(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static function setUpBeforeClass() |
||||||
|
{ |
||||||
|
parent::setUpBeforeClass(); |
||||||
|
$databases = static::getParam('databases'); |
||||||
|
static::$database = $databases[static::$driverName]; |
||||||
|
$pdo_database = 'pdo_' . static::$driverName; |
||||||
|
|
||||||
|
if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) { |
||||||
|
static::markTestSkipped('pdo and ' . $pdo_database . ' extension are required.'); |
||||||
|
} |
||||||
|
|
||||||
|
static::runConsoleAction('migrate/up', ['migrationPath' => '@yii/i18n/migrations/', 'interactive' => false]); |
||||||
|
|
||||||
|
static::$db->createCommand()->batchInsert('source_message', ['id', 'category', 'message'], [ |
||||||
|
[1, 'test', 'Hello world!'], |
||||||
|
[2, 'test', 'The dog runs fast.'], |
||||||
|
[3, 'test', 'His speed is about {n} km/h.'], |
||||||
|
[4, 'test', 'His name is {name} and his speed is about {n, number} km/h.'], |
||||||
|
[5, 'test', 'There {n, plural, =0{no cats} =1{one cat} other{are # cats}} on lying on the sofa!'], |
||||||
|
])->execute(); |
||||||
|
|
||||||
|
static::$db->createCommand()->batchInsert('message', ['id', 'language', 'translation'], [ |
||||||
|
[1, 'de', 'Hallo Welt!'], |
||||||
|
[2, 'de-DE', 'Der Hund rennt schnell.'], |
||||||
|
[2, 'en-US', 'The dog runs fast (en-US).'], |
||||||
|
[2, 'ru', 'Собака бегает быстро.'], |
||||||
|
[3, 'de-DE', 'Seine Geschwindigkeit beträgt {n} km/h.'], |
||||||
|
[4, 'de-DE', 'Er heißt {name} und ist {n, number} km/h schnell.'], |
||||||
|
[5, 'ru', 'На диване {n, plural, =0{нет кошек} =1{лежит одна кошка} one{лежит # кошка} few{лежит # кошки} many{лежит # кошек} other{лежит # кошки}}!'], |
||||||
|
])->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
public static function tearDownAfterClass() |
||||||
|
{ |
||||||
|
static::runConsoleAction('migrate/down', ['migrationPath' => '@yii/i18n/migrations/', 'interactive' => false]); |
||||||
|
if (static::$db) { |
||||||
|
static::$db->close(); |
||||||
|
} |
||||||
|
Yii::$app = null; |
||||||
|
parent::tearDownAfterClass(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @throws \yii\base\InvalidParamException |
||||||
|
* @throws \yii\db\Exception |
||||||
|
* @throws \yii\base\InvalidConfigException |
||||||
|
* @return \yii\db\Connection |
||||||
|
*/ |
||||||
|
public static function getConnection() |
||||||
|
{ |
||||||
|
if (static::$db == null) { |
||||||
|
$db = new Connection; |
||||||
|
$db->dsn = static::$database['dsn']; |
||||||
|
if (isset(static::$database['username'])) { |
||||||
|
$db->username = static::$database['username']; |
||||||
|
$db->password = static::$database['password']; |
||||||
|
} |
||||||
|
if (isset(static::$database['attributes'])) { |
||||||
|
$db->attributes = static::$database['attributes']; |
||||||
|
} |
||||||
|
if (!$db->isActive) { |
||||||
|
$db->open(); |
||||||
|
} |
||||||
|
static::$db = $db; |
||||||
|
} |
||||||
|
return static::$db; |
||||||
|
} |
||||||
|
|
||||||
|
public function testMissingTranslationEvent() |
||||||
|
{ |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE')); |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
|
||||||
|
Event::on(DbMessageSource::className(), DbMessageSource::EVENT_MISSING_TRANSLATION, function ($event) {}); |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE')); |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
Event::off(DbMessageSource::className(), DbMessageSource::EVENT_MISSING_TRANSLATION); |
||||||
|
|
||||||
|
Event::on(DbMessageSource::className(), DbMessageSource::EVENT_MISSING_TRANSLATION, function ($event) { |
||||||
|
if ($event->message == 'New missing translation message.') { |
||||||
|
$event->translatedMessage = 'TRANSLATION MISSING HERE!'; |
||||||
|
} |
||||||
|
}); |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
$this->assertEquals('Another missing translation message.', $this->i18n->translate('test', 'Another missing translation message.', [], 'de-DE')); |
||||||
|
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE')); |
||||||
|
$this->assertEquals('TRANSLATION MISSING HERE!', $this->i18n->translate('test', 'New missing translation message.', [], 'de-DE')); |
||||||
|
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); |
||||||
|
Event::off(DbMessageSource::className(), DbMessageSource::EVENT_MISSING_TRANSLATION); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue