From 66d1507a4c7fef2cc5a8cf911a483eb5fcdbcd23 Mon Sep 17 00:00:00 2001 From: n0099 Date: Sat, 26 Oct 2024 16:34:57 +0000 Subject: [PATCH] * fix broken test `CursorCodecTest` & `PostsTreeTest` * rename class `BaseQueryTest` to `PostsTreeTest` @ `App\Tests\PostsQuery` @ be --- be/src/PostsQuery/SearchQuery.php | 4 +- be/tests/PostsQuery/BaseQueryTest.php | 269 ------------------------ be/tests/PostsQuery/CursorCodecTest.php | 19 +- be/tests/PostsQuery/PostsTreeTest.php | 191 +++++++++++++++++ 4 files changed, 204 insertions(+), 279 deletions(-) delete mode 100644 be/tests/PostsQuery/BaseQueryTest.php create mode 100644 be/tests/PostsQuery/PostsTreeTest.php diff --git a/be/src/PostsQuery/SearchQuery.php b/be/src/PostsQuery/SearchQuery.php index e68c060c..63727b76 100644 --- a/be/src/PostsQuery/SearchQuery.php +++ b/be/src/PostsQuery/SearchQuery.php @@ -25,8 +25,8 @@ public function query(QueryParams $params, ?string $cursor): void $fid = $params->getUniqueParamValue('fid'); $orderByParam = $params->pick('orderBy')[0]; - $this->setOrderByField($orderByParam->value === 'default' ? 'postedAt' : $orderByParam->value); - $this->setOrderByDesc($orderByParam->value === 'default' || $orderByParam->getSub('direction') === 'DESC'); + $this->setOrderByField($orderByParam->value === 'default' ? 'postedAt' : $orderByParam->value) + ->setOrderByDesc($orderByParam->value === 'default' || $orderByParam->getSub('direction') === 'DESC'); /** @var array $cachedUserQueryResult key by param name */ $cachedUserQueryResult = []; diff --git a/be/tests/PostsQuery/BaseQueryTest.php b/be/tests/PostsQuery/BaseQueryTest.php deleted file mode 100644 index 2c691db7..00000000 --- a/be/tests/PostsQuery/BaseQueryTest.php +++ /dev/null @@ -1,269 +0,0 @@ -sut = $container->get(IndexQuery::class); - (new \ReflectionProperty(BaseQuery::class, 'orderByField'))->setValue($this->sut, 'postedAt'); - $this->denormalizer = $container->get(DenormalizerInterface::class); - } - - public function testPerPageItemsDefaultValue(): void - { - $prop = new \ReflectionProperty(BaseQuery::class, 'perPageItems'); - self::assertEquals(50, $prop->getValue($this->sut)); - } - - #[DataProvider('provideReOrderNestedPostsData')] - public function testReOrderNestedPosts( - array $input, - bool $orderByDesc, - array $expected, - bool $shouldRemoveSortingKey, - ): void { - $input = collect($input)->recursive(); - (new \ReflectionProperty(BaseQuery::class, 'orderByDesc')) - ->setValue($this->sut, $orderByDesc); - if ($shouldRemoveSortingKey) { // make https://infection.github.io/guide/mutators.html#TrueValue happy - self::assertEquals($expected, $this->sut->reOrderNestedPosts($input)); - } else { - self::assertEquals($expected, $this->sut->reOrderNestedPosts($input, shouldRemoveSortingKey: false)); - } - } - - public static function provideReOrderNestedPostsData(): array - { - $input = [ - [ - 'postedAt' => 1, - 'isMatchQuery' => true, - 'replies' => [ - [ - 'postedAt' => 2, - 'isMatchQuery' => true, - 'subReplies' => [['postedAt' => 30]], - ], - [ - 'postedAt' => 20, - 'isMatchQuery' => false, - 'subReplies' => [['postedAt' => 3]], - ], - [ - 'postedAt' => 4, - 'isMatchQuery' => false, - 'subReplies' => [ - ['postedAt' => 5], - ['postedAt' => 33, 'isMatchQuery' => false], - ['postedAt' => 60], - ], - ], - ], - ], - [ - 'postedAt' => 7, - 'isMatchQuery' => false, - 'replies' => [ - ['postedAt' => 31, 'isMatchQuery' => true, 'subReplies' => []], - ], - ], - ]; - $expectedWhenOrderByAsc = [ - [ - 'postedAt' => 1, - 'isMatchQuery' => true, - 'replies' => [ - [ - 'postedAt' => 2, - 'isMatchQuery' => true, - 'subReplies' => [['postedAt' => 30]], - 'sortingKey' => 2, - ], - [ - 'postedAt' => 20, - 'isMatchQuery' => false, - 'subReplies' => [['postedAt' => 3]], - 'sortingKey' => 3, - ], - [ - 'postedAt' => 4, - 'isMatchQuery' => false, - 'subReplies' => [ - ['postedAt' => 5], - ['postedAt' => 33, 'isMatchQuery' => false], - ['postedAt' => 60], - ], - 'sortingKey' => 5, - ], - ], - 'sortingKey' => 1, - ], - [ - 'postedAt' => 7, - 'isMatchQuery' => false, - 'replies' => [ - [ - 'postedAt' => 31, - 'isMatchQuery' => true, - 'subReplies' => [], - 'sortingKey' => 31, - ], - ], - 'sortingKey' => 31, - ], - ]; - $expectedWhenOrderByAscRemoveSortingKey = [ - [ - 'postedAt' => 1, - 'isMatchQuery' => true, - 'replies' => [ - [ - 'postedAt' => 2, - 'isMatchQuery' => true, - 'subReplies' => [['postedAt' => 30]], - ], - [ - 'postedAt' => 20, - 'isMatchQuery' => false, - 'subReplies' => [['postedAt' => 3]], - ], - [ - 'postedAt' => 4, - 'isMatchQuery' => false, - 'subReplies' => [ - ['postedAt' => 5], - ['postedAt' => 33, 'isMatchQuery' => false], - ['postedAt' => 60], - ], - ], - ], - ], - [ - 'postedAt' => 7, - 'isMatchQuery' => false, - 'replies' => [ - [ - 'postedAt' => 31, - 'isMatchQuery' => true, - 'subReplies' => [], - ], - ], - ], - ]; - $expectedWhenOrderByDesc = [ - [ - 'postedAt' => 1, - 'isMatchQuery' => true, - 'replies' => [ - [ - 'postedAt' => 4, - 'isMatchQuery' => false, - 'subReplies' => [ - ['postedAt' => 60], - ['postedAt' => 33, 'isMatchQuery' => false], - ['postedAt' => 5], - ], - 'sortingKey' => 60, - ], - [ - 'postedAt' => 2, - 'isMatchQuery' => true, - 'subReplies' => [['postedAt' => 30]], - 'sortingKey' => 30, - ], - [ - 'postedAt' => 20, - 'isMatchQuery' => false, - 'subReplies' => [['postedAt' => 3]], - 'sortingKey' => 3, - ], - ], - 'sortingKey' => 60, - ], - [ - 'postedAt' => 7, - 'isMatchQuery' => false, - 'replies' => [ - [ - 'postedAt' => 31, - 'isMatchQuery' => true, - 'subReplies' => [], - 'sortingKey' => 31, - ], - ], - 'sortingKey' => 31, - ], - ]; - return [ - [$input, false, $expectedWhenOrderByAsc, false], - [$input, true, $expectedWhenOrderByDesc, false], - [$input, false, $expectedWhenOrderByAscRemoveSortingKey, true], - ]; - } - - #[DataProvider('provideNestPostsWithParent')] - public function testNestPostsWithParent(array $input, array $expected): void - { - $input = collect($input)->mapWithKeys(function (array $posts, string $postTypePluralName) { - $postClass = 'App\Entity\Post\\' . ucfirst(Helper::POST_TYPE_PLURAL_TO_SINGULAR[$postTypePluralName]); - return [ - $postTypePluralName => array_map( - fn(array $post) => $this->denormalizer->denormalize($post, $postClass), - $posts, - ), - ]; - })->all(); - self::assertEquals( - collect($expected)->recursive(), - $this->sut->nestPostsWithParent(...array_map('collect', $input)) - ->map(function (Collection $thread) { - /** @var Collection $replies */ - $replies = $thread['replies']; - $replies->transform(function (Collection $reply) { - /** @var Collection $subReplies */ - $subReplies = $reply['subReplies']; - $subReplies->transform(fn(Collection $subReply) => $subReply->filter()); - return $reply->filter(); - }); - return $thread->filter(); // remove normalized entity props with default value - }), - ); - } - - public static function provideNestPostsWithParent(): array - { - return [[ - [ - 'threads' => [['tid' => 1]], - 'replies' => [['tid' => 1, 'pid' => 2]], - 'subReplies' => [['tid' => 1, 'pid' => 2, 'spid' => 3]], - ], - [[ - 'tid' => 1, - 'replies' => [[ - 'tid' => 1, - 'pid' => 2, - 'subReplies' => [['tid' => 1, 'pid' => 2, 'spid' => 3]], - ]], - ]], - ]]; - } -} diff --git a/be/tests/PostsQuery/CursorCodecTest.php b/be/tests/PostsQuery/CursorCodecTest.php index 24d3d1fe..4f680629 100644 --- a/be/tests/PostsQuery/CursorCodecTest.php +++ b/be/tests/PostsQuery/CursorCodecTest.php @@ -2,6 +2,9 @@ namespace App\Tests\PostsQuery; +use App\DTO\PostKey\Reply; +use App\DTO\PostKey\SubReply; +use App\DTO\PostKey\Thread; use App\PostsQuery\CursorCodec; use Illuminate\Support\Collection; use PHPUnit\Framework\Attributes\CoversClass; @@ -30,31 +33,31 @@ public static function provideEncodeNextCursor(): array return [[ 'AQ,0,Ag,-:____fw,Aw,S:test', collect([ - 'threads' => collect([['tid' => 1, 'postedAt' => 0]]), - 'replies' => collect([['pid' => 2, 'postedAt' => -2147483649]]), - 'subReplies' => collect([['spid' => 3, 'postedAt' => 'test']]), + 'threads' => collect([new Thread(1, 'postedAt', 0)]), + 'replies' => collect([new Reply(1, 2, 'postedAt', -2147483649)]), + 'subReplies' => collect([new SubReply(1, 2, 3, 'postedAt', 'test')]), ]), ]]; } #[DataProvider('provideDecodeCursor')] - public function testDecodeCursor(string $cursor, array $expected): void + public function testDecodeCursor(string $cursor, Collection $expected): void { - self::assertEquals(collect($expected), $this->sut->decodeCursor($cursor, 'postedAt')); + self::assertEquals($expected, $this->sut->decodeCursor($cursor, 'postedAt')); } public static function provideDecodeCursor(): array { return [[ 'AQ,0,Ag,-:____fw,Aw,S:test', - [ + collect([ 'thread' => ['tid' => 1, 'postedAt' => 0], 'reply' => ['pid' => 2, 'postedAt' => -2147483649], 'subReply' => ['spid' => 3, 'postedAt' => 'test'], - ], + ]), ], [ ',,,,0,0', - [], + collect(), ]]; } } diff --git a/be/tests/PostsQuery/PostsTreeTest.php b/be/tests/PostsQuery/PostsTreeTest.php new file mode 100644 index 00000000..64f8224a --- /dev/null +++ b/be/tests/PostsQuery/PostsTreeTest.php @@ -0,0 +1,191 @@ +sut = $container->get(PostsTree::class); + } + + #[DataProvider('provideReOrderNestedPostsData')] + public function testReOrderNestedPosts(Collection $input, bool $orderByDesc, Collection $expected): void + { + self::assertEquals($expected, $this->sut->reOrderNestedPosts($input, 'postedAt', $orderByDesc)); + } + + public static function provideReOrderNestedPostsData(): array + { + $input = collect([ + (new Thread()) + ->setPostedAt(1) + ->setIsMatchQuery(true) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(2) + ->setIsMatchQuery(true) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(30), + ])), + (new Reply()) + ->setPostedAt(20) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(3), + ])), + (new Reply()) + ->setPostedAt(4) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(5), + (new SubReply())->setPostedAt(33)->setIsMatchQuery(false), + (new SubReply())->setPostedAt(60), + ])), + ])), + (new Thread()) + ->setPostedAt(7) + ->setIsMatchQuery(false) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(31) + ->setIsMatchQuery(true) + ->setSubReplies(collect()), + ])), + ]); + $expectedWhenOrderByAsc = collect([ + (new Thread()) + ->setPostedAt(1) + ->setIsMatchQuery(true) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(2) + ->setIsMatchQuery(true) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(30), + ])) + ->setSortingKey(2), + (new Reply()) + ->setPostedAt(20) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(3), + ])) + ->setSortingKey(3), + (new Reply()) + ->setPostedAt(4) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(5), + (new SubReply())->setPostedAt(33)->setIsMatchQuery(false), + (new SubReply())->setPostedAt(60), + ])) + ->setSortingKey(5), + ])) + ->setSortingKey(1), + (new Thread()) + ->setPostedAt(7) + ->setIsMatchQuery(false) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(31) + ->setIsMatchQuery(true) + ->setSubReplies(collect()) + ->setSortingKey(31), + ])) + ->setSortingKey(31), + ]); + $expectedWhenOrderByDesc = collect([ + (new Thread()) + ->setPostedAt(1) + ->setIsMatchQuery(true) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(4) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(60), + (new SubReply())->setPostedAt(33)->setIsMatchQuery(false), + (new SubReply())->setPostedAt(5), + ])) + ->setSortingKey(60), + (new Reply()) + ->setPostedAt(2) + ->setIsMatchQuery(true) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(30), + ])) + ->setSortingKey(30), + (new Reply()) + ->setPostedAt(20) + ->setIsMatchQuery(false) + ->setSubReplies(collect([ + (new SubReply())->setPostedAt(3), + ])) + ->setSortingKey(3), + ])) + ->setSortingKey(60), + (new Thread()) + ->setPostedAt(7) + ->setIsMatchQuery(false) + ->setReplies(collect([ + (new Reply()) + ->setPostedAt(31) + ->setIsMatchQuery(true) + ->setSubReplies(collect()) + ->setSortingKey(31), + ])) + ->setSortingKey(31), + ]); + return [ + [$input, false, $expectedWhenOrderByAsc], + [$input, true, $expectedWhenOrderByDesc], + ]; + } + + /** @param array{threads: Collection, replies: Collection, subReplies: Collection} $input */ + #[DataProvider('provideNestPostsWithParent')] + public function testNestPostsWithParent(array $input, Collection $expected): void + { + (new \ReflectionProperty(PostsTree::class, 'threads'))->setValue($this->sut, $input['threads']); + (new \ReflectionProperty(PostsTree::class, 'replies'))->setValue($this->sut, $input['replies']); + (new \ReflectionProperty(PostsTree::class, 'subReplies'))->setValue($this->sut, $input['subReplies']); + self::assertEquals($expected, $this->sut->nestPostsWithParent()); + } + + public static function provideNestPostsWithParent(): array + { + return [[ + [ + 'threads' => collect([(new Thread())->setTid(1)]), + 'replies' => collect([(new Reply())->setTid(1)->setPid(2)]), + 'subReplies' => collect([(new SubReply())->setTid(1)->setPid(2)->setSpid(3)]), + ], + collect([(new Thread()) + ->setTid(1) + ->setReplies(collect([ + (new Reply()) + ->setTid(1)->setPid(2) + ->setSubReplies(collect([ + (new SubReply())->setTid(1)->setPid(2)->setSpid(3), + ])), + ])), + ]), + ]]; + } +}