Add Routing module: EvRoute, pricing, time slots, read API, caching (T3.1-T3.7)
Implements Phase 3 in full: EvRoute model with company/destination relations and a from/to-must-differ guard; the ev_route_time_slots pivot; RoutePricing with a per-route is_blocked flag (every route auto-manages exactly one price row per vehicle option via a fixed-row Filament repeater on both create and edit); PricingService::quote() with a shared VehicleOption enum; RoutingPlugin with the EvRouteResource admin UI (activation gated on non-blocked options being priced); the /api/v1/routes read API (list/show/pricing/time-slots, AI-agent-friendly nested shape); and Redis-tag-based response caching invalidated via EvRoute/RoutePricing observers.
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Livewire\Livewire;
|
||||
use Modules\Catalog\Models\Destination;
|
||||
use Modules\Catalog\Models\EvCompany;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\CreateEvRoute;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\EditEvRoute;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\ListEvRoutes;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
use function Pest\Laravel\assertDatabaseHas;
|
||||
|
||||
beforeEach(function () {
|
||||
Permission::findOrCreate('manage_routes', 'web');
|
||||
|
||||
$this->admin = User::factory()->create()->givePermissionTo('manage_routes');
|
||||
$this->actingAs($this->admin);
|
||||
});
|
||||
|
||||
function pricingPayload(array $overrides = []): array
|
||||
{
|
||||
return collect(VehicleOption::cases())
|
||||
->map(fn (VehicleOption $option) => array_merge([
|
||||
'vehicle_option' => $option->value,
|
||||
'price' => 0,
|
||||
'is_blocked' => false,
|
||||
], $overrides[$option->value] ?? []))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
test('can list ev routes', function () {
|
||||
$routes = EvRoute::factory()->count(3)->create();
|
||||
|
||||
Livewire::test(ListEvRoutes::class)
|
||||
->assertOk()
|
||||
->assertCanSeeTableRecords($routes);
|
||||
});
|
||||
|
||||
test('list shows each vehicle option price stacked, and blocked options instead of a price', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::FrontSeat, 'price' => 12000]);
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::BackSeat, 'price' => 9000]);
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::WholeVehicle, 'is_blocked' => true]);
|
||||
|
||||
Livewire::test(ListEvRoutes::class)
|
||||
->assertOk()
|
||||
->assertTableColumnStateSet('pricing', ['Back Seat: 9,000', 'Front Seat: 12,000', 'Whole Vehicle: Blocked'], $route);
|
||||
});
|
||||
|
||||
test('creating a route also creates all three vehicle option pricing rows, defaulting to 0', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$from = Destination::factory()->create();
|
||||
$to = Destination::factory()->create();
|
||||
|
||||
Livewire::test(CreateEvRoute::class)
|
||||
->fillForm([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $from->id,
|
||||
'to_destination_id' => $to->id,
|
||||
'is_round_trip' => false,
|
||||
'is_active' => false,
|
||||
'pricing' => pricingPayload(),
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||
|
||||
expect($route->pricing)->toHaveCount(3);
|
||||
|
||||
foreach (VehicleOption::cases() as $option) {
|
||||
assertDatabaseHas(RoutePricing::class, [
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => $option->value,
|
||||
'price' => '0.00',
|
||||
'is_blocked' => false,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test('can price all three vehicle options directly on the create form', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$from = Destination::factory()->create();
|
||||
$to = Destination::factory()->create();
|
||||
|
||||
Livewire::test(CreateEvRoute::class)
|
||||
->fillForm([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $from->id,
|
||||
'to_destination_id' => $to->id,
|
||||
'is_active' => true,
|
||||
'pricing' => pricingPayload([
|
||||
'front_seat' => ['price' => 12000],
|
||||
'back_seat' => ['price' => 9000],
|
||||
'whole_vehicle' => ['price' => 30000],
|
||||
]),
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||
|
||||
expect($route->is_active)->toBeTrue();
|
||||
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'front_seat', 'price' => 12000]);
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'back_seat', 'price' => 9000]);
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'whole_vehicle', 'price' => 30000]);
|
||||
});
|
||||
|
||||
test('cannot create a route with the same from and to destination', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$destination = Destination::factory()->create();
|
||||
|
||||
Livewire::test(CreateEvRoute::class)
|
||||
->fillForm([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $destination->id,
|
||||
'to_destination_id' => $destination->id,
|
||||
'pricing' => pricingPayload(),
|
||||
])
|
||||
->call('create')
|
||||
->assertHasFormErrors(['to_destination_id' => 'different'])
|
||||
->assertNotNotified()
|
||||
->assertNoRedirect();
|
||||
|
||||
$this->assertDatabaseCount(EvRoute::class, 0);
|
||||
});
|
||||
|
||||
test('cannot create a route as active while any non-blocked vehicle option is priced at 0', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$from = Destination::factory()->create();
|
||||
$to = Destination::factory()->create();
|
||||
|
||||
Livewire::test(CreateEvRoute::class)
|
||||
->fillForm([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $from->id,
|
||||
'to_destination_id' => $to->id,
|
||||
'is_active' => true,
|
||||
'pricing' => pricingPayload([
|
||||
'front_seat' => ['price' => 12000],
|
||||
// back_seat and whole_vehicle left at 0.
|
||||
]),
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified();
|
||||
|
||||
$this->assertDatabaseCount(EvRoute::class, 0);
|
||||
});
|
||||
|
||||
test('a blocked vehicle option does not need a price to create an active route', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$from = Destination::factory()->create();
|
||||
$to = Destination::factory()->create();
|
||||
|
||||
Livewire::test(CreateEvRoute::class)
|
||||
->fillForm([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $from->id,
|
||||
'to_destination_id' => $to->id,
|
||||
'is_active' => true,
|
||||
'pricing' => pricingPayload([
|
||||
'front_seat' => ['price' => 12000],
|
||||
'back_seat' => ['price' => 9000],
|
||||
'whole_vehicle' => ['is_blocked' => true],
|
||||
]),
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||
|
||||
expect($route->is_active)->toBeTrue();
|
||||
});
|
||||
|
||||
test('can update pricing and toggle blocked from the edit page', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
foreach (VehicleOption::cases() as $option) {
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => $option,
|
||||
'price' => 5000,
|
||||
]);
|
||||
}
|
||||
|
||||
Livewire::test(EditEvRoute::class, ['record' => $route->getRouteKey()])
|
||||
->assertOk()
|
||||
->fillForm([
|
||||
'pricing' => pricingPayload([
|
||||
'front_seat' => ['price' => 15000],
|
||||
'back_seat' => ['price' => 8000],
|
||||
'whole_vehicle' => ['is_blocked' => true],
|
||||
]),
|
||||
])
|
||||
->call('save')
|
||||
->assertNotified();
|
||||
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'front_seat', 'price' => 15000]);
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'back_seat', 'price' => 8000]);
|
||||
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'whole_vehicle', 'is_blocked' => true]);
|
||||
|
||||
expect($route->pricing()->count())->toBe(3);
|
||||
});
|
||||
|
||||
test('cannot activate a route from the edit page while any non-blocked vehicle option is priced at 0', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => false]);
|
||||
foreach (VehicleOption::cases() as $option) {
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => $option]);
|
||||
}
|
||||
|
||||
Livewire::test(EditEvRoute::class, ['record' => $route->getRouteKey()])
|
||||
->assertOk()
|
||||
->fillForm([
|
||||
'is_active' => true,
|
||||
'pricing' => pricingPayload([
|
||||
'front_seat' => ['price' => 12000],
|
||||
// back_seat and whole_vehicle left at 0.
|
||||
]),
|
||||
])
|
||||
->call('save')
|
||||
->assertNotified();
|
||||
|
||||
expect($route->fresh()->is_active)->toBeFalse();
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\QueryException;
|
||||
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||
use Modules\Catalog\Models\Destination;
|
||||
use Modules\Catalog\Models\EvCompany;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
|
||||
test('an ev route belongs to a company and two destinations', function () {
|
||||
$company = EvCompany::factory()->create();
|
||||
$from = Destination::factory()->create();
|
||||
$to = Destination::factory()->create();
|
||||
|
||||
$route = EvRoute::factory()->create([
|
||||
'ev_company_id' => $company->id,
|
||||
'from_destination_id' => $from->id,
|
||||
'to_destination_id' => $to->id,
|
||||
]);
|
||||
|
||||
expect($route->company)->toBeInstanceOf(EvCompany::class)
|
||||
->and($route->company->is($company))->toBeTrue()
|
||||
->and($route->fromDestination)->toBeInstanceOf(Destination::class)
|
||||
->and($route->fromDestination->is($from))->toBeTrue()
|
||||
->and($route->toDestination)->toBeInstanceOf(Destination::class)
|
||||
->and($route->toDestination->is($to))->toBeTrue();
|
||||
});
|
||||
|
||||
test('is_round_trip and is_active cast to boolean', function () {
|
||||
$route = EvRoute::factory()->create([
|
||||
'is_round_trip' => 1,
|
||||
'is_active' => 0,
|
||||
]);
|
||||
|
||||
expect($route->is_round_trip)->toBeTrue()
|
||||
->and($route->is_active)->toBeFalse();
|
||||
});
|
||||
|
||||
test('a route can be attached to time slots via the pivot, carrying its own is_active flag', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
$morningSlot = DepartureTimeSlot::factory()->create();
|
||||
$eveningSlot = DepartureTimeSlot::factory()->create();
|
||||
|
||||
$route->timeSlots()->attach([
|
||||
$morningSlot->id => ['is_active' => true],
|
||||
$eveningSlot->id => ['is_active' => false],
|
||||
]);
|
||||
|
||||
$route->refresh();
|
||||
|
||||
expect($route->timeSlots)->toHaveCount(2);
|
||||
|
||||
$attachedMorning = $route->timeSlots->firstWhere('id', $morningSlot->id);
|
||||
$attachedEvening = $route->timeSlots->firstWhere('id', $eveningSlot->id);
|
||||
|
||||
expect($attachedMorning->pivot->is_active)->toBeTrue()
|
||||
->and($attachedEvening->pivot->is_active)->toBeFalse();
|
||||
|
||||
expect($morningSlot->routes)->toHaveCount(1)
|
||||
->and($morningSlot->routes->first()->is($route))->toBeTrue();
|
||||
});
|
||||
|
||||
test('a route cannot have the same from and to destination', function () {
|
||||
$destination = Destination::factory()->create();
|
||||
|
||||
expect(fn () => EvRoute::factory()->create([
|
||||
'from_destination_id' => $destination->id,
|
||||
'to_destination_id' => $destination->id,
|
||||
]))->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
test('a route cannot be attached to the same time slot twice', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
$slot = DepartureTimeSlot::factory()->create();
|
||||
|
||||
$route->timeSlots()->attach($slot->id, ['is_active' => true]);
|
||||
|
||||
expect(fn () => $route->timeSlots()->attach($slot->id, ['is_active' => true]))
|
||||
->toThrow(QueryException::class);
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\QueryException;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
test('route pricing belongs to a route and casts its vehicle option, price and blocked flag', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
$pricing = RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::FrontSeat,
|
||||
'price' => 12000,
|
||||
'is_blocked' => true,
|
||||
]);
|
||||
|
||||
expect($pricing->route)->toBeInstanceOf(EvRoute::class)
|
||||
->and($pricing->route->is($route))->toBeTrue()
|
||||
->and($pricing->vehicle_option)->toBe(VehicleOption::FrontSeat)
|
||||
->and($pricing->price)->toEqual('12000.00')
|
||||
->and($pricing->is_blocked)->toBeTrue();
|
||||
|
||||
expect($route->pricing()->first()->is($pricing))->toBeTrue();
|
||||
});
|
||||
|
||||
test('a route can only have one price per vehicle option', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||
]);
|
||||
|
||||
expect(fn () => RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||
]))->toThrow(QueryException::class);
|
||||
});
|
||||
|
||||
test('a route can have all three vehicle options priced', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
foreach (VehicleOption::cases() as $option) {
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => $option,
|
||||
]);
|
||||
}
|
||||
|
||||
expect($route->pricing()->count())->toBe(3);
|
||||
});
|
||||
|
||||
test('pricing defaults to unblocked', function () {
|
||||
$pricing = RoutePricing::factory()->create();
|
||||
|
||||
expect($pricing->is_blocked)->toBeFalse();
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
|
||||
});
|
||||
|
||||
test('the pricing endpoint serves a cached response until invalidated', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
$pricing = RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::FrontSeat,
|
||||
'price' => 10000,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||
->assertJsonFragment(['price' => '10000.00']);
|
||||
|
||||
// Bypass Eloquent (no 'saved' event) so a stale cached response proves caching is active.
|
||||
RoutePricing::query()->where('id', $pricing->id)->update(['price' => 99999]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||
->assertJsonFragment(['price' => '10000.00']);
|
||||
|
||||
// A genuine Eloquent save fires the observer and flushes the 'routes' tag.
|
||||
$pricing->refresh()->save();
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||
->assertJsonFragment(['price' => '99999.00']);
|
||||
});
|
||||
|
||||
test('saving an ev route invalidates the routes cache tag', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertJsonCount(1, 'data');
|
||||
|
||||
// Bypass Eloquent so the change wouldn't be visible without invalidation.
|
||||
EvRoute::query()->where('id', $route->id)->update(['is_active' => false]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertJsonCount(1, 'data');
|
||||
|
||||
$route->refresh()->save();
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertJsonCount(0, 'data');
|
||||
});
|
||||
|
||||
test('deleting an ev route invalidates the routes cache tag', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertJsonCount(1, 'data');
|
||||
|
||||
$route->delete();
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertJsonCount(0, 'data');
|
||||
});
|
||||
|
||||
test('flushing the routes cache tag does not affect other cached data', function () {
|
||||
Cache::put('unrelated-key', 'still here', now()->addMinutes(5));
|
||||
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
$route->update(['is_round_trip' => true]);
|
||||
|
||||
expect(Cache::get('unrelated-key'))->toBe('still here');
|
||||
});
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||
use Modules\Catalog\Models\Destination;
|
||||
use Modules\Catalog\Models\EvCompany;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
|
||||
});
|
||||
|
||||
test('lists active routes with nested company, destinations, time slots and pricing', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
EvRoute::factory()->create(['is_active' => false]);
|
||||
|
||||
$slot = DepartureTimeSlot::factory()->create();
|
||||
$route->timeSlots()->attach($slot->id, ['is_active' => true]);
|
||||
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::FrontSeat,
|
||||
'price' => 12000,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes')
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $route->id)
|
||||
->assertJsonPath('data.0.company.id', $route->ev_company_id)
|
||||
->assertJsonPath('data.0.from_destination.id', $route->from_destination_id)
|
||||
->assertJsonPath('data.0.to_destination.id', $route->to_destination_id)
|
||||
->assertJsonPath('data.0.time_slots.0.id', $slot->id)
|
||||
->assertJsonPath('data.0.time_slots.0.is_active', true)
|
||||
->assertJsonPath('data.0.pricing.0.vehicle_option', 'front_seat')
|
||||
->assertJsonPath('data.0.pricing.0.price', '12000.00');
|
||||
});
|
||||
|
||||
test('filters routes by company, from, and to', function () {
|
||||
$companyA = EvCompany::factory()->create();
|
||||
$companyB = EvCompany::factory()->create();
|
||||
$yangon = Destination::factory()->create();
|
||||
$mandalay = Destination::factory()->create();
|
||||
$bagan = Destination::factory()->create();
|
||||
|
||||
$matching = EvRoute::factory()->create([
|
||||
'ev_company_id' => $companyA->id,
|
||||
'from_destination_id' => $yangon->id,
|
||||
'to_destination_id' => $mandalay->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
EvRoute::factory()->create([
|
||||
'ev_company_id' => $companyB->id,
|
||||
'from_destination_id' => $yangon->id,
|
||||
'to_destination_id' => $mandalay->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
EvRoute::factory()->create([
|
||||
'ev_company_id' => $companyA->id,
|
||||
'from_destination_id' => $yangon->id,
|
||||
'to_destination_id' => $bagan->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson('/api/v1/routes?'.http_build_query([
|
||||
'company' => $companyA->id,
|
||||
'from' => $yangon->id,
|
||||
'to' => $mandalay->id,
|
||||
]))
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $matching->id);
|
||||
});
|
||||
|
||||
test('shows a single active route', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}")
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('data.id', $route->id);
|
||||
});
|
||||
|
||||
test('an inactive route is not found via show, pricing, or time-slots', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => false]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}")
|
||||
->assertNotFound();
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||
->assertNotFound();
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/time-slots")
|
||||
->assertNotFound();
|
||||
});
|
||||
|
||||
test('lists a route\'s pricing including blocked options', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::FrontSeat, 'price' => 12000]);
|
||||
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::WholeVehicle, 'is_blocked' => true]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(2, 'data')
|
||||
->assertJsonFragment(['vehicle_option' => 'front_seat', 'price' => '12000.00', 'is_blocked' => false])
|
||||
->assertJsonFragment(['vehicle_option' => 'whole_vehicle', 'is_blocked' => true]);
|
||||
});
|
||||
|
||||
test('lists a route\'s time slots with the pivot active flag', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
$active = DepartureTimeSlot::factory()->create();
|
||||
$inactive = DepartureTimeSlot::factory()->create();
|
||||
|
||||
$route->timeSlots()->attach([
|
||||
$active->id => ['is_active' => true],
|
||||
$inactive->id => ['is_active' => false],
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||
->getJson("/api/v1/routes/{$route->id}/time-slots")
|
||||
->assertSuccessful()
|
||||
->assertJsonCount(2, 'data')
|
||||
->assertJsonFragment(['id' => $active->id, 'is_active' => true])
|
||||
->assertJsonFragment(['id' => $inactive->id, 'is_active' => false]);
|
||||
});
|
||||
|
||||
test('routes endpoints reject unauthenticated requests', function () {
|
||||
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->getJson('/api/v1/routes')->assertUnauthorized();
|
||||
$this->getJson("/api/v1/routes/{$route->id}")->assertUnauthorized();
|
||||
$this->getJson("/api/v1/routes/{$route->id}/pricing")->assertUnauthorized();
|
||||
$this->getJson("/api/v1/routes/{$route->id}/time-slots")->assertUnauthorized();
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
use Modules\Routing\Data\PriceQuoteData;
|
||||
use Modules\Routing\Exceptions\RoutePricingNotFoundException;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
use Modules\Routing\Services\PricingService;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
test('quote returns a price for each priced vehicle option on a route', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::FrontSeat,
|
||||
'price' => 12000,
|
||||
]);
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::BackSeat,
|
||||
'price' => 9000,
|
||||
]);
|
||||
RoutePricing::factory()->create([
|
||||
'ev_route_id' => $route->id,
|
||||
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||
'price' => 30000,
|
||||
]);
|
||||
|
||||
$service = new PricingService;
|
||||
|
||||
$frontSeatQuote = $service->quote($route, VehicleOption::FrontSeat);
|
||||
$backSeatQuote = $service->quote($route, VehicleOption::BackSeat);
|
||||
$wholeVehicleQuote = $service->quote($route, VehicleOption::WholeVehicle);
|
||||
|
||||
expect($frontSeatQuote)->toBeInstanceOf(PriceQuoteData::class)
|
||||
->and($frontSeatQuote->evRouteId)->toBe($route->id)
|
||||
->and($frontSeatQuote->vehicleOption)->toBe(VehicleOption::FrontSeat)
|
||||
->and($frontSeatQuote->price)->toEqual('12000.00');
|
||||
|
||||
expect($backSeatQuote->price)->toEqual('9000.00');
|
||||
expect($wholeVehicleQuote->price)->toEqual('30000.00');
|
||||
});
|
||||
|
||||
test('quote throws when the route has no pricing for the requested vehicle option', function () {
|
||||
$route = EvRoute::factory()->create();
|
||||
|
||||
$service = new PricingService;
|
||||
|
||||
expect(fn () => $service->quote($route, VehicleOption::WholeVehicle))
|
||||
->toThrow(RoutePricingNotFoundException::class);
|
||||
});
|
||||
Reference in New Issue
Block a user