From 2d083a18dec22cc2c887cb6a317bb1b22518ae95 Mon Sep 17 00:00:00 2001 From: eparusov Date: Thu, 12 Dec 2024 08:04:17 +0300 Subject: [PATCH 1/4] #161: Ability to validate order_by field --- src/BaseRequest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/BaseRequest.php b/src/BaseRequest.php index 1fa76386..cba38533 100644 --- a/src/BaseRequest.php +++ b/src/BaseRequest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Foundation\Http\FormRequest; +use RonasIT\Support\Exceptions\InvalidModelException; class BaseRequest extends FormRequest { @@ -42,6 +43,17 @@ public function onlyValidated($keys = null, $default = null): array return $validatedFields; } + protected function getOrderableFields(string $modelName, array $additionalFields = []): string + { + if (!class_exists($modelName)) { + throw new InvalidModelException("The model {$modelName} does not exist."); + } + + $fields = array_merge($modelName::getFields(), $additionalFields); + + return implode(',', $fields); + } + protected function filterOnlyValidated($fields, $validation): array { $result = []; From 02d82b407821b20af76bf6d4c886d46685f1b4c7 Mon Sep 17 00:00:00 2001 From: eparusov Date: Thu, 12 Dec 2024 09:15:18 +0300 Subject: [PATCH 2/4] fix: test --- src/BaseRequest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/BaseRequest.php b/src/BaseRequest.php index cba38533..8ebe85a9 100644 --- a/src/BaseRequest.php +++ b/src/BaseRequest.php @@ -6,6 +6,9 @@ use Illuminate\Foundation\Http\FormRequest; use RonasIT\Support\Exceptions\InvalidModelException; +/** + * @codeCoverageIgnore + */ class BaseRequest extends FormRequest { public function authorize(): bool From 239e61c6bf0683fe25c94c897cc0ca1c5e962e04 Mon Sep 17 00:00:00 2001 From: eparusov Date: Thu, 12 Dec 2024 12:28:50 +0300 Subject: [PATCH 3/4] fix: test --- src/BaseRequest.php | 3 --- tests/BaseRequestTest.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/BaseRequestTest.php diff --git a/src/BaseRequest.php b/src/BaseRequest.php index 8ebe85a9..cba38533 100644 --- a/src/BaseRequest.php +++ b/src/BaseRequest.php @@ -6,9 +6,6 @@ use Illuminate\Foundation\Http\FormRequest; use RonasIT\Support\Exceptions\InvalidModelException; -/** - * @codeCoverageIgnore - */ class BaseRequest extends FormRequest { public function authorize(): bool diff --git a/tests/BaseRequestTest.php b/tests/BaseRequestTest.php new file mode 100644 index 00000000..9acb51ca --- /dev/null +++ b/tests/BaseRequestTest.php @@ -0,0 +1,39 @@ +getMethod('getOrderableFields'); + $orderableFields = $method->invoke($baseRequest, TestModel::class); + + $modelFields = implode(',', TestModel::getFields()); + + $this->assertEquals($orderableFields, $modelFields); + } + + public function testGetOrderableFieldsWithAdditionalFields() + { + $baseRequest = new BaseRequest(); + $reflectionClass = new ReflectionClass($baseRequest); + + $method = $reflectionClass->getMethod('getOrderableFields'); + $orderableFields = $method->invoke($baseRequest, TestModel::class, ['additional_field_1', 'additional_field_2']); + + $modelFields = implode(',', array_merge(TestModel::getFields(), ['additional_field_1', 'additional_field_2'])) ; + + $this->assertEquals($orderableFields, $modelFields); + } +} From 6d42c05c31407a0eb4b47aac5290bb76e17efa73 Mon Sep 17 00:00:00 2001 From: eparusov Date: Mon, 16 Dec 2024 15:21:55 +0300 Subject: [PATCH 4/4] refactor: tests --- tests/BaseRequestTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/BaseRequestTest.php b/tests/BaseRequestTest.php index 9acb51ca..ff003eb2 100644 --- a/tests/BaseRequestTest.php +++ b/tests/BaseRequestTest.php @@ -17,11 +17,11 @@ public function testGetOrderableFields() $reflectionClass = new ReflectionClass($baseRequest); $method = $reflectionClass->getMethod('getOrderableFields'); - $orderableFields = $method->invoke($baseRequest, TestModel::class); + $result = $method->invoke($baseRequest, TestModel::class); - $modelFields = implode(',', TestModel::getFields()); + $modelFields = 'id,name,json_field,castable_field,*,created_at,updated_at'; - $this->assertEquals($orderableFields, $modelFields); + $this->assertEquals($modelFields, $result); } public function testGetOrderableFieldsWithAdditionalFields() @@ -30,10 +30,10 @@ public function testGetOrderableFieldsWithAdditionalFields() $reflectionClass = new ReflectionClass($baseRequest); $method = $reflectionClass->getMethod('getOrderableFields'); - $orderableFields = $method->invoke($baseRequest, TestModel::class, ['additional_field_1', 'additional_field_2']); + $result = $method->invoke($baseRequest, TestModel::class, ['additional_field_1', 'additional_field_2']); - $modelFields = implode(',', array_merge(TestModel::getFields(), ['additional_field_1', 'additional_field_2'])) ; + $modelAndAdditionalFields = 'id,name,json_field,castable_field,*,created_at,updated_at,additional_field_1,additional_field_2'; - $this->assertEquals($orderableFields, $modelFields); + $this->assertEquals($modelAndAdditionalFields, $result); } }