Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Commit 65197e7

Browse files
nsauminidimsav
authored andcommitted
Translate on different connection (#366)
* Able to translate on different connection * Test setup for multiple connection * Added test case Added test case for translation on different connection * Apply fixes from StyleCI #366 * Apply fixes from StyleCI #366 * Review changes Added more assertions to make sure the record is not in default db connection * Added check before set * Apply fixes from StyleCI #366
1 parent df33880 commit 65197e7

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/Translatable/Translatable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ protected function saveTranslations()
427427
$saved = true;
428428
foreach ($this->translations as $translation) {
429429
if ($saved && $this->isTranslationDirty($translation)) {
430+
if (! empty($connectionName = $this->getConnectionName())) {
431+
$translation->setConnection($connectionName);
432+
}
433+
430434
$translation->setAttribute($this->getRelationKey(), $this->getKey());
431435
$saved = $translation->save();
432436
}

tests/TestsBase.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,65 @@
66
class TestsBase extends TestCase
77
{
88
protected $queriesCount;
9+
protected static $db2Setup = false;
910

1011
const DB_NAME = 'translatable_test';
12+
const DB_NAME2 = 'translatable_test2';
1113
const DB_USERNAME = 'homestead';
1214
const DB_PASSWORD = 'secret';
1315

1416
public function setUp()
1517
{
16-
$this->makeSureDatabaseExists();
18+
$this->makeSureDatabaseExists(static::DB_NAME);
19+
20+
if (! static::$db2Setup) {
21+
$this->makeSureDatabaseExists(static::DB_NAME2);
22+
}
1723

1824
parent::setUp();
1925

20-
$this->makeSureSchemaIsCreated();
26+
if (! static::$db2Setup) {
27+
$this->makeSureSchemaIsCreated('mysql2');
28+
$this->truncateAllTablesButMigrations(static::DB_NAME2);
29+
static::$db2Setup = true;
30+
}
31+
32+
$this->makeSureSchemaIsCreated('mysql');
2133
$this->enableQueryCounter();
2234
$this->refreshSeedData();
2335
}
2436

2537
private function refreshSeedData()
2638
{
27-
$this->truncateAllTablesButMigrations();
39+
$this->truncateAllTablesButMigrations(static::DB_NAME);
2840
$seeder = new AddFreshSeeds;
2941
$seeder->run();
3042
}
3143

32-
private function makeSureDatabaseExists()
44+
private function makeSureDatabaseExists($dbName)
3345
{
34-
$this->runQuery('CREATE DATABASE IF NOT EXISTS '.static::DB_NAME);
46+
$this->runQuery('CREATE DATABASE IF NOT EXISTS '.$dbName);
3547
}
3648

37-
private function makeSureSchemaIsCreated()
49+
private function makeSureSchemaIsCreated($dbConnectionName)
3850
{
3951
$migrationsPath = '../../../../tests/migrations';
4052
$artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');
4153

4254
// Makes sure the migrations table is created
4355
$artisan->call('migrate', [
44-
'--database' => 'mysql',
56+
'--database' => $dbConnectionName,
4557
'--path' => $migrationsPath,
4658
]);
4759
}
4860

49-
private function truncateAllTablesButMigrations()
61+
private function truncateAllTablesButMigrations($dbName)
5062
{
5163
$db = $this->app->make('db');
5264
$db->statement('SET FOREIGN_KEY_CHECKS=0;');
5365

5466
foreach ($tables = $db->select('SHOW TABLES') as $table) {
55-
$table = $table->{'Tables_in_'.static::DB_NAME};
67+
$table = $table->{'Tables_in_'.$dbName};
5668
if ($table != 'migrations') {
5769
$db->table($table)->truncate();
5870
}
@@ -101,6 +113,16 @@ protected function getEnvironmentSetUp($app)
101113
'collation' => 'utf8_unicode_ci',
102114
'strict' => false,
103115
]);
116+
$app['config']->set('database.connections.mysql2', [
117+
'driver' => 'mysql',
118+
'host' => '127.0.0.1',
119+
'database' => static::DB_NAME2,
120+
'username' => static::DB_USERNAME,
121+
'password' => static::DB_PASSWORD,
122+
'charset' => 'utf8',
123+
'collation' => 'utf8_unicode_ci',
124+
'strict' => false,
125+
]);
104126
$app['config']->set('translatable.locales', ['el', 'en', 'fr', 'de', 'id']);
105127
}
106128

tests/TranslatableTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,32 @@ public function test_it_uses_fallback_locale_if_default_is_empty()
634634
$this->app->setLocale('fr');
635635
$this->assertEquals('Tunisia', $country->name);
636636
}
637+
638+
public function test_translation_with_multiconnection()
639+
{
640+
// Add country & translation in second db
641+
$country = new Country();
642+
$country->setConnection('mysql2');
643+
$country->code = 'sg';
644+
$country->{'name:sg'} = 'Singapore';
645+
$this->assertTrue($country->save());
646+
647+
$countryId = $country->id;
648+
649+
// Verify added country & translation in second db
650+
$country = new Country();
651+
$country->setConnection('mysql2');
652+
$sgCountry = $country->find($countryId);
653+
$this->assertEquals('Singapore', $sgCountry->translate('sg')->name);
654+
655+
// Verify added country not in default db
656+
$country = new Country();
657+
$sgCountry = $country::where('code', 'sg')->get();
658+
$this->assertEmpty($sgCountry);
659+
660+
// Verify added translation not in default db
661+
$country = new Country();
662+
$sgCountry = $country->find($countryId);
663+
$this->assertEmpty($sgCountry->translate('sg'));
664+
}
637665
}

0 commit comments

Comments
 (0)