mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-06-17 04:30:31 +00:00
🧪 Add API tests and update controllers for city and venue details
- ✏️ Added feature tests for cities and venues, including pagination limits and `withDetails` parameter handling. - ✏️ Updated `CityController` to support `withDetails`, returning country code and flag URL while lifting pagination limits. - ✏️ Updated `VenueController` to support `withDetails`, lifting pagination limits and enriching venue responses with city details.
This commit is contained in:
@@ -30,10 +30,13 @@ class CityController extends Controller
|
||||
*/
|
||||
#[QueryParameter(name: 'search', description: 'Teilstring-Suche im Namen der Stadt.', required: false, type: 'string')]
|
||||
#[QueryParameter(name: 'selected', description: 'Lädt gezielt die angegebenen IDs.', required: false, type: 'array')]
|
||||
#[QueryParameter(name: 'withDetails', description: 'Presence-Flag: liefert zusätzlich Country-Code und Flaggen-URL und hebt die Begrenzung auf 10 Einträge auf.', required: false, type: 'string')]
|
||||
public function index(Request $request)
|
||||
{
|
||||
$withDetails = $request->exists('withDetails');
|
||||
|
||||
return City::query()
|
||||
->with(['country:id,name'])
|
||||
->with([$withDetails ? 'country:id,name,code' : 'country:id,name'])
|
||||
->select('id', 'name', 'country_id')
|
||||
->orderBy('name')
|
||||
->when(
|
||||
@@ -44,9 +47,16 @@ class CityController extends Controller
|
||||
->when(
|
||||
$request->exists('selected'),
|
||||
fn (Builder $query) => $query->whereIn('id', $this->numericIds($request)),
|
||||
fn (Builder $query) => $query->limit(10)
|
||||
fn (Builder $query) => $withDetails ? $query : $query->limit(10)
|
||||
)
|
||||
->get();
|
||||
->get()
|
||||
->map(function (City $city) use ($withDetails) {
|
||||
if ($withDetails) {
|
||||
$city->flag = asset('vendor/blade-country-flags/4x3-'.$city->country->code.'.svg');
|
||||
}
|
||||
|
||||
return $city;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ class VenueController extends Controller
|
||||
*/
|
||||
#[QueryParameter(name: 'search', description: 'Teilstring-Suche im Namen des Veranstaltungsortes.', required: false, type: 'string')]
|
||||
#[QueryParameter(name: 'selected', description: 'Lädt gezielt die angegebenen Veranstaltungsort-IDs (umgeht die Begrenzung auf 10 Einträge).', required: false, type: 'array')]
|
||||
#[QueryParameter(name: 'withDetails', description: 'Presence-Flag: hebt die Begrenzung auf 10 Einträge auf.', required: false, type: 'string')]
|
||||
public function index(Request $request)
|
||||
{
|
||||
return Venue::query()
|
||||
@@ -46,7 +47,7 @@ class VenueController extends Controller
|
||||
->when(
|
||||
$request->exists('selected'),
|
||||
fn (Builder $query) => $query->whereIn('id', $this->numericIds($request)),
|
||||
fn (Builder $query) => $query->limit(10)
|
||||
fn (Builder $query) => $request->exists('withDetails') ? $query : $query->limit(10)
|
||||
)
|
||||
->get()
|
||||
->map(function (Venue $venue) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\Country;
|
||||
use App\Models\Venue;
|
||||
|
||||
it('limits GET /api/cities to 10 entries without withDetails', function () {
|
||||
City::factory()->count(11)->create(['country_id' => Country::factory()->create()->id]);
|
||||
|
||||
$this->getJson('/api/cities')
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(10);
|
||||
});
|
||||
|
||||
it('returns all cities with country code and flag on GET /api/cities?withDetails', function () {
|
||||
$cities = City::factory()->count(11)->create(['country_id' => Country::factory()->create()->id]);
|
||||
|
||||
$response = $this->getJson('/api/cities?withDetails')
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(11);
|
||||
|
||||
$first = collect($response->json())->firstWhere('id', $cities->first()->id);
|
||||
|
||||
expect($first)
|
||||
->toHaveKeys(['id', 'name', 'country_id', 'country', 'flag'])
|
||||
->and($first['country'])->toHaveKeys(['id', 'name', 'code'])
|
||||
->and($first['flag'])->toContain('4x3-'.$cities->first()->country->code.'.svg');
|
||||
});
|
||||
|
||||
it('limits GET /api/venues to 10 entries without withDetails', function () {
|
||||
Venue::factory()->count(11)->create(['city_id' => City::factory()->create()->id]);
|
||||
|
||||
$this->getJson('/api/venues')
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(10);
|
||||
});
|
||||
|
||||
it('returns all venues on GET /api/venues?withDetails', function () {
|
||||
$venues = Venue::factory()->count(11)->create(['city_id' => City::factory()->create()->id]);
|
||||
|
||||
$response = $this->getJson('/api/venues?withDetails')
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(11);
|
||||
|
||||
$first = collect($response->json())->firstWhere('id', $venues->first()->id);
|
||||
|
||||
expect($first)
|
||||
->toHaveKeys(['id', 'name', 'city_id', 'flag', 'description', 'city'])
|
||||
->and($first['city']['country'])->toHaveKeys(['id', 'name', 'code']);
|
||||
});
|
||||
Reference in New Issue
Block a user