mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-16 22:05:20 +00:00
Ports #639 to 3.x — the original targets the now feature-frozen 2.x line. Address::country_name now resolves the localized country name for the current app locale via Symfony\Component\Intl\Countries, falling back to the stored name on lookup failure. Adds symfony/intl + a unit test. Co-authored-by: Lukas Selch <selchlukas@icloud.com>
58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
use App\Models\Address;
|
|
use App\Models\Country;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
beforeEach(function () {
|
|
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
|
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
|
});
|
|
|
|
it('returns localized country name for German locale', function () {
|
|
App::setLocale('de');
|
|
|
|
$address = Address::factory()->create([
|
|
'country_id' => Country::where('code', 'DE')->first()->id,
|
|
]);
|
|
|
|
expect($address->country_name)->toBe('Deutschland');
|
|
});
|
|
|
|
it('returns localized country name for French locale', function () {
|
|
App::setLocale('fr');
|
|
|
|
$address = Address::factory()->create([
|
|
'country_id' => Country::where('code', 'DE')->first()->id,
|
|
]);
|
|
|
|
expect($address->country_name)->toBe('Allemagne');
|
|
});
|
|
|
|
it('returns null when address has no country', function () {
|
|
$address = Address::factory()->create([
|
|
'country_id' => null,
|
|
]);
|
|
|
|
expect($address->country_name)->toBeNull();
|
|
});
|
|
|
|
it('falls back to database name when intl lookup fails', function () {
|
|
App::setLocale('de');
|
|
|
|
// Create a country with a code that won't be found in ICU data
|
|
$country = new Country;
|
|
$country->timestamps = false;
|
|
$country->code = 'XX';
|
|
$country->name = 'Unknown Land';
|
|
$country->phonecode = 0;
|
|
$country->save();
|
|
|
|
$address = Address::factory()->create([
|
|
'country_id' => $country->id,
|
|
]);
|
|
|
|
expect($address->country_name)->toBe('Unknown Land');
|
|
});
|