diff --git a/src/app/Http/Requests/ReservationStoreRequest.php b/src/app/Http/Requests/ReservationStoreRequest.php index ed426c1..8498e9a 100644 --- a/src/app/Http/Requests/ReservationStoreRequest.php +++ b/src/app/Http/Requests/ReservationStoreRequest.php @@ -2,9 +2,9 @@ namespace App\Http\Requests; +use App\Models\Reservation; use App\Rules\DateRangeNotOverlap; use Illuminate\Foundation\Http\FormRequest; -use App\Models\Reservation; class ReservationStoreRequest extends FormRequest { diff --git a/src/app/Http/Resources/ReservationResource.php b/src/app/Http/Resources/ReservationResource.php index 845bd2a..7613482 100644 --- a/src/app/Http/Resources/ReservationResource.php +++ b/src/app/Http/Resources/ReservationResource.php @@ -4,7 +4,6 @@ use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; -use App\Http\Resources\BookResource; class ReservationResource extends JsonResource { diff --git a/src/app/Models/Reservation.php b/src/app/Models/Reservation.php index 81ead37..a33f635 100644 --- a/src/app/Models/Reservation.php +++ b/src/app/Models/Reservation.php @@ -31,7 +31,7 @@ public function book(): BelongsTo public function overlapped($data): bool { - return $this->where(function ($query) use($data) { + return $this->where(function ($query) use ($data) { $query->where('date_to', '>=', $data['date_from']) ->where('date_from', '<=', $data['date_to']) ->where('book_id', $data['book_id']); diff --git a/src/app/Rules/DateRangeNotOverlap.php b/src/app/Rules/DateRangeNotOverlap.php index 9839c80..e73409d 100644 --- a/src/app/Rules/DateRangeNotOverlap.php +++ b/src/app/Rules/DateRangeNotOverlap.php @@ -15,9 +15,11 @@ class DateRangeNotOverlap implements DataAwareRule, ValidationRule * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ protected $data = []; + protected $reservationModel; - public function __construct($reservationModel = Reservation::class) { + public function __construct($reservationModel = Reservation::class) + { $this->reservationModel = $reservationModel; } diff --git a/src/tests/Feature/RequestsControllerTest.php b/src/tests/Feature/RequestsControllerTest.php index 64b9458..c80c8a5 100644 --- a/src/tests/Feature/RequestsControllerTest.php +++ b/src/tests/Feature/RequestsControllerTest.php @@ -1,27 +1,22 @@ Http::response([ - 'requester' => 'biblio x', - 'date_from' => '2024-03-01 12:00:00', - 'date_to' => '2024-03-10 12:00:00', - 'book' => [ - 'id' => 1, - 'title' => 'book title', - 'author' => 'book author', - 'published_at' => '2020-01-01', - 'isbn' => '1234567890123', - ] - ], 200) + '*/api/reservations' => Http::response([ + 'requester' => 'biblio x', + 'date_from' => '2024-03-01 12:00:00', + 'date_to' => '2024-03-10 12:00:00', + 'book' => [ + 'id' => 1, + 'title' => 'book title', + 'author' => 'book author', + 'published_at' => '2020-01-01', + 'isbn' => '1234567890123', + ], + ], 200), ]); $jsonResponse = $this->getJson('/api/requests/reservations/valid') @@ -36,24 +31,24 @@ 'author' => 'book author', 'published_at' => '2020-01-01', 'isbn' => '1234567890123', - ] + ], ]); }); test('A not valid request from a library manager receive a response status 422 with the errors info', function () { Http::fake([ - "*/api/reservations" => Http::response([ - "message" => "The date from field must be a date before or equal to date to. (and 1 more error)", - "errors" => [ - "date_from" => [ - "The date from field must be a date before or equal to date to." - ], - "date_to" => [ - "The date to field must be a date after or equal to date from." - ] - ] - ], 422) + '*/api/reservations' => Http::response([ + 'message' => 'The date from field must be a date before or equal to date to. (and 1 more error)', + 'errors' => [ + 'date_from' => [ + 'The date from field must be a date before or equal to date to.', + ], + 'date_to' => [ + 'The date to field must be a date after or equal to date from.', + ], + ], + ], 422), ]); $jsonResponse = $this->getJson('/api/requests/reservations/not-valid') @@ -61,17 +56,17 @@ ->assertJson([ 'error' => 'API request failed', 'response' => [ - "message" => "The date from field must be a date before or equal to date to. (and 1 more error)", - "errors" => [ - "date_from" => [ - "The date from field must be a date before or equal to date to." + 'message' => 'The date from field must be a date before or equal to date to. (and 1 more error)', + 'errors' => [ + 'date_from' => [ + 'The date from field must be a date before or equal to date to.', + ], + 'date_to' => [ + 'The date to field must be a date after or equal to date from.', ], - "date_to" => [ - "The date to field must be a date after or equal to date from." - ] - ] - ] + ], + ], ]); }); -}); \ No newline at end of file +}); diff --git a/src/tests/Feature/ReservationsControllerTest.php b/src/tests/Feature/ReservationsControllerTest.php index 8c10873..7b25db4 100644 --- a/src/tests/Feature/ReservationsControllerTest.php +++ b/src/tests/Feature/ReservationsControllerTest.php @@ -20,10 +20,10 @@ // act Reservation::create([ - 'requester'=>'biblio x', - 'date_from'=>'2024-03-01', - 'date_to'=>'2024-03-10', - 'book_id'=> $book->id, + 'requester' => 'biblio x', + 'date_from' => '2024-03-01', + 'date_to' => '2024-03-10', + 'book_id' => $book->id, ]); $jsonResponse = $this->getJson('/api/reservations') @@ -50,13 +50,12 @@ 'date_to' => '2024-03-10 12:00:00', 'book_id' => $book->id, ]) - ->assertStatus(201) - ->assertJsonPath('requester', 'biblio x') - ->assertJsonPath('date_from', '2024-03-01 12:00:00') - ->assertJsonPath('date_to', '2024-03-10 12:00:00') - ->assertJsonPath('book.title', $book->title); + ->assertStatus(201) + ->assertJsonPath('requester', 'biblio x') + ->assertJsonPath('date_from', '2024-03-01 12:00:00') + ->assertJsonPath('date_to', '2024-03-10 12:00:00') + ->assertJsonPath('book.title', $book->title); - ; }); test('a library manager cannot create a new reservation when dates are not valid', function () { @@ -76,11 +75,11 @@ 'date_to' => '2024-03-10 12:00:00', 'book_id' => $book->id, ]) - ->assertStatus(422) - ->assertJsonPath('errors', [ - 'date_from' => ['The date from field must be a date before or equal to date to.'], - 'date_to' => ['The date to field must be a date after or equal to date from.'], - ]); + ->assertStatus(422) + ->assertJsonPath('errors', [ + 'date_from' => ['The date from field must be a date before or equal to date to.'], + 'date_to' => ['The date to field must be a date after or equal to date from.'], + ]); }); test('a library manager cannot create a new reservation when dates for a book overlap other reservations and dates', function () { @@ -94,10 +93,10 @@ ]); $reservation = Reservation::create([ - 'requester'=>'biblio x', - 'date_from'=>'2024-03-01 12:00:00', - 'date_to'=>'2024-03-10 12:00:00', - 'book_id'=> $book->id, + 'requester' => 'biblio x', + 'date_from' => '2024-03-01 12:00:00', + 'date_to' => '2024-03-10 12:00:00', + 'book_id' => $book->id, ]); //act & assert @@ -107,10 +106,10 @@ 'date_to' => '2024-03-20 12:00:00', 'book_id' => $book->id, ]) - ->assertStatus(422) - ->assertJsonPath('errors', [ - 'date_to' => ['The reservation period overlaps with a previous one.'], - ]); + ->assertStatus(422) + ->assertJsonPath('errors', [ + 'date_to' => ['The reservation period overlaps with a previous one.'], + ]); }); -}); \ No newline at end of file +}); diff --git a/src/tests/Unit/BookTest.php b/src/tests/Unit/BookTest.php index 27e94c8..c214fd5 100644 --- a/src/tests/Unit/BookTest.php +++ b/src/tests/Unit/BookTest.php @@ -1,7 +1,6 @@ shouldReceive('where')->once()->andReturnSelf(); $reservation->shouldReceive('exists')->once()->andReturnTrue(); - try{ + try { $validator->validated(); - } catch(Exception $e){ + } catch (Exception $e) { expect($e->getMessage())->toBe('The reservation period overlaps with a previous one.'); } diff --git a/src/tests/Unit/ReservationTest.php b/src/tests/Unit/ReservationTest.php index 471bf6d..d5ff083 100644 --- a/src/tests/Unit/ReservationTest.php +++ b/src/tests/Unit/ReservationTest.php @@ -14,7 +14,7 @@ 'requester' => 'biblio x', 'date_from' => '2024-03-01', 'date_to' => '2024-03-10', - 'book_id' => 1 + 'book_id' => 1, ] );