Add Booking module: model, create/read/cancel API, Filament resource (T4.1-T4.7)
- Booking model with booking_vehicle_options line items (supports mixing vehicle options like front_seat + back_seat in one booking), price snapshot, status machine, and driver/car assignment fields - BookingService: front-seat max, disabled-option toggles, duplicate-option and whole-vehicle-exclusivity guards - CreateBookingAction, CancelBookingAction, AssignDriverAction - BookingRefGenerator: sequential EVB-AAAAA1-style refs via row lock - POST/GET/cancel booking API endpoints (Sanctum, ownership + admin policy) - BookingPlugin + Filament BookingResource: list, detail view, Cancel and Assign Driver actions (shared between table and detail page) - domain.md updated for multi-vehicle-option bookings (§2) and driver/ vehicle assignment (§5a)
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Livewire\Livewire;
|
||||
use Modules\Booking\Enums\BookingStatus;
|
||||
use Modules\Booking\Filament\Resources\Bookings\Pages\ListBookings;
|
||||
use Modules\Booking\Filament\Resources\Bookings\Pages\ViewBooking;
|
||||
use Modules\Booking\Models\Booking;
|
||||
use Modules\Booking\Models\BookingVehicleOption;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
beforeEach(function () {
|
||||
foreach (['view_bookings', 'manage_bookings'] as $permission) {
|
||||
Permission::findOrCreate($permission, 'web');
|
||||
}
|
||||
|
||||
$this->admin = User::factory()->create()->givePermissionTo(['view_bookings', 'manage_bookings']);
|
||||
$this->actingAs($this->admin);
|
||||
});
|
||||
|
||||
test('can list bookings', function () {
|
||||
$bookings = Booking::factory()->count(3)->create();
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertOk()
|
||||
->assertCanSeeTableRecords($bookings);
|
||||
});
|
||||
|
||||
test('can filter bookings by status', function () {
|
||||
$pending = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
|
||||
$confirmed = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->filterTable('status', BookingStatus::PendingPayment->value)
|
||||
->assertCanSeeTableRecords([$pending])
|
||||
->assertCanNotSeeTableRecords([$confirmed]);
|
||||
});
|
||||
|
||||
test('the cancel action is visible and enabled for a pending_payment booking', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionVisible('cancel', $booking)
|
||||
->assertTableActionEnabled('cancel', $booking);
|
||||
});
|
||||
|
||||
test('the cancel action is visible but disabled for a confirmed booking', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionVisible('cancel', $booking)
|
||||
->assertTableActionDisabled('cancel', $booking);
|
||||
});
|
||||
|
||||
test('the cancel action is hidden from a user without manage_bookings and not the owner', function () {
|
||||
$stranger = User::factory()->create();
|
||||
$this->actingAs($stranger);
|
||||
|
||||
$booking = Booking::factory()->create(['user_id' => User::factory()->create()->id, 'status' => BookingStatus::PendingPayment]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionHidden('cancel', $booking);
|
||||
});
|
||||
|
||||
test('calling the cancel action cancels a pending_payment booking', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->callTableAction('cancel', $booking)
|
||||
->assertNotified();
|
||||
|
||||
expect($booking->refresh()->status)->toBe(BookingStatus::Cancelled);
|
||||
});
|
||||
|
||||
test('the view action is visible for a user with view_bookings', function () {
|
||||
$booking = Booking::factory()->create();
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionVisible('view', $booking);
|
||||
});
|
||||
|
||||
test('the view action is hidden from a non-owner without view_bookings', function () {
|
||||
$stranger = User::factory()->create();
|
||||
$this->actingAs($stranger);
|
||||
|
||||
$booking = Booking::factory()->create(['user_id' => User::factory()->create()->id]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionHidden('view', $booking);
|
||||
});
|
||||
|
||||
test('can view a booking\'s detail page', function () {
|
||||
$booking = Booking::factory()->create([
|
||||
'passenger_name' => 'Jane Doe',
|
||||
'passenger_phone' => '+959123456789',
|
||||
]);
|
||||
|
||||
BookingVehicleOption::factory()->create([
|
||||
'booking_id' => $booking->id,
|
||||
'vehicle_option' => VehicleOption::BackSeat,
|
||||
'passenger_count' => 2,
|
||||
'unit_price' => 9000,
|
||||
'line_total' => 18000,
|
||||
]);
|
||||
|
||||
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
|
||||
->assertOk()
|
||||
->assertSee($booking->booking_ref)
|
||||
->assertSee('Jane Doe')
|
||||
->assertSee('+959123456789')
|
||||
->assertSee($booking->route->company->name)
|
||||
->assertSee($booking->pickup_address)
|
||||
->assertSee($booking->dropoff_address);
|
||||
});
|
||||
|
||||
test('the assign driver action is visible for a confirmed booking and hidden otherwise', function () {
|
||||
$confirmed = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
$pending = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionVisible('assignDriver', $confirmed)
|
||||
->assertTableActionHidden('assignDriver', $pending);
|
||||
});
|
||||
|
||||
test('the assign driver action is hidden from a user without manage_bookings', function () {
|
||||
$viewer = User::factory()->create()->givePermissionTo('view_bookings');
|
||||
$this->actingAs($viewer);
|
||||
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->assertTableActionHidden('assignDriver', $booking);
|
||||
});
|
||||
|
||||
test('calling the assign driver action sets driver and car details on a confirmed booking', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->callTableAction('assignDriver', $booking, data: [
|
||||
'driver_name' => 'U Aung',
|
||||
'driver_phone' => '+959111222333',
|
||||
'car_plate_number' => 'YGN-1234',
|
||||
'car_model' => 'Tesla Model Y',
|
||||
])
|
||||
->assertNotified();
|
||||
|
||||
$booking->refresh();
|
||||
|
||||
expect($booking->driver_name)->toBe('U Aung')
|
||||
->and($booking->driver_phone)->toBe('+959111222333')
|
||||
->and($booking->car_plate_number)->toBe('YGN-1234')
|
||||
->and($booking->car_model)->toBe('Tesla Model Y');
|
||||
});
|
||||
|
||||
test('the assign driver form requires driver_name, driver_phone, and car_plate_number', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->callTableAction('assignDriver', $booking, data: [
|
||||
'driver_name' => '',
|
||||
'driver_phone' => '',
|
||||
'car_plate_number' => '',
|
||||
])
|
||||
->assertHasTableActionErrors(['driver_name' => 'required', 'driver_phone' => 'required', 'car_plate_number' => 'required']);
|
||||
|
||||
expect($booking->refresh()->driver_name)->toBeNull();
|
||||
});
|
||||
|
||||
test('the assign driver form is pre-filled with the booking\'s existing driver/car details', function () {
|
||||
$booking = Booking::factory()->create([
|
||||
'status' => BookingStatus::Confirmed,
|
||||
'driver_name' => 'U Aung',
|
||||
'driver_phone' => '+959111222333',
|
||||
'car_plate_number' => 'YGN-1234',
|
||||
'car_model' => 'Tesla Model Y',
|
||||
]);
|
||||
|
||||
Livewire::test(ListBookings::class)
|
||||
->mountTableAction('assignDriver', $booking)
|
||||
->assertTableActionDataSet([
|
||||
'driver_name' => 'U Aung',
|
||||
'driver_phone' => '+959111222333',
|
||||
'car_plate_number' => 'YGN-1234',
|
||||
'car_model' => 'Tesla Model Y',
|
||||
]);
|
||||
});
|
||||
|
||||
test('the detail page also has assign driver and cancel actions, shared with the table', function () {
|
||||
$confirmed = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ViewBooking::class, ['record' => $confirmed->getRouteKey()])
|
||||
->assertActionVisible('assignDriver')
|
||||
->assertActionVisible('cancel')
|
||||
->assertActionDisabled('cancel');
|
||||
});
|
||||
|
||||
test('calling assign driver from the detail page sets driver and car details', function () {
|
||||
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
|
||||
|
||||
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
|
||||
->callAction('assignDriver', data: [
|
||||
'driver_name' => 'U Aung',
|
||||
'driver_phone' => '+959111222333',
|
||||
'car_plate_number' => 'YGN-1234',
|
||||
'car_model' => 'Tesla Model Y',
|
||||
])
|
||||
->assertNotified();
|
||||
|
||||
expect($booking->refresh()->driver_name)->toBe('U Aung');
|
||||
});
|
||||
|
||||
test('the detail page\'s assign driver action is hidden for a pending_payment booking', function () {
|
||||
$pending = Booking::factory()->create(['status' => BookingStatus::PendingPayment]);
|
||||
|
||||
Livewire::test(ViewBooking::class, ['record' => $pending->getRouteKey()])
|
||||
->assertActionHidden('assignDriver')
|
||||
->assertActionEnabled('cancel');
|
||||
});
|
||||
Reference in New Issue
Block a user