From 9d781a567021f6a6ccd46eb11458ee2fc1c11521 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Tue, 10 Jun 2025 07:54:30 +0900 Subject: [PATCH 01/80] add-show-post-index-collection --- .idea/workspace.xml | 35 +-------- .../DomainTest/PostEntityCollectionTest.php | 73 +++++++++++++++++++ .../Domain/Entity/PostEntityCollection.php | 63 ++++++++++++++++ 3 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 src/app/Post/Domain/DomainTest/PostEntityCollectionTest.php create mode 100644 src/app/Post/Domain/Entity/PostEntityCollection.php diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bbfd6fa..7e6bc08 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,39 +5,8 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + + + - - - + + + - - - + + + + - - + + + - - - - - - + + + + - diff --git a/src/app/Post/Application/QueryServiceInterface/GetAllUserPostQueryServiceInterface.php b/src/app/Post/Application/QueryServiceInterface/GetAllUserPostQueryServiceInterface.php index e775c3f..0310160 100644 --- a/src/app/Post/Application/QueryServiceInterface/GetAllUserPostQueryServiceInterface.php +++ b/src/app/Post/Application/QueryServiceInterface/GetAllUserPostQueryServiceInterface.php @@ -3,10 +3,13 @@ namespace App\Post\Application\QueryServiceInterface; use App\Post\Domain\Entity\PostEntityCollection; +use App\Common\Application\Dto\Pagination as PaginationDto; interface GetAllUserPostQueryServiceInterface { public function getAllUserPosts( - int $userId - ): PostEntityCollection; + int $userId, + int $perPage, + int $currentPage + ): PaginationDto; } \ No newline at end of file diff --git a/src/app/Post/Infrastructure/InfrastructureTest/GetAllUserPostQueryServiceTest.php b/src/app/Post/Infrastructure/InfrastructureTest/GetAllUserPostQueryServiceTest.php index 6ceee42..6f016df 100644 --- a/src/app/Post/Infrastructure/InfrastructureTest/GetAllUserPostQueryServiceTest.php +++ b/src/app/Post/Infrastructure/InfrastructureTest/GetAllUserPostQueryServiceTest.php @@ -2,6 +2,7 @@ namespace App\Post\Infrastructure\InfrastructureTest; +use App\Common\Application\Dto\Pagination; use App\Post\Domain\Entity\PostEntity; use App\Post\Infrastructure\QueryService\GetAllUserPostQueryService; use Illuminate\Support\Facades\DB; @@ -16,13 +17,18 @@ use App\Common\Domain\ValueObject\UserId; use App\Common\Domain\ValueObject\PostId; use App\Post\Domain\ValueObject\Postvisibility; +use Illuminate\Support\Carbon; class GetAllUserPostQueryServiceTest extends TestCase { private $user; private $queryService; + private $perPage = 15; + private $currentPage = 1; + private $post; + protected function setUp(): void { parent::setUp(); @@ -61,141 +67,80 @@ private function refresh(): void private function createDummyPosts(): void { - Post::insert([ - [ - 'user_id' => $this->user->id, - 'content' => 'ダミーポスト1', - 'media_path' => 'https://example.com/image1.jpg', - 'visibility' => 0, - 'created_at' => now(), - 'updated_at' => now(), - ], - [ - 'user_id' => $this->user->id, - 'content' => 'ダミーポスト2', - 'media_path' => null, - 'visibility' => 1, - 'created_at' => now(), - 'updated_at' => now(), - ], - ]); - } + $posts = []; + $baseTime = Carbon::now(); - private function mockEntityCollection(): PostEntityCollection - { - $entityCollection = Mockery::mock(PostEntityCollection::class); - - $entityCollection - ->shouldReceive('getAll') - ->andReturn($this->arrayTestData()); - - $entityCollection - ->shouldReceive('build') - ->andReturnUsing(function ($data) { - return Mockery::mock(PostFromModelEntityFactory::class) - ->shouldReceive('build') - ->with($data) - ->andReturn(Mockery::mock(PostFromModelEntityFactory::class)); - }); - - return $entityCollection; - } + for ($i = 1; $i <= 50; $i++) { + $time = $baseTime->copy()->addSeconds($i); - private function mockEntity(): PostEntity - { - $entity = Mockery::mock(PostEntity::class); - - $entity - ->shouldReceive('getId') - ->andReturn($this->arrayTestData()[0]['id']); - - $entity - ->shouldReceive('getMediaPath') - ->andReturn($this->arrayTestData()[0]['media_path']); - - $entity - ->shouldReceive('getContent') - ->andReturn($this->arrayTestData()[0]['content']); - - $entity - ->shouldReceive('getUserId') - ->andReturn($this->arrayTestData()[0]['user_id']); - - $entity - ->shouldReceive('getPostVisibility') - ->andReturn(PostVisibilityEnum::from($this->arrayTestData()[0]['visibility'])); + $posts[] = [ + 'user_id' => $this->user->id, + 'content' => "ダミーポスト{$i}", + 'media_path' => $i % 2 === 0 ? null : "https://example.com/image{$i}.jpg", + 'visibility' => $i % 2 === 0 ? 1 : 0, + 'created_at' => $time, + 'updated_at' => $time, + ]; + } - return $entity; + Post::insert($posts); } - private function arrayTestData(): array + public function test_check_pagination_type(): void { - return [ - [ - 'id' => 1, - 'user_id' => $this->user->id, - 'content' => 'Test post content', - 'media_path' => 'https://example.com/media/test.jpg', - 'visibility' => PostVisibilityEnum::PUBLIC->value - ], - [ - 'id' => 2, - 'user_id' => $this->user->id, - 'content' => 'Another test post content', - 'media_path' => null, - 'visibility' => PostVisibilityEnum::PRIVATE->value - ] - ]; + $result = $this->queryService->getAllUserPosts( + $this->user->id, + $this->perPage, + $this->currentPage + ); + + $this->assertInstanceOf( + Pagination::class, + $result + ); } - private function mockPostFromEntity(): PostEntity + public function test_check_pagination_data_value(): void { - $factory = Mockery::mock( - 'alias' . PostFromModelEntityFactory::class + $result = $this->queryService->getAllUserPosts( + $this->user->id, + $this->perPage, + $this->currentPage ); - $entity = Mockery::mock(PostEntity::class); - - $factory - ->shouldReceive('buildFromModel') - ->andReturn($entity); - - $entity - ->shouldReceive('getId') - ->andReturn(new PostId($this->arrayTestData()[0]['id'])); - - $entity - ->shouldReceive('getUserId') - ->andReturn(new UserId($this->arrayTestData()[0]['user_id'])); - - $entity - ->shouldReceive('getContent') - ->andReturn($this->arrayTestData()[0]['content']); - - $entity - ->shouldReceive('getMediaPath') - ->andReturn($this->arrayTestData()[0]['media_path']); - - $entity - ->shouldReceive('getPostVisibility') - ->andReturn(new Postvisibility( - PostVisibilityEnum::from((int) $this->arrayTestData()[0]['visibility']) - )); - - return $entity; + foreach ($result->getData() as $post) { + $this->assertInstanceOf( + PostEntity::class, + $post + ); + } } - public function test_get_all_post_check_type(): void + public function test_check_pagination_current_page_value(): void { - $result = $this->queryService->getAllUserPosts($this->user->id); + $result = $this->queryService->getAllUserPosts( + $this->user->id, + $this->perPage, + $this->currentPage = 30 + ); - $this->assertInstanceOf(PostEntityCollection::class, $result); + $this->assertEquals( + $this->currentPage, + $result->getCurrentPage() + ); } - public function test_get_all_post_check_value(): void + public function test_check_pagination_per_page_value(): void { - $result = $this->queryService->getAllUserPosts($this->user->id); + $result = $this->queryService->getAllUserPosts( + $this->user->id, + $this->perPage = 20, + $this->currentPage + ); - $this->assertCount(2, $result->getPosts()); + $this->assertEquals( + $this->perPage, + $result->getPerPage() + ); } } \ No newline at end of file diff --git a/src/app/Post/Infrastructure/QueryService/GetAllUserPostQueryService.php b/src/app/Post/Infrastructure/QueryService/GetAllUserPostQueryService.php index 078361a..f961bb8 100644 --- a/src/app/Post/Infrastructure/QueryService/GetAllUserPostQueryService.php +++ b/src/app/Post/Infrastructure/QueryService/GetAllUserPostQueryService.php @@ -6,6 +6,7 @@ use App\Post\Application\QueryServiceInterface\GetAllUserPostQueryServiceInterface; use App\Post\Domain\Entity\PostEntityCollection; use App\Post\Domain\EntityFactory\PostFromModelEntityFactory; +use App\Common\Application\Dto\Pagination as PaginationDto; class GetAllUserPostQueryService implements GetAllUserPostQueryServiceInterface { @@ -13,14 +14,48 @@ public function __construct( private readonly Post $post ) {} - public function getAllUserPosts(int $userId): PostEntityCollection + public function getAllUserPosts( + int $userId, + int $perPage, + int $currentPage, + ): PaginationDto { - $userPosts = $this->post->where('user_id', $userId)->get(); + $userPosts = $this->post + ->where('user_id', $userId) + ->orderBy('created_at', 'desc') + ->paginate( + $perPage, + ['*'], + 'page', + $currentPage + ); - $postEntities = $userPosts->map(function ($post) { - return PostFromModelEntityFactory::build($post->toArray()); - }); + return $this->paginationDto($userPosts->toArray()); + } + + private function paginationDto( + array $data + ): PaginationDto + { + $dtos = array_map( + fn($item) => PostFromModelEntityFactory::build($item), + $data['data'] + ); - return new PostEntityCollection($postEntities->all()); + return new PaginationDto( + $dtos, + currentPage: $data['current_page'] ?? null, + from: $data['from'] ?? null, + to: $data['to'] ?? null, + perPage: $data['per_page'] ?? null, + path: $data['path'] ?? null, + lastPage: $data['last_page'] ?? null, + total: $data['total'] ?? null, + firstPageUrl: $data['first_page_url'] ?? null, + lastPageUrl: $data['last_page_url'] ?? null, + nextPageUrl: $data['next_page_url'] ?? null, + prevPageUrl: $data['prev_page_url'] ?? null, + links: $data['links'] ?? null + ); } } \ No newline at end of file From 11d7a91add0a014e6fd95ffeb0a882827cedb17d Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sat, 14 Jun 2025 13:56:42 +0900 Subject: [PATCH 09/80] fix: delete-useless-helper&change-property-primitive-type --- .idea/workspace.xml | 7 +------ src/app/Common/Application/Dto/Pagination.php | 4 ++-- .../QueryService/GetAllUserPostQueryService.php | 1 - 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 52ef24e..19982a8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,12 +4,7 @@ - - - - - - + - + + + + + + + - - - - + + - - + + - - + + - - + - - - - + - + + + + + + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - - - - - + + - - - - -