From 61a009ca08cb0507909382f56197a4b21f679d58 Mon Sep 17 00:00:00 2001 From: Obinna Elvis Okechukwu Date: Fri, 19 Jan 2024 19:22:39 +0100 Subject: [PATCH] Form validation fields (#43) * add comment field to reservation request * PHP Linting (Pint) - Fix styling * add tests for reservation requests --------- Co-authored-by: obinnaelviso --- src/Requests/ReservationRequest.php | 2 + tests/Requests/ReservationTest.php | 72 +++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/Requests/ReservationRequest.php b/src/Requests/ReservationRequest.php index 2756d67..e01d283 100644 --- a/src/Requests/ReservationRequest.php +++ b/src/Requests/ReservationRequest.php @@ -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'), ]; } @@ -33,6 +34,7 @@ public function rules() 'guest_num' => ['required', 'integer'], 'duration' => ['integer', 'min:1'], 'tables' => ['nullable', 'array'], + 'comment' => ['nullable', 'string'], ]; } } diff --git a/tests/Requests/ReservationTest.php b/tests/Requests/ReservationTest.php index 8937e3f..8906773 100644 --- a/tests/Requests/ReservationTest.php +++ b/tests/Requests/ReservationTest.php @@ -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')) @@ -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')); });