Skip to content

Commit

Permalink
* fix passing empty entities by denormalize the array with props valu…
Browse files Browse the repository at this point in the history
…e into entity objects via their prop setter and filter out default values @ `(test|provide)NestPostsWithParent()`

+ dependency `DenormalizerInterface` for `testNestPostsWithParent()`
@ `App\Tests\PostsQuery\BaseQueryTest`
@ be
  • Loading branch information
n0099 committed Oct 9, 2024
1 parent 2e9fc86 commit 4504af1
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions be/tests/PostsQuery/BaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace App\Tests\PostsQuery;

use App\Entity\Post\Reply;
use App\Entity\Post\SubReply;
use App\Entity\Post\Thread;
use App\Helper;
use App\PostsQuery\BaseQuery;
use App\PostsQuery\IndexQuery;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

#[CoversClass(BaseQuery::class)]
class BaseQueryTest extends KernelTestCase
{
private BaseQuery $sut;
private DenormalizerInterface $denormalizer;

protected function setUp(): void
{
Expand All @@ -23,6 +24,7 @@ protected function setUp(): void
$container = self::getContainer();
$this->sut = $container->get(IndexQuery::class);
(new \ReflectionProperty(BaseQuery::class, 'orderByField'))->setValue($this->sut, 'postedAt');
$this->denormalizer = $container->get(DenormalizerInterface::class);
}

public function testPerPageItemsDefaultValue(): void
Expand Down Expand Up @@ -220,19 +222,39 @@ public static function provideReOrderNestedPostsData(): array
#[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)),
$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' => [new Thread(['tid' => 1])],
'replies' => [new Reply(['tid' => 1, 'pid' => 2])],
'subReplies' => [new SubReply(['tid' => 1, 'pid' => 2, 'spid' => 3])],
'threads' => [['tid' => 1]],
'replies' => [['tid' => 1, 'pid' => 2]],
'subReplies' => [['tid' => 1, 'pid' => 2, 'spid' => 3]],
],
[[
'tid' => 1,
Expand Down

0 comments on commit 4504af1

Please sign in to comment.