Skip to content

Commit

Permalink
* fix treating any post types coverage as ALL when it's not SUB @…
Browse files Browse the repository at this point in the history
… `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
  • Loading branch information
n0099 committed Sep 25, 2024
1 parent dc4d69b commit 5294102
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 70 deletions.
66 changes: 38 additions & 28 deletions be/app/Http/PostsQuery/ParamsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
),
);
}
}
Expand Down
58 changes: 30 additions & 28 deletions be/app/Http/PostsQuery/QueryParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 29 additions & 5 deletions be/tests/Feature/App/Http/PostsQuery/ParamsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
32 changes: 23 additions & 9 deletions be/tests/Unit/App/Http/PostsQuery/QueryParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
]];
}
Expand All @@ -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();
}
}

0 comments on commit 5294102

Please sign in to comment.