From 5fa2155cf360c7414a85fc32446a2d22fba20911 Mon Sep 17 00:00:00 2001 From: Obinna Elvis Okechukwu Date: Sun, 17 Dec 2023 23:09:56 +0100 Subject: [PATCH 1/2] add the following menu fields to requests: mealtimes, ingredients --- src/Requests/MenuRequest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Requests/MenuRequest.php b/src/Requests/MenuRequest.php index ae134c5..3222a54 100644 --- a/src/Requests/MenuRequest.php +++ b/src/Requests/MenuRequest.php @@ -13,6 +13,8 @@ public function attributes() 'menu_description' => lang('igniter::admin.label_description'), 'menu_price' => lang('igniter.cart::default.menus.label_price'), 'categories.*' => lang('igniter.cart::default.menus.label_category'), + 'ingredients.*' => lang('igniter.cart::default.menus.label_ingredients'), + 'mealtimes.*' => lang('igniter.cart::default.menus.label_mealtime'), 'locations.*' => lang('igniter::admin.column_location'), 'minimum_qty' => lang('igniter.cart::default.menus.label_minimum_qty'), 'order_restriction.*' => lang('igniter.cart::default.menus.label_order_restriction'), @@ -30,6 +32,8 @@ public function rules() 'menu_description' => ['string', 'between:2,1028'], 'menu_price' => ['required', 'numeric', 'min:0'], 'categories.*' => ['sometimes', 'required', 'integer'], + 'ingredients.*' => ['sometimes', 'required', 'integer'], + 'mealtimes.*' => ['sometimes', 'required', 'integer'], 'locations.*' => ['integer'], 'minimum_qty' => ['sometimes', 'required', 'integer', 'min:1'], 'order_restriction.*' => ['nullable', 'string'], From 7fdc427827cfae3db663a7159f9d6cc1f0c0612e Mon Sep 17 00:00:00 2001 From: Obinna Elvis Okechukwu Date: Sun, 31 Dec 2023 20:18:22 +0100 Subject: [PATCH 2/2] Add test for menu requests --- tests/Requests/MenuTest.php | 118 +++++++++++++++++++++++++++++++++--- 1 file changed, 110 insertions(+), 8 deletions(-) diff --git a/tests/Requests/MenuTest.php b/tests/Requests/MenuTest.php index 1f67627..29c922b 100644 --- a/tests/Requests/MenuTest.php +++ b/tests/Requests/MenuTest.php @@ -4,16 +4,118 @@ use Igniter\Cart\Requests\MenuRequest; -it('has rules for menu_name', function () { - expect('required')->toBeIn(array_get((new MenuRequest)->rules(), 'menu_name')) - ->and('between:2,255')->toBeIn(array_get((new MenuRequest)->rules(), 'menu_name')); +it('has required rule for inputs: + menu_name, menu_price, categories.*, ingredients.*, mealtimes.*, minimum_qty', + function () { + $rules = (new MenuRequest)->rules(); + $inputNames = ['menu_name', 'menu_price', 'categories.*', 'ingredients.*', 'mealtimes.*', 'minimum_qty']; + $testExpectation = null; + + foreach ($inputNames as $key => $inputName) { + if ($key == 0) { + $testExpectation = expect('required')->toBeIn(array_get($rules, $inputName)); + } + $testExpectation = $testExpectation->and('required')->toBeIn(array_get($rules, $inputName)); + } + } +); + +it('has string rule for inputs: + menu_name, menu_description, order_restriction.*', + function () { + $rules = (new MenuRequest)->rules(); + $inputNames = ['menu_name', 'menu_description', 'order_restriction.*']; + $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 between:2,255 rule for menu_name input', function () { + $rules = (new MenuRequest)->rules(); + + expect('between:2,255')->toBeIn(array_get($rules, 'menu_name')); +}); + +it('has between:2,1028 rule for menu_description input', function () { + $rules = (new MenuRequest)->rules(); + + expect('between:2,1028')->toBeIn(array_get($rules, 'menu_description')); +}); + +it('has numeric rule for menu_price input', function () { + $rules = (new MenuRequest)->rules(); + + expect('numeric')->toBeIn(array_get($rules, 'menu_price')); }); -it('has rules for menu_price', function () { - expect('required')->toBeIn(array_get((new MenuRequest)->rules(), 'menu_price')) - ->and('min:0')->toBeIn(array_get((new MenuRequest)->rules(), 'menu_price')); +it('has min:0 rule for menu_price input', function () { + $rules = (new MenuRequest)->rules(); + + expect('min:0')->toBeIn(array_get($rules, 'menu_price')); +}); + +it('has min:1 rule for minimum_qty input', function () { + $rules = (new MenuRequest)->rules(); + + expect('min:1')->toBeIn(array_get($rules, 'minimum_qty')); }); -it('has rules for menu_description', function () { - expect('between:2,1028')->toBeIn(array_get((new MenuRequest)->rules(), 'menu_description')); +it('has boolean rule for menu_status input', function () { + $rules = (new MenuRequest)->rules(); + + expect('boolean')->toBeIn(array_get($rules, 'menu_status')); }); + +it('has sometimes rule for inputs: + categories.*, ingredients.*, mealtimes.*, minimum_qty', + function () { + $rules = (new MenuRequest)->rules(); + $inputNames = ['categories.*', 'ingredients.*', 'mealtimes.*', 'minimum_qty']; + $testExpectation = null; + + foreach ($inputNames as $key => $inputName) { + if ($key == 0) { + $testExpectation = expect('sometimes')->toBeIn(array_get($rules, $inputName)); + } + $testExpectation = $testExpectation->and('sometimes')->toBeIn(array_get($rules, $inputName)); + } + } +); + +it('has integer rule for inputs: + categories.*, ingredients.*, mealtimes.*, locations.*, minimum_qty, mealtime_id, menu_priority', + function () { + $rules = (new MenuRequest)->rules(); + $inputNames = ['categories.*', 'ingredients.*', 'mealtimes.*', 'locations.*', 'minimum_qty', 'mealtime_id', 'menu_priority']; + $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 nullable rule for inputs: + order_restriction.*, mealtime_id, menu_priority', + function () { + $rules = (new MenuRequest)->rules(); + $inputNames = ['order_restriction.*', 'mealtime_id', 'menu_priority']; + $testExpectation = null; + + foreach ($inputNames as $key => $inputName) { + if ($key == 0) { + $testExpectation = expect('nullable')->toBeIn(array_get($rules, $inputName)); + } + $testExpectation = $testExpectation->and('nullable')->toBeIn(array_get($rules, $inputName)); + } + } +);