Files
famous-ly4-ev/app-modules/booking/tests/Feature/BookingResourceTest.php
T
Nyan Lin Paing d19a14a45e Complete Payment module: initiate/webhook/confirm/refund actions, Filament resources (T5.8-T5.13)
- InitiatePaymentAction + POST /api/v1/payments/{booking}/initiate
- Generic KBZ webhook (POST /api/v1/webhooks/{method}/{encryptBookingId?}),
  signature verification per KBZ's real callback spec, PaymentGatewayInterface::handleWebhook()
- ConfirmPaymentAction: idempotent confirmation, PaymentCompleted/PaymentFailed events,
  MarkBookingPaid listener
- RefundBookingAction + POST /api/v1/bookings/{booking}/refund: partial refunds validated
  against remaining balance, RefundProcessed event, MarkBookingRefunded listener
- CancelBookingAction now refunds confirmed bookings instead of rejecting; BookingPolicy::cancel
  requires process_refunds for confirmed bookings
- PaymentPlugin + PaymentResource/RefundResource Filament admin UI (read-only payments,
  refund list + Process action)
- Booking detail page now shows related payments
- Fix CACHE_STORE mismatch (database -> redis) so tagged route caching works
- CLAUDE.md: never run migrate:fresh/migrate:refresh/db:wipe on dev without being asked
2026-08-09 16:20:21 +07:00

246 lines
9.1 KiB
PHP

<?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\Payment\Enums\PaymentMethod;
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Models\Payment;
use Modules\Shared\Enums\VehicleOption;
use Spatie\Permission\Models\Permission;
beforeEach(function () {
foreach (['view_bookings', 'manage_bookings', 'process_refunds'] as $permission) {
Permission::findOrCreate($permission, 'web');
}
$this->admin = User::factory()->create()->givePermissionTo(['view_bookings', 'manage_bookings', 'process_refunds']);
$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 booking detail page shows its related payments', function () {
$booking = Booking::factory()->create();
Payment::factory()->completed()->create([
'booking_id' => $booking->id,
'gateway' => PaymentMethod::KbzMiniApp,
'gateway_transaction_id' => 'EVB-INFOLIST-TEST-1',
]);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertOk()
->assertSee('EVB-INFOLIST-TEST-1')
->assertSee(PaymentStatus::Completed->value);
});
test('the booking detail page shows a placeholder when there are no payments yet', function () {
$booking = Booking::factory()->create();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertOk()
->assertSee('No payment attempts yet.');
});
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');
});