Skip to content

Commit

Permalink
reapply tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MortenDHansen committed Mar 2, 2022
1 parent 23d57d0 commit 66b4123
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 201 deletions.
6 changes: 5 additions & 1 deletion src/DatabaseTranslationsTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

// The key was parsed, item place will be null if key is ungrouped
if (!isset($item)) {
$item = $group;
$item = $group == '*' ? $key : $group;
$group = '*';
}

Expand All @@ -75,6 +75,10 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

public function createMissingKey($group, $item, $locale)
{
// That's not a key
if ($item === '*') {
return;
}
DbTrans::createLanguageItem($group, $item, $locale);
$this->loaded = [];
$this->loadedFromDb = [];
Expand Down
339 changes: 139 additions & 200 deletions tests/Feature/DatabaseTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,206 +214,145 @@ public function commandRebuildsCache()
$this->assertEquals('schnitzel', \Cache::get(DbTrans::getCacheKey('salad', 'de'))['tomato']);
}

//
// /**
// * @test
// * @return void
// */
// public function itLoadsTranslationFromDatabaseInDifferentLocales()
// {
// $this->addTranslationFile(['salad' => 'blue']);
// $this->addTranslationFile(['salad' => 'schwarz'], 'de');
// DatabaseLangItem::factory()->create([
// 'group' => '*',
// 'key' => 'salad',
// 'value' => 'green',
// 'locale' => 'en'
// ]);
// DatabaseLangItem::factory()->create([
// 'group' => '*',
// 'key' => 'salad',
// 'value' => 'schwarz',
// 'locale' => 'de'
// ]);
//
// $this->assertEquals('green', __('salad'));
//
// app()->setLocale('de');
//
// $this->assertEquals('schwarz', __('salad'));
// }
//
// /**
// * @test
// * @return void
// */
// public function itLoadsTranslationFromFile()
// {
// // file says salad is blue
// $this->addTranslationFile(['salad' => 'blue']);
// $this->assertEquals('blue', __('salad'));
// }
//
// /**
// * @test
// * @return void
// */
// public function ifTranslationIsBothInDatabaseAndFilesDatabaseWins()
// {
// // file says salad is blue
// $this->addTranslationFile(['salad' => 'blue']);
// $this->assertEquals('blue', __('salad'));
//
// $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'locale' => 'en', 'key' => 'salad']);
// $langLine = DatabaseLangItem::where('key', 'salad')->first();
// $langLine->value = 'green';
// $langLine->save();
// $this->assertEquals('green', __('salad'));
// }
//
// /**
// * @test
// * @return void
// */
// public function itLoadsGroupedTranslationFromFile()
// {
// $this->assertEquals('The password is incorrect.', __('validation.current_password'));
// }
//
// /**
// * @test
// * @return void
// */
// public function ifGroupedTranslationIsBothInDatabaseAndFilesDatabaseWins()
// {
// DatabaseLangItem::factory()->create([
// 'key' => 'current_password',
// 'value' => 'Database has a different opinion!',
// 'group' => 'validation',
// 'locale' => 'en'
// ]);
// $this->assertDatabaseHas('database_lang_items', [
// 'group' => 'validation',
// 'locale' => 'en',
// 'key' => 'current_password'
// ]);
//
// $this->assertEquals('Database has a different opinion!', __('validation.current_password'));
// }
//
// /**
// * @test
// * @return void
// */
// public function itTakesTranslationFromFileIfDatabaseHasEmptyValue()
// {
// $this->addTranslationFile(['salad' => 'blue']);
//
// DatabaseLangItem::factory()->create([
// 'key' => 'salad',
// 'group' => '*',
// 'value' => null,
// 'locale' => 'en'
// ]);
//
// $this->assertEquals('blue', __('salad'));
// $this->assertDatabaseHas('database_lang_items', ['key' => 'salad', 'locale' => 'en']);
// }
//
// /**
// * @test
// * @return void
// */
// public function itCreatesMissingGroupedKeysButDoesNotReturnEmptyValues()
// {
// $translated = __('validation.current_password');
// $this->assertDatabaseHas('database_lang_items', ['group' => 'validation', 'key' => 'current_password']);
// }
//
// /**
// * @test
// * @return void
// */
// public function itCreatesMissingKeysButDoesNotReturnEmptyValues()
// {
// $translated = __('current_password');
// $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'current_password']);
// }
// /**
// * @test
// * @return void
// */
// public function itCreatesMissingKeysInDifferentLocales()
// {
// $translated = __('color');
// $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'color', 'locale' => 'en']);
//
// app()->setLocale('de');
//
// $translated = __('color');
// $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'color', 'locale' => 'de']);
//
// }
//
// /**
// * @test
// * @return void
// */
// public function loaderReturnsItemsWhichHasValue()
// {
// DatabaseLangItem::create([
// 'group' => '*',
// 'locale' => 'en',
// 'key' => 'somekey'
// ]);
//
// DatabaseLangItem::create([
// 'group' => '*',
// 'locale' => 'en',
// 'key' => 'someotherkey',
// 'value' => 'withvalue'
// ]);
//
// $loader = new DatabaseTranslationsLoader();
// $result = $loader->getDbTranslations('*', 'en');
// $this->assertCount(1, $result);
// $this->assertCount(2, $loader->dbTranslations['*']['en']);
// $this->assertArrayNotHasKey('somekey', $result);
// $this->assertArrayHasKey('someotherkey', $result);
// }
//
// /**
// * @test
// * @return void
// */
// public function itFiguresOutMixedCaseKeys()
// {
// $key = app('dbtrans')->getCacheKey('*', 'en');
// __('Im an annoying Key!');
// $this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'Im an annoying Key!', 'locale' => 'en']);
// $line = DatabaseLangItem::where('key', 'Im an annoying Key!')->first();
// $line->value = 'blabla';
// $line->save();
// cache()->forget($key);
// $this->assertEquals('blabla', __('Im an annoying Key!'));
// }
//
// /**
// * @test
// * @return void
// */
// public function itFiguresOutMixedCaseGroupedKeys()
// {
// __('metallica.Im an annoying Key!');
// $this->assertDatabaseHas('database_lang_items',
// ['group' => 'metallica', 'key' => 'Im an annoying Key!', 'locale' => 'en']);
// $line = DatabaseLangItem::where('key', 'Im an annoying Key!')->where('group', 'metallica')->first();
// $line->value = 'blabla';
// $line->save();
// cache()->forget(DbTrans::getCacheKey('metalica', 'en'));
//
// $this->assertEquals('blabla', __('metallica.Im an annoying Key!'));
// }
/**
* @test
* @return void
*/
public function itLoadsTranslationFromDatabaseInDifferentLocales()
{
$this->addTranslationFile(['salad' => 'blue']);
$this->addTranslationFile(['salad' => 'schwarz'], 'de');
DatabaseLangItem::factory()->create([
'group' => '*',
'key' => 'salad',
'value' => 'green',
'locale' => 'en'
]);
DatabaseLangItem::factory()->create([
'group' => '*',
'key' => 'salad',
'value' => 'schwarz',
'locale' => 'de'
]);

$this->assertEquals('green', __('salad'));

app()->setLocale('de');

$this->assertEquals('schwarz', __('salad'));
}

/**
* @test
* @return void
*/
public function itLoadsTranslationFromFile()
{
// file says salad is blue
$this->addTranslationFile(['salad' => 'blue']);
$this->assertEquals('blue', __('salad'));
}

/**
* @test
* @return void
*/
public function ifTranslationIsBothInDatabaseAndFilesDatabaseWins()
{
// file says salad is blue
$this->addTranslationFile(['salad' => 'blue']);
$this->assertEquals('blue', __('salad'));

$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'locale' => 'en', 'key' => 'salad']);
$langLine = DatabaseLangItem::where('key', 'salad')->first();
$langLine->value = 'green';
$langLine->save();
$this->assertEquals('green', __('salad'));
}

/**
* @test
* @return void
*/
public function itLoadsGroupedTranslationFromFile()
{
$this->assertEquals('The password is incorrect.', __('validation.current_password'));
}

/**
* @test
* @return void
*/
public function ifGroupedTranslationIsBothInDatabaseAndFilesDatabaseWins()
{
DatabaseLangItem::factory()->create([
'key' => 'current_password',
'value' => 'Database has a different opinion!',
'group' => 'validation',
'locale' => 'en'
]);
$this->assertDatabaseHas('database_lang_items', [
'group' => 'validation',
'locale' => 'en',
'key' => 'current_password'
]);

$this->assertEquals('Database has a different opinion!', __('validation.current_password'));
}

/**
* @test
* @return void
*/
public function itTakesTranslationFromFileIfDatabaseHasEmptyValue()
{
$this->addTranslationFile(['salad' => 'blue']);

DatabaseLangItem::factory()->create([
'key' => 'salad',
'group' => '*',
'value' => null,
'locale' => 'en'
]);

$this->assertEquals('blue', __('salad'));
$this->assertDatabaseHas('database_lang_items', ['key' => 'salad', 'locale' => 'en']);
}

/**
* @test
* @return void
*/
public function itCreatesMissingGroupedKeysButDoesNotReturnEmptyValues()
{
__('validation.current_password');
$this->assertDatabaseHas('database_lang_items', ['group' => 'validation', 'key' => 'current_password']);
}

/**
* @test
* @return void
*/
public function itCreatesMissingKeysButDoesNotReturnEmptyValues()
{
__('current_password');
$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'current_password']);
}

/**
* @test
* @return void
*/
public function itCreatesMissingKeysInDifferentLocales()
{
__('color');
$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'color', 'locale' => 'en']);

app()->setLocale('de');

__('color');
$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'key' => 'color', 'locale' => 'de']);
}


}

0 comments on commit 66b4123

Please sign in to comment.