Skip to content

Commit

Permalink
Form validation fields (#43)
Browse files Browse the repository at this point in the history
* add comment field to reservation request

* PHP Linting (Pint) - Fix styling

* add tests for reservation requests

---------

Co-authored-by: obinnaelviso <[email protected]>
  • Loading branch information
obinnaelviso and obinnaelviso authored Jan 19, 2024
1 parent 0963910 commit 61a009c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Requests/ReservationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function attributes()
'reserve_date' => lang('igniter.reservation::default.label_reservation_date'),
'reserve_time' => lang('igniter.reservation::default.label_reservation_time'),
'guest_num' => lang('igniter.reservation::default.label_guest'),
'comment' => lang('igniter.reservation::default.column_comment'),
];
}

Expand All @@ -33,6 +34,7 @@ public function rules()
'guest_num' => ['required', 'integer'],
'duration' => ['integer', 'min:1'],
'tables' => ['nullable', 'array'],
'comment' => ['nullable', 'string'],
];
}
}
72 changes: 68 additions & 4 deletions tests/Requests/ReservationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Igniter\Reservation\Requests\ReservationRequest;

it('has required rule for inputs', function () {
it('has required rule for inputs: location_id, first_name, last_name, reserve_date, reserve_time, guest_num', function () {
expect('required')->toBeIn(array_get((new ReservationRequest)->rules(), 'location_id'))
->and('required')->toBeIn(array_get((new ReservationRequest)->rules(), 'first_name'))
->and('required')->toBeIn(array_get((new ReservationRequest)->rules(), 'last_name'))
Expand All @@ -13,13 +13,77 @@
->and('required')->toBeIn(array_get((new ReservationRequest)->rules(), 'guest_num'));
});

it('has sometimes rule for inputs: location_id and telephone', function () {
expect('sometimes')->toBeIn(array_get((new ReservationRequest)->rules(), 'location_id'))
->and('sometimes')->toBeIn(array_get((new ReservationRequest)->rules(), 'telephone'));
});

it('has nullable rule for inputs: tables and comment', function () {
expect('nullable')->toBeIn(array_get((new ReservationRequest)->rules(), 'tables'))
->and('nullable')->toBeIn(array_get((new ReservationRequest)->rules(), 'comment'));
});

it('has min:1 rule for duration input', function () {
expect('min:1')->toBeIn(array_get((new ReservationRequest)->rules(), 'duration'));
});

it('has array rule for tables input', function () {
expect('array')->toBeIn(array_get((new ReservationRequest)->rules(), 'tables'));
});

it('has between:1,48 rule for inputs: first_name and last_name', function () {
expect('between:1,48')->toBeIn(array_get((new ReservationRequest)->rules(), 'first_name'))
->and('between:1,48')->toBeIn(array_get((new ReservationRequest)->rules(), 'last_name'));
});

it('has email:filter rule for inputs: email', function () {
expect('email:filter')->toBeIn(array_get((new ReservationRequest)->rules(), 'email'));
});

it('has integer rule for inputs:
location_id, guest_num and duration',
function () {
$rules = (new ReservationRequest)->rules();
$inputNames = ['location_id', 'guest_num', 'duration'];
$testExpectation = null;

foreach ($inputNames as $key => $inputName) {
if ($key == 0) {
$testExpectation = expect('integer')->toBeIn(array_get($rules, $inputName));
}
$testExpectation = $testExpectation->and('integer')->toBeIn(array_get($rules, $inputName));
}

}
);

it('has string rule for inputs:
first_name, last_name, telephone and comment',
function () {
$rules = (new ReservationRequest)->rules();
$inputNames = ['first_name', 'last_name', 'telephone', 'comment'];
$testExpectation = null;

foreach ($inputNames as $key => $inputName) {
if ($key == 0) {
$testExpectation = expect('string')->toBeIn(array_get($rules, $inputName));
}
$testExpectation = $testExpectation->and('string')->toBeIn(array_get($rules, $inputName));
}

}
);

it('has max characters rule for inputs', function () {
expect('between:1,48')->toBeIn(array_get((new ReservationRequest)->rules(), 'first_name'))
->and('between:1,48')->toBeIn(array_get((new ReservationRequest)->rules(), 'last_name'))
->and('max:96')->toBeIn(array_get((new ReservationRequest)->rules(), 'email'));
});

it('has valid_date and valid_time rule for inputs', function () {
expect('valid_date')->toBeIn(array_get((new ReservationRequest)->rules(), 'reserve_date'))
->and('valid_time')->toBeIn(array_get((new ReservationRequest)->rules(), 'reserve_time'));
it('has valid_date rule for reserve_date input', function () {
expect('valid_date')->toBeIn(array_get((new ReservationRequest)->rules(), 'reserve_date'));
});

it('has valid_time rule for reserve_time input', function () {
expect('valid_time')->toBeIn(array_get((new ReservationRequest)->rules(), 'reserve_time'));
});

0 comments on commit 61a009c

Please sign in to comment.