Skip to content

Commit

Permalink
fix: linting code (pint)
Browse files Browse the repository at this point in the history
  • Loading branch information
gianni committed Feb 25, 2024
1 parent d597195 commit 086f917
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/app/Http/Requests/ReservationStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/app/Http/Resources/ReservationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\BookResource;

class ReservationResource extends JsonResource
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/Models/Reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
4 changes: 3 additions & 1 deletion src/app/Rules/DateRangeNotOverlap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
73 changes: 34 additions & 39 deletions src/tests/Feature/RequestsControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?php

use App\Models\Book;
use App\Models\Reservation;
use App\Http\Controllers\RequestsController;


describe('Requests Controller', function () {

test('A valid request from a library manager receive a response status 200 with the correct data', function () {

Http::fake([
"*/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)
'*/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')
Expand All @@ -36,42 +31,42 @@
'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')
->assertStatus(422)
->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."
]
]
]
],
],
]);
});

});
});
47 changes: 23 additions & 24 deletions src/tests/Feature/ReservationsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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
Expand All @@ -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.'],
]);
});

});
});
1 change: 0 additions & 1 deletion src/tests/Unit/BookTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use App\Models\Book;
use App\Models\Reservation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

Expand Down
7 changes: 3 additions & 4 deletions src/tests/Unit/DataRngeNotOverlapTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use App\Rules\DateRangeNotOverlap;
use App\Models\Reservation;
use Illuminate\Validation\ValidationException;
use App\Rules\DateRangeNotOverlap;
use Tests\TestCase;

uses(TestCase::class);
Expand Down Expand Up @@ -41,9 +40,9 @@
$reservation->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.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/Unit/ReservationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'requester' => 'biblio x',
'date_from' => '2024-03-01',
'date_to' => '2024-03-10',
'book_id' => 1
'book_id' => 1,
]
);

Expand Down

0 comments on commit 086f917

Please sign in to comment.