modify booking response data

This commit is contained in:
Nyan Lin Paing
2026-08-23 14:51:10 +07:00
parent fa908cdcaf
commit 41c9454334
5 changed files with 166 additions and 3 deletions
@@ -1,7 +1,10 @@
<?php
use App\Models\User;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Models\Booking;
use Modules\Payment\Enums\PaymentMethod;
use Modules\Payment\Models\Payment;
use Spatie\Permission\Models\Permission;
beforeEach(function () {
@@ -11,10 +14,27 @@ beforeEach(function () {
$this->token = $this->owner->createToken('test-token')->plainTextToken;
});
/**
* Index only ever shows bookings with a completed payment give the
* booking a completed Payment row so it's not silently excluded.
*/
function paidBooking(array $attributes = []): Booking
{
$booking = Booking::factory()->create($attributes);
Payment::factory()->completed()->create([
'booking_id' => $booking->id,
'gateway' => PaymentMethod::KbzMiniApp,
'amount' => $booking->price,
]);
return $booking;
}
test('index lists only the authenticated user\'s own bookings, latest first', function () {
$mine = Booking::factory()->create(['user_id' => $this->owner->id, 'created_at' => now()->subMinute()]);
$mineNewer = Booking::factory()->create(['user_id' => $this->owner->id]);
Booking::factory()->create(['user_id' => User::factory()->create()->id]);
$mine = paidBooking(['user_id' => $this->owner->id, 'created_at' => now()->subMinute()]);
$mineNewer = paidBooking(['user_id' => $this->owner->id]);
paidBooking(['user_id' => User::factory()->create()->id]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/bookings')
@@ -24,6 +44,115 @@ test('index lists only the authenticated user\'s own bookings, latest first', fu
->assertJsonPath('data.1.id', $mine->id);
});
test('index excludes bookings with no completed payment', function () {
// pending_payment, never paid.
Booking::factory()->create(['user_id' => $this->owner->id]);
// Has a payment attempt, but it failed — still not "complete".
$failedPayment = Booking::factory()->create(['user_id' => $this->owner->id]);
Payment::factory()->failed()->create(['booking_id' => $failedPayment->id, 'gateway' => PaymentMethod::KbzMiniApp]);
$paid = paidBooking(['user_id' => $this->owner->id]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/bookings')
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $paid->id);
});
test('index surfaces a round trip once, not as two separate rows, with a combined total_price', function () {
$outbound = paidBooking(['user_id' => $this->owner->id, 'price' => '9000.00']);
$return = Booking::factory()->create([
'user_id' => $this->owner->id,
'price' => '11000.00',
'is_return_leg' => true,
'linked_booking_id' => $outbound->id,
]);
$outbound->update(['linked_booking_id' => $return->id]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/bookings')
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $outbound->id)
->assertJsonPath('data.0.price', '9000.00')
->assertJsonPath('data.0.total_price', '20000.00')
->assertJsonPath('data.0.linked_booking.id', $return->id);
});
test('linked_booking carries the return leg\'s own driver/vehicle assignment, independent of the outbound leg\'s', function () {
$outbound = paidBooking([
'user_id' => $this->owner->id,
'status' => BookingStatus::Confirmed,
'driver_name' => 'U Aung',
'driver_phone' => '+959111222333',
'car_plate_number' => 'YGN-1234',
'car_model' => 'Tesla Model Y',
]);
$return = Booking::factory()->create([
'user_id' => $this->owner->id,
'is_return_leg' => true,
'linked_booking_id' => $outbound->id,
'status' => BookingStatus::Confirmed,
'driver_name' => 'Daw Hla',
'driver_phone' => '+959444555666',
'car_plate_number' => 'MDY-5678',
'car_model' => null,
]);
$outbound->update(['linked_booking_id' => $return->id]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/bookings/{$outbound->booking_ref}")
->assertSuccessful()
->assertJsonPath('data.driver_name', 'U Aung')
->assertJsonPath('data.car_plate_number', 'YGN-1234')
->assertJsonPath('data.linked_booking.driver_name', 'Daw Hla')
->assertJsonPath('data.linked_booking.driver_phone', '+959444555666')
->assertJsonPath('data.linked_booking.car_plate_number', 'MDY-5678')
->assertJsonPath('data.linked_booking.car_model', null);
});
test('total_price equals price for a plain one-way booking, on both index and show', function () {
$booking = paidBooking(['user_id' => $this->owner->id, 'price' => '15000.00']);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/bookings')
->assertJsonPath('data.0.total_price', '15000.00');
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/bookings/{$booking->booking_ref}")
->assertJsonPath('data.total_price', '15000.00');
});
test('show returns the combined total_price for a round trip', function () {
$outbound = Booking::factory()->create(['user_id' => $this->owner->id, 'price' => '9000.00']);
$return = Booking::factory()->create([
'user_id' => $this->owner->id,
'price' => '11000.00',
'is_return_leg' => true,
'linked_booking_id' => $outbound->id,
]);
$outbound->update(['linked_booking_id' => $return->id]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/bookings/{$outbound->booking_ref}")
->assertSuccessful()
->assertJsonPath('data.price', '9000.00')
->assertJsonPath('data.total_price', '20000.00');
});
test('index filters by booking_ref, partial and case-insensitive', function () {
$match = paidBooking(['user_id' => $this->owner->id, 'booking_ref' => 'EVB-FINDME1']);
paidBooking(['user_id' => $this->owner->id, 'booking_ref' => 'EVB-OTHER01']);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/bookings?booking_ref=findme')
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $match->id);
});
test('index rejects unauthenticated requests', function () {
$this->getJson('/api/v1/bookings')->assertUnauthorized();
});