Skip to content

Commit e80f811

Browse files
authored
Merge pull request #209 from zigzagdev/feautre/show-post-index-application-dto
Add DTO and DTO Collection for GetAllUserPost use case
2 parents e32a9ef + dfd8c96 commit e80f811

File tree

9 files changed

+302
-135
lines changed

9 files changed

+302
-135
lines changed

.idea/workspace.xml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/Common/Application/Dto/Pagination.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class Pagination
99
public function __construct(
1010
private array $data,
1111
private ?int $currentPage = null,
12-
private ?string $from = null,
13-
private ?string $to = null,
12+
private ?int $from = null,
13+
private ?int $to = null,
1414
private ?int $perPage = null,
1515
private ?string $path = null,
1616
private ?int $lastPage = null,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Post\Application\ApplicationTest;
4+
5+
use Tests\TestCase;
6+
use App\Post\Application\Dto\GetAllUserPostDtoCollection;
7+
8+
class GetAllUserPostDtoCollectionTest extends TestCase
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
}
14+
15+
protected function tearDown(): void
16+
{
17+
parent::tearDown();
18+
}
19+
20+
private function arrayRequestData(): array
21+
{
22+
return [
23+
[
24+
'id' => 1,
25+
'userId' => 2,
26+
'content' => 'Sample post content',
27+
'mediaPath' => 'path/to/media.jpg',
28+
'visibility' => 'public',
29+
],
30+
[
31+
'id' => 2,
32+
'userId' => 3,
33+
'content' => 'Another post content',
34+
'mediaPath' => 'path/to/another_media.jpg',
35+
'visibility' => 'private',
36+
],
37+
];
38+
}
39+
40+
public function test_get_all_user_post_dto_collection(): void
41+
{
42+
$data = $this->arrayRequestData();
43+
$dtoCollection = GetAllUserPostDtoCollection::build($data);
44+
45+
$this->assertInstanceOf(GetAllUserPostDtoCollection::class, $dtoCollection);
46+
}
47+
48+
public function test_get_all_user_post_dto_collection_values(): void
49+
{
50+
$data = $this->arrayRequestData();
51+
$dtoCollection = GetAllUserPostDtoCollection::build($data);
52+
53+
$this->assertCount(count($data), $dtoCollection->getPosts());
54+
55+
foreach ($data as $index => $item) {
56+
$dto = $dtoCollection->getPosts()[$index];
57+
$this->assertEquals($dto->id, $item['id']);
58+
$this->assertEquals($dto->userId, $item['userId']);
59+
$this->assertEquals($dto->content, $item['content']);
60+
$this->assertEquals($dto->mediaPath, $item['mediaPath']);
61+
$this->assertEquals($dto->visibility, $item['visibility']);
62+
}
63+
}
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Post\Application\ApplicationTest;
4+
5+
use Tests\TestCase;
6+
use App\Post\Application\Dto\GetAllUserPostDto;
7+
8+
class GetAllUserPostDtoTest extends TestCase
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
}
14+
15+
protected function tearDown(): void
16+
{
17+
parent::tearDown();
18+
}
19+
20+
private function arrayRequestData(): array
21+
{
22+
return [
23+
'id' => 1,
24+
'userId' => 2,
25+
'content' => 'Sample post content',
26+
'mediaPath' => 'path/to/media.jpg',
27+
'visibility' => 'public',
28+
];
29+
}
30+
31+
public function test_get_all_user_post_dto() : void
32+
{
33+
$data = $this->arrayRequestData();
34+
$dto = GetAllUserPostDto::build($data);
35+
36+
$this->assertInstanceOf(GetAllUserPostDto::class, $dto);
37+
}
38+
39+
public function test_get_all_user_post_dto_values() : void
40+
{
41+
$data = $this->arrayRequestData();
42+
$dto = GetAllUserPostDto::build($data);
43+
44+
$this->assertEquals($dto->id, $data['id']);
45+
$this->assertEquals($dto->userId, $data['userId']);
46+
$this->assertEquals($dto->content, $data['content']);
47+
$this->assertEquals($dto->mediaPath, $data['mediaPath']);
48+
$this->assertEquals($dto->visibility, $data['visibility']);
49+
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Post\Application\Dto;
4+
5+
class GetAllUserPostDto
6+
{
7+
public function __construct(
8+
public readonly int $id,
9+
public readonly int $userId,
10+
public readonly string $content,
11+
public readonly ?string $mediaPath,
12+
public readonly string $visibility,
13+
) {}
14+
15+
public static function build(array $data): self
16+
{
17+
return new self(
18+
id: $data['id'],
19+
userId: $data['userId'],
20+
content: $data['content'],
21+
mediaPath: $data['mediaPath'] ?? null,
22+
visibility: $data['visibility'],
23+
);
24+
}
25+
26+
public function toArray(): array
27+
{
28+
return [
29+
'id' => $this->id,
30+
'userId' => $this->userId,
31+
'content' => $this->content,
32+
'mediaPath' => $this->mediaPath,
33+
'visibility' => $this->visibility
34+
];
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Post\Application\Dto;
4+
5+
use App\Post\Application\Dto\GetAllUserPostDto as PostDto;
6+
7+
class GetAllUserPostDtoCollection
8+
{
9+
/**
10+
* @param PostDto[] $items
11+
*/
12+
public function __construct(
13+
public readonly array $items
14+
) {}
15+
16+
public static function build(array $items): self
17+
{
18+
$postDtos = array_map(
19+
fn($item) => PostDto::build($item),
20+
$items
21+
);
22+
23+
return new self($postDtos);
24+
}
25+
26+
/**
27+
* Convert the collection to an array of DTOs.
28+
*
29+
* @return PostDto[]
30+
*/
31+
public function getPosts(): array
32+
{
33+
return $this->items;
34+
}
35+
}

src/app/Post/Application/QueryServiceInterface/GetAllUserPostQueryServiceInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace App\Post\Application\QueryServiceInterface;
44

55
use App\Post\Domain\Entity\PostEntityCollection;
6+
use App\Common\Application\Dto\Pagination as PaginationDto;
67

78
interface GetAllUserPostQueryServiceInterface
89
{
910
public function getAllUserPosts(
10-
int $userId
11-
): PostEntityCollection;
11+
int $userId,
12+
int $perPage,
13+
int $currentPage
14+
): PaginationDto;
1215
}

0 commit comments

Comments
 (0)