-
Notifications
You must be signed in to change notification settings - Fork 0
Merge GetAllUserPost UseCase and DTO & DtoCollection #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zigzagdev
merged 4 commits into
feature/show-post-index
from
feature/show-post-index-application
Jun 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfd8c96
feat: add-post-dto_collection
zigzagdev e80f811
Merge pull request #209 from zigzagdev/feautre/show-post-index-applic…
zigzagdev d27d0d6
feat: add-show-post-index-usecase
zigzagdev 0807467
Merge pull request #210 from zigzagdev/feature/show-post-index-applic…
zigzagdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/app/Post/Application/ApplicationTest/GetAllUserPostDtoCollectionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\ApplicationTest; | ||
|
||
use Tests\TestCase; | ||
use App\Post\Application\Dto\GetAllUserPostDtoCollection; | ||
|
||
class GetAllUserPostDtoCollectionTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
private function arrayRequestData(): array | ||
{ | ||
return [ | ||
[ | ||
'id' => 1, | ||
'userId' => 2, | ||
'content' => 'Sample post content', | ||
'mediaPath' => 'path/to/media.jpg', | ||
'visibility' => 'public', | ||
], | ||
[ | ||
'id' => 2, | ||
'userId' => 3, | ||
'content' => 'Another post content', | ||
'mediaPath' => 'path/to/another_media.jpg', | ||
'visibility' => 'private', | ||
], | ||
]; | ||
} | ||
|
||
public function test_get_all_user_post_dto_collection(): void | ||
{ | ||
$data = $this->arrayRequestData(); | ||
$dtoCollection = GetAllUserPostDtoCollection::build($data); | ||
|
||
$this->assertInstanceOf(GetAllUserPostDtoCollection::class, $dtoCollection); | ||
} | ||
|
||
public function test_get_all_user_post_dto_collection_values(): void | ||
{ | ||
$data = $this->arrayRequestData(); | ||
$dtoCollection = GetAllUserPostDtoCollection::build($data); | ||
|
||
$this->assertCount(count($data), $dtoCollection->getPosts()); | ||
|
||
foreach ($data as $index => $item) { | ||
$dto = $dtoCollection->getPosts()[$index]; | ||
$this->assertEquals($dto->id, $item['id']); | ||
$this->assertEquals($dto->userId, $item['userId']); | ||
$this->assertEquals($dto->content, $item['content']); | ||
$this->assertEquals($dto->mediaPath, $item['mediaPath']); | ||
$this->assertEquals($dto->visibility, $item['visibility']); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/app/Post/Application/ApplicationTest/GetAllUserPostDtoTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\ApplicationTest; | ||
|
||
use Tests\TestCase; | ||
use App\Post\Application\Dto\GetAllUserPostDto; | ||
|
||
class GetAllUserPostDtoTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
private function arrayRequestData(): array | ||
{ | ||
return [ | ||
'id' => 1, | ||
'userId' => 2, | ||
'content' => 'Sample post content', | ||
'mediaPath' => 'path/to/media.jpg', | ||
'visibility' => 'public', | ||
]; | ||
} | ||
|
||
public function test_get_all_user_post_dto() : void | ||
{ | ||
$data = $this->arrayRequestData(); | ||
$dto = GetAllUserPostDto::build($data); | ||
|
||
$this->assertInstanceOf(GetAllUserPostDto::class, $dto); | ||
} | ||
|
||
public function test_get_all_user_post_dto_values() : void | ||
{ | ||
$data = $this->arrayRequestData(); | ||
$dto = GetAllUserPostDto::build($data); | ||
|
||
$this->assertEquals($dto->id, $data['id']); | ||
$this->assertEquals($dto->userId, $data['userId']); | ||
$this->assertEquals($dto->content, $data['content']); | ||
$this->assertEquals($dto->mediaPath, $data['mediaPath']); | ||
$this->assertEquals($dto->visibility, $data['visibility']); | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
src/app/Post/Application/ApplicationTest/GetAllUserPostUseCaseTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\ApplicationTest; | ||
|
||
use App\Common\Domain\ValueObject\UserId; | ||
use App\Post\Application\QueryServiceInterface\GetAllUserPostQueryServiceInterface; | ||
use Tests\TestCase; | ||
use Mockery; | ||
use App\Post\Application\Dto\GetAllUserPostDto; | ||
use App\Post\Application\Dto\GetAllUserPostDtoCollection; | ||
use App\Common\Application\Dto\Pagination; | ||
use App\Common\Domain\ValueObject\PostId; | ||
use App\Post\Application\UseCase\GetAllUserPostUseCase; | ||
|
||
class GetAllUserPostUseCaseTest extends TestCase | ||
{ | ||
private $userId; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
private function arrayRequestData(): array | ||
{ | ||
return [ | ||
[ | ||
'id' => 1, | ||
'userId' => $this->userId, | ||
'content' => 'Sample post content', | ||
'mediaPath' => 'path/to/media.jpg', | ||
'visibility' => 'public', | ||
], | ||
[ | ||
'id' => 2, | ||
'userId' => $this->userId, | ||
'content' => 'Another post content', | ||
'mediaPath' => 'path/to/another_media.jpg', | ||
'visibility' => 'private', | ||
], | ||
]; | ||
} | ||
|
||
private function mockQueryService(): GetAllUserPostQueryServiceInterface | ||
{ | ||
$mock = Mockery::mock(GetAllUserPostQueryServiceInterface::class); | ||
|
||
$mock->shouldReceive('getAllUserPosts') | ||
->with( | ||
Mockery::on('is_int'), | ||
Mockery::on('is_int'), | ||
Mockery::on('is_int') | ||
) | ||
->andReturn($this->mockPagination()); | ||
|
||
return $mock; | ||
} | ||
|
||
private function mockPagination(): Pagination | ||
{ | ||
$mock = Mockery::mock(Pagination::class); | ||
|
||
$mock->shouldReceive('getCurrentPage') | ||
->andReturn(1); | ||
|
||
$mock->shouldReceive('getPerPage') | ||
->andReturn(10); | ||
|
||
$mock->shouldReceive('getTotal') | ||
->andReturn(2); | ||
|
||
$mock->shouldReceive('getItems') | ||
->andReturn($this->mockDtoCollection()->getPosts()); | ||
|
||
return $mock; | ||
} | ||
|
||
private function mockDtoCollection(): GetAllUserPostDtoCollection | ||
{ | ||
$mock = Mockery::mock(GetAllUserPostDtoCollection::class); | ||
|
||
$mock->shouldReceive('getPosts') | ||
->andReturn(array_map(function ($data) { | ||
return $this->mockDto(); | ||
}, $this->arrayRequestData())); | ||
|
||
return $mock; | ||
} | ||
|
||
private function mockDto(): GetAllUserPostDto | ||
{ | ||
$mock = Mockery::mock(GetAllUserPostDto::class); | ||
|
||
$mock->shouldReceive('getId') | ||
->andReturn(new PostId($this->arrayRequestData()[0]['id'])); | ||
|
||
$mock->shouldReceive('getUserId') | ||
->andReturn(new UserId($this->arrayRequestData()[0]['userId'])); | ||
|
||
$mock->shouldReceive('getContent') | ||
->andReturn($this->arrayRequestData()[0]['content']); | ||
|
||
$mock->shouldReceive('getMediaPath') | ||
->andReturn($this->arrayRequestData()[0]['mediaPath']); | ||
|
||
$mock->shouldReceive('getPostVisibility') | ||
->andReturn($this->arrayRequestData()[0]['visibility']); | ||
|
||
return $mock; | ||
} | ||
|
||
public function test_use_case_check(): void | ||
{ | ||
$queryService = $this->mockQueryService(); | ||
|
||
$useCase = new GetAllUserPostUseCase($queryService); | ||
|
||
$result = $useCase->handle( | ||
$this->arrayRequestData()[0]['id'], | ||
1, | ||
10 | ||
); | ||
|
||
$this->assertInstanceOf(Pagination::class, $result); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\Dto; | ||
|
||
class GetAllUserPostDto | ||
{ | ||
public function __construct( | ||
public readonly int $id, | ||
public readonly int $userId, | ||
public readonly string $content, | ||
public readonly ?string $mediaPath, | ||
public readonly string $visibility, | ||
) {} | ||
|
||
public static function build(array $data): self | ||
{ | ||
return new self( | ||
id: $data['id'], | ||
userId: $data['userId'], | ||
content: $data['content'], | ||
mediaPath: $data['mediaPath'] ?? null, | ||
visibility: $data['visibility'], | ||
); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'userId' => $this->userId, | ||
'content' => $this->content, | ||
'mediaPath' => $this->mediaPath, | ||
'visibility' => $this->visibility | ||
]; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/app/Post/Application/Dto/GetAllUserPostDtoCollection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\Dto; | ||
|
||
use App\Post\Application\Dto\GetAllUserPostDto as PostDto; | ||
|
||
class GetAllUserPostDtoCollection | ||
{ | ||
/** | ||
* @param PostDto[] $items | ||
*/ | ||
public function __construct( | ||
public readonly array $items | ||
) {} | ||
|
||
public static function build(array $items): self | ||
{ | ||
$postDtos = array_map( | ||
fn($item) => PostDto::build($item), | ||
$items | ||
); | ||
|
||
return new self($postDtos); | ||
} | ||
|
||
/** | ||
* Convert the collection to an array of DTOs. | ||
* | ||
* @return PostDto[] | ||
*/ | ||
public function getPosts(): array | ||
{ | ||
return $this->items; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/app/Post/Application/UseCase/GetAllUserPostUseCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Post\Application\UseCase; | ||
|
||
use App\Common\Domain\ValueObject\UserId; | ||
use App\Common\Application\Dto\Pagination; | ||
use App\Post\Application\QueryServiceInterface\GetAllUserPostQueryServiceInterface; | ||
|
||
class GetAllUserPostUseCase | ||
{ | ||
public function __construct( | ||
private readonly GetAllUserPostQueryServiceInterface $queryService | ||
) {} | ||
|
||
public function handle( | ||
int $userId, | ||
int $perPage, | ||
int $currentPage | ||
): Pagination | ||
{ | ||
return $this->queryService->getAllUserPosts( | ||
$userId, | ||
$perPage, | ||
$currentPage | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The property $this->userId is used in the test data but never initialized, which might lead to unexpected behavior or errors during test execution. Consider initializing $this->userId in the setUp method.
Copilot uses AI. Check for mistakes.