add sms sending feat

This commit is contained in:
Nyan Lin Paing
2026-08-23 20:44:52 +07:00
parent 41c9454334
commit da9cd9bbe0
17 changed files with 492 additions and 9 deletions
@@ -149,6 +149,15 @@ test('the assign driver action is visible for a confirmed booking and hidden oth
->assertTableActionHidden('assignDriver', $pending);
});
test('the assign driver action is hidden once the travel date has passed', function () {
$past = Booking::factory()->create(['status' => BookingStatus::Confirmed, 'travel_date' => today()->subDay()]);
$today = Booking::factory()->create(['status' => BookingStatus::Confirmed, 'travel_date' => today()]);
Livewire::test(ListBookings::class)
->assertTableActionHidden('assignDriver', $past)
->assertTableActionVisible('assignDriver', $today);
});
test('the assign driver action is hidden from a user without manage_bookings', function () {
$viewer = User::factory()->create()->givePermissionTo('view_bookings');
$this->actingAs($viewer);
@@ -0,0 +1,75 @@
<?php
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Events\DriverAssigned;
use Modules\Booking\Listeners\SendDriverAssignedSms;
use Modules\Booking\Models\Booking;
use Modules\Catalog\Models\Destination;
use Modules\Routing\Models\EvRoute;
use Modules\Shared\Sms\SmsService;
beforeEach(function () {
config([
'app.name' => 'FamousLY4 EV',
'app.support_phone' => '+959123456789',
'app.support_email' => 'support@famousLY4.test',
]);
});
test('a first driver assignment texts the passenger with an "assigned" message including the route', function () {
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'passenger_phone' => '+959999888777',
'driver_name' => 'U Aung',
'driver_phone' => '+959111222333',
'car_plate_number' => 'YGN-1234',
'car_model' => 'Tesla Model Y',
'ev_route_id' => EvRoute::factory()->create([
'from_destination_id' => Destination::factory()->create(['name' => 'Yangon'])->id,
'to_destination_id' => Destination::factory()->create(['name' => 'Mandalay'])->id,
])->id,
]);
$sms = Mockery::mock(SmsService::class);
$sms->shouldReceive('send')
->once()
->with('+959999888777', Mockery::on(fn (string $message) => str_contains($message, 'assigned')
&& str_contains($message, 'U Aung')
&& str_contains($message, 'YGN-1234')
&& str_contains($message, 'Yangon - Mandalay')
&& str_contains($message, config('app.name'))
&& str_contains($message, config('app.support_phone'))
&& str_contains($message, config('app.support_email'))
&& str_contains($message, 'ယာဉ်မောင်း')
&& str_contains($message, 'အကူအညီလိုအပ်ပါက ဆက်သွယ်ရန်')));
(new SendDriverAssignedSms($sms))->handle(new DriverAssigned($booking, isFirstAssignment: true));
});
test('a driver reassignment texts the passenger with an "updated" message including the route', function () {
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'passenger_phone' => '+959999888777',
'driver_name' => 'Daw Hla',
'driver_phone' => '+959444555666',
'car_plate_number' => 'YGN-5678',
'ev_route_id' => EvRoute::factory()->create([
'from_destination_id' => Destination::factory()->create(['name' => 'Yangon'])->id,
'to_destination_id' => Destination::factory()->create(['name' => 'Mandalay'])->id,
])->id,
]);
$sms = Mockery::mock(SmsService::class);
$sms->shouldReceive('send')
->once()
->with('+959999888777', Mockery::on(fn (string $message) => str_contains($message, 'updated')
&& str_contains($message, 'Daw Hla')
&& str_contains($message, 'Yangon - Mandalay')
&& str_contains($message, config('app.name'))
&& str_contains($message, config('app.support_phone'))
&& str_contains($message, config('app.support_email'))
&& str_contains($message, 'ယာဉ်မောင်း')
&& str_contains($message, 'အကူအညီလိုအပ်ပါက ဆက်သွယ်ရန်')));
(new SendDriverAssignedSms($sms))->handle(new DriverAssigned($booking, isFirstAssignment: false));
});
@@ -1,8 +1,10 @@
<?php
use Illuminate\Support\Facades\Event;
use Modules\Booking\Actions\AssignDriverAction;
use Modules\Booking\Data\AssignDriverData;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Events\DriverAssigned;
use Modules\Booking\Exceptions\DriverAssignmentNotAllowedException;
use Modules\Booking\Models\Booking;
@@ -23,6 +25,57 @@ test('it assigns driver and car details to a confirmed booking', function () {
->and($booking->refresh()->driver_name)->toBe('U Aung');
});
test('it dispatches DriverAssigned with isFirstAssignment true for a booking with no prior driver', function () {
Event::fake([DriverAssigned::class]);
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
(new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
));
Event::assertDispatched(DriverAssigned::class, fn (DriverAssigned $event) => $event->booking->is($booking) && $event->isFirstAssignment === true);
});
test('it dispatches DriverAssigned with isFirstAssignment false when reassigning', function () {
Event::fake([DriverAssigned::class]);
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'driver_name' => 'U Aung',
'driver_phone' => '+959111222333',
'car_plate_number' => 'YGN-1234',
]);
(new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'Daw Hla',
driverPhone: '+959444555666',
carPlateNumber: 'YGN-5678',
));
Event::assertDispatched(DriverAssigned::class, fn (DriverAssigned $event) => $event->isFirstAssignment === false);
});
test('it does not dispatch DriverAssigned again when resubmitted with identical driver/car details', function () {
Event::fake([DriverAssigned::class]);
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'driver_name' => 'U Aung',
'driver_phone' => '+959111222333',
'car_plate_number' => 'YGN-1234',
'car_model' => 'Tesla Model Y',
]);
(new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
carModel: 'Tesla Model Y',
));
Event::assertNotDispatched(DriverAssigned::class);
});
test('car_model is optional', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Confirmed]);
@@ -47,6 +100,36 @@ test('it guards against assigning a driver to a pending_payment booking', functi
expect($booking->refresh()->driver_name)->toBeNull();
});
test('it guards against assigning a driver when the travel date has already passed', function () {
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'travel_date' => today()->subDay(),
]);
expect(fn () => (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
)))->toThrow(DriverAssignmentNotAllowedException::class);
expect($booking->refresh()->driver_name)->toBeNull();
});
test('it allows assigning a driver when the travel date is today', function () {
$booking = Booking::factory()->create([
'status' => BookingStatus::Confirmed,
'travel_date' => today(),
]);
$updated = (new AssignDriverAction)->handle($booking, new AssignDriverData(
driverName: 'U Aung',
driverPhone: '+959111222333',
carPlateNumber: 'YGN-1234',
));
expect($updated->driver_name)->toBe('U Aung');
});
test('it guards against assigning a driver to a cancelled booking', function () {
$booking = Booking::factory()->create(['status' => BookingStatus::Cancelled]);