From 5294102339b1f8fe7a030e6c3ed5a46df06581e9 Mon Sep 17 00:00:00 2001 From: n0099 Date: Wed, 25 Sep 2024 12:09:42 +0000 Subject: [PATCH] * fix treating any post types coverage as `ALL` when it's not `SUB` @ `isRequiredPostTypes()` * move variable `$paramsRequiredPostTypes` out of `validate40003()` as public const `REQUIRED_POST_TYPES_KEY_BY_PARAM_NAME` * move variable `$orderByRequiredPostTypes` out of `validate40004()` as public const `REQUIRED_POST_TYPES_KEY_BY_ORDER_BY_VALUE` @ ParamsValidator * move variable `$paramDefaultValueByType` & `$paramsNameByType` out of `addDefaultValueOnParams()` as public const `PARAM_DEFAULT_VALUE_KEY_BY_TYPE` & `PARAM_NAME_KEY_BY_TYPE` @ QueryParams @ `App\Http\PostsQuery` * providing all predefined params and their expected output as test data @ `provideValidate4000{3,4}()` * add provided data name by using string keys in the returned array @ `provideValidate4000{1,5}()` @ `Tests\Feature\App\Http\PostsQuery\ParamsValidatorTest` * providing all predefined params and their expected output as test data @ `provideAddDefaultValueOnParams()` * now providing empty input and expecting all filled default params @ `provideAddDefaultValueOnUniqueParams()` @ `Tests\Unit\App\Http\PostsQuery\QueryParamsTest` @ be --- be/app/Http/PostsQuery/ParamsValidator.php | 66 +++++++++++-------- be/app/Http/PostsQuery/QueryParams.php | 58 ++++++++-------- .../Http/PostsQuery/ParamsValidatorTest.php | 34 ++++++++-- .../App/Http/PostsQuery/QueryParamsTest.php | 32 ++++++--- 4 files changed, 120 insertions(+), 70 deletions(-) diff --git a/be/app/Http/PostsQuery/ParamsValidator.php b/be/app/Http/PostsQuery/ParamsValidator.php index 60e139cf..57f0058d 100644 --- a/be/app/Http/PostsQuery/ParamsValidator.php +++ b/be/app/Http/PostsQuery/ParamsValidator.php @@ -100,49 +100,59 @@ private function validate40005(): void private static function isRequiredPostTypes(array $current, array $required): bool { - $required[1] = Arr::sort($required[1]); - return $required[0] === 'SUB' - ? array_diff($current, $required[1]) === [] - : $current === $required[1]; + /** @var 'SUB' | 'All' $coverage */ + /** @var array $postTypes */ + [$coverage, $postTypes] = $required; + $postTypes = Arr::sort($postTypes); + return match ($coverage) { + 'SUB' => array_diff($current, $postTypes) === [], + 'ALL' => $current === $postTypes, + default => throw new \Exception(), + }; } + public const array REQUIRED_POST_TYPES_KEY_BY_PARAM_NAME = [ + 'pid' => ['SUB', ['reply', 'subReply']], + 'spid' => ['ALL', ['subReply']], + 'latestReplyPostedAt' => ['ALL', ['thread']], + 'threadTitle' => ['ALL', ['thread']], + 'postContent' => ['SUB', ['reply', 'subReply']], + 'threadViewCount' => ['ALL', ['thread']], + 'threadShareCount' => ['ALL', ['thread']], + 'threadReplyCount' => ['ALL', ['thread']], + 'replySubReplyCount' => ['ALL', ['reply']], + 'threadProperties' => ['ALL', ['thread']], + 'authorExpGrade' => ['SUB', ['reply', 'subReply']], + 'latestReplierUid' => ['ALL', ['thread']], + 'latestReplierName' => ['ALL', ['thread']], + 'latestReplierDisplayName' => ['ALL', ['thread']], + 'latestReplierGender' => ['ALL', ['thread']], + ]; + private function validate40003(array $currentPostTypes): void { - $paramsRequiredPostTypes = [ - 'pid' => ['SUB', ['reply', 'subReply']], - 'spid' => ['ALL', ['subReply']], - 'latestReplyPostedAt' => ['ALL', ['thread']], - 'threadTitle' => ['ALL', ['thread']], - 'postContent' => ['SUB', ['reply', 'subReply']], - 'threadViewCount' => ['ALL', ['thread']], - 'threadShareCount' => ['ALL', ['thread']], - 'threadReplyCount' => ['ALL', ['thread']], - 'replySubReplyCount' => ['ALL', ['reply']], - 'threadProperties' => ['ALL', ['thread']], - 'authorExpGrade' => ['SUB', ['reply', 'subReply']], - 'latestReplierUid' => ['ALL', ['thread']], - 'latestReplierName' => ['ALL', ['thread']], - 'latestReplierDisplayName' => ['ALL', ['thread']], - 'latestReplierGender' => ['ALL', ['thread']], - ]; - foreach ($paramsRequiredPostTypes as $paramName => $requiredPostTypes) { + foreach (self::REQUIRED_POST_TYPES_KEY_BY_PARAM_NAME as $paramName => $requiredPostTypes) { if ($this->params->pick($paramName) !== []) { Helper::abortAPIIfNot(40003, self::isRequiredPostTypes($currentPostTypes, $requiredPostTypes)); } } } + public const array REQUIRED_POST_TYPES_KEY_BY_ORDER_BY_VALUE = [ + 'pid' => ['SUB', ['reply', 'subReply']], + 'spid' => ['SUB', ['subReply']], + ]; + private function validate40004(array $currentPostTypes): void { - $orderByRequiredPostTypes = [ - 'pid' => ['SUB', ['reply', 'subReply']], - 'spid' => ['SUB', ['subReply']], - ]; $currentOrderBy = (string) $this->params->getUniqueParamValue('orderBy'); - if (\array_key_exists($currentOrderBy, $orderByRequiredPostTypes)) { + if (\array_key_exists($currentOrderBy, self::REQUIRED_POST_TYPES_KEY_BY_ORDER_BY_VALUE)) { Helper::abortAPIIfNot( 40004, - self::isRequiredPostTypes($currentPostTypes, $orderByRequiredPostTypes[$currentOrderBy]), + self::isRequiredPostTypes( + $currentPostTypes, + self::REQUIRED_POST_TYPES_KEY_BY_ORDER_BY_VALUE[$currentOrderBy] + ), ); } } diff --git a/be/app/Http/PostsQuery/QueryParams.php b/be/app/Http/PostsQuery/QueryParams.php index 43daa753..fc25f3a3 100644 --- a/be/app/Http/PostsQuery/QueryParams.php +++ b/be/app/Http/PostsQuery/QueryParams.php @@ -84,37 +84,39 @@ public function addDefaultValueOnUniqueParams(): void } } + public const array PARAM_DEFAULT_VALUE_KEY_BY_TYPE = [ + 'numeric' => ['range' => '='], + 'text' => ['matchBy' => 'explicit', 'spaceSplit' => false], + ]; + + public const array PARAM_NAME_KEY_BY_TYPE = [ + 'numeric' => [ + 'tid', + 'pid', + 'spid', + 'threadViewCount', + 'threadShareCount', + 'threadReplyCount', + 'replySubReplyCount', + 'authorUid', + 'authorExpGrade', + 'latestReplierUid', + ], + 'text' => [ + 'threadTitle', + 'postContent', + 'authorName', + 'authorDisplayName', + 'latestReplierName', + 'latestReplierDisplayName', + ], + ]; + public function addDefaultValueOnParams(): void { - $paramDefaultValueByType = [ - 'numeric' => ['range' => '='], - 'text' => ['matchBy' => 'explicit', 'spaceSplit' => false], - ]; - $paramsNameByType = [ - 'numeric' => [ - 'tid', - 'pid', - 'spid', - 'threadViewCount', - 'threadShareCount', - 'threadReplyCount', - 'replySubReplyCount', - 'authorUid', - 'authorExpGrade', - 'latestReplierUid', - ], - 'text' => [ - 'threadTitle', - 'postContent', - 'authorName', - 'authorDisplayName', - 'latestReplierName', - 'latestReplierDisplayName', - ], - ]; - $subParamsDefaultValue = collect($paramsNameByType) + $subParamsDefaultValue = collect(self::PARAM_NAME_KEY_BY_TYPE) ->mapWithKeys(static fn(array $names, string $type) => - array_fill_keys($names, $paramDefaultValueByType[$type])); + array_fill_keys($names, self::PARAM_DEFAULT_VALUE_KEY_BY_TYPE[$type])); foreach ($this->params as $param) { // set sub params with default value foreach ($subParamsDefaultValue->get($param->name, []) as $name => $value) { if ($param->getSub($name) === null) { diff --git a/be/tests/Feature/App/Http/PostsQuery/ParamsValidatorTest.php b/be/tests/Feature/App/Http/PostsQuery/ParamsValidatorTest.php index 4ce048aa..414c2bb9 100644 --- a/be/tests/Feature/App/Http/PostsQuery/ParamsValidatorTest.php +++ b/be/tests/Feature/App/Http/PostsQuery/ParamsValidatorTest.php @@ -38,23 +38,47 @@ public function testValidate(int $errorCode, array $params): void public static function provideValidate40001(): array { $uniqueParams = [['postTypes' => ['thread']], ['orderBy' => 'postedAt']]; - return array_map(static fn(array $i) => [40001, $i], [[$uniqueParams[0]], [$uniqueParams[1]], $uniqueParams]); + return collect([[$uniqueParams[0]], [$uniqueParams[1]], $uniqueParams]) + ->mapWithKeys(static function (array $params) { + $keys = implode(',', array_map(static fn(array $param) => array_key_first($param), $params)); + return ["40001, $keys" => [40001, $params]]; + })->all(); } public static function provideValidate40005(): array { - $uniqueParams = [['fid' => 0], ['postTypes' => ['thread']], ['orderBy' => 'postedAt']]; - return array_map(static fn(array $p) => [40005, [['tid' => 0], $p, $p]], $uniqueParams); + return collect([['fid' => 0], ['postTypes' => ['thread']], ['orderBy' => 'postedAt']]) + ->mapWithKeys(static fn(array $p) => [ + '40005, ' . array_key_first($p) => [40005, [['tid' => 0], $p, $p]], + ]) + ->all(); } public static function provideValidate40003(): array { - return [[40003, [['postTypes' => ['thread']], ['spid' => '0']]]]; + return collect(ParamsValidator::REQUIRED_POST_TYPES_KEY_BY_PARAM_NAME) + ->map(static fn(array $coverageAndPostTypes, string $name) => [ + [$name => match ($name) { + 'latestReplyPostedAt' => '2024-01-01,2024-01-01', + 'threadProperties' => ['sticky'], + default => '0', + }], + ['postTypes' => array_diff(Helper::POST_TYPES, $coverageAndPostTypes[1])], + ]) + ->mapWithKeys(static fn(array $i, string $name) => ["40003, $name" => [40003, $i]]) + ->all(); } public static function provideValidate40004(): array { - return [[40004, [['postTypes' => ['thread']], ['tid' => '0'], ['orderBy' => 'spid']]]]; + return collect(ParamsValidator::REQUIRED_POST_TYPES_KEY_BY_ORDER_BY_VALUE) + ->map(static fn(array $coverageAndPostTypes, string $name) => [ + ['tid' => 0], + ['postTypes' => array_diff(Helper::POST_TYPES, $coverageAndPostTypes[1])], + ['orderBy' => $name], + ]) + ->mapWithKeys(static fn(array $i, string $name) => ["40004, $name" => [40004, $i]]) + ->all(); } #[DataProvider('providerDateRangeParamValueOrder')] diff --git a/be/tests/Unit/App/Http/PostsQuery/QueryParamsTest.php b/be/tests/Unit/App/Http/PostsQuery/QueryParamsTest.php index 3d7f8ac8..8316d902 100644 --- a/be/tests/Unit/App/Http/PostsQuery/QueryParamsTest.php +++ b/be/tests/Unit/App/Http/PostsQuery/QueryParamsTest.php @@ -48,10 +48,10 @@ public function testAddDefaultValueOnUniqueParams(array $params, array $expected public static function provideAddDefaultValueOnUniqueParams(): array { return [[ - [['orderBy' => 'test']], + [], [ - ['orderBy' => 'test', 'direction' => 'ASC'], ['postTypes' => Helper::POST_TYPES], + ['orderBy' => 'default', 'direction' => 'ASC'], ], ]]; } @@ -66,12 +66,26 @@ public function testAddDefaultValueOnParams(array $params, array $expected): voi public static function provideAddDefaultValueOnParams(): array { - return [[ - [['tid' => 0], ['threadTitle' => 'test']], - [ - ['tid' => 0, 'range' => '='], - ['threadTitle' => 'test', 'matchBy' => 'explicit', 'spaceSplit' => false], - ], - ]]; + return collect(QueryParams::PARAM_NAME_KEY_BY_TYPE) + ->flatMap(static fn(array $names, string $type) => + array_map(static fn(string $name) => [$type, $name], $names)) + ->mapWithKeys(static fn(array $typeAndName) => [$typeAndName[1] => $typeAndName]) + ->map(static function (array $typeAndName) { + /** @var 'numeric' | 'text' $name */ + /** @var string $name */ + [$type, $name] = $typeAndName; + $param = [ + $name => match ($type) { + 'numeric' => 0, + 'text' => 'test', + default => throw new \Exception(), + }, + ]; + return [ + [$param], + [[...$param, ...QueryParams::PARAM_DEFAULT_VALUE_KEY_BY_TYPE[$type]]], + ]; + }) + ->all(); } }