Files
ppeasy_vg12/tests/Unit/Http/Resources/CountryResourceTest.php
T
2026-10-09 08:21:06 +02:00

55 lines
1.5 KiB
PHP

<?php
use App\Http\Resources\CountryResource;
use App\Models\Country;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->artisan('db:seed');
});
test('resource has expected keys', function () {
$country = Country::first();
$array = (new CountryResource($country))->toArray(new Request);
$this->assertSame([
'id', 'name', 'full_name', 'capital', 'citizenship', 'country_code',
'currency', 'currency_code', 'currency_sub_unit', 'currency_symbol',
'iso_3166_2', 'iso_3166_3', 'region_code', 'sub_region_code',
'eea', 'calling_code', 'flag',
], array_keys($array));
});
test('flag resolves to url when set', function () {
$country = Country::first();
$country->flag = 'UK.png';
$array = (new CountryResource($country))->toArray(new Request);
$this->assertStringContainsString('flags/UK.png', $array['flag']);
});
test('flag is null when missing', function () {
$country = Country::first();
$country->flag = null;
$array = (new CountryResource($country))->toArray(new Request);
$this->assertNull($array['flag']);
});
test('numeric casts', function () {
$country = Country::first();
$array = (new CountryResource($country))->toArray(new Request);
$this->assertIsInt($array['id']);
$this->assertIsInt($array['country_code']);
$this->assertIsInt($array['region_code']);
$this->assertIsBool($array['eea']);
});