diff --git a/src/app/Post/Presentation/Controller/PostController.php b/src/app/Post/Presentation/Controller/PostController.php index 64cbc8c..3b2272b 100644 --- a/src/app/Post/Presentation/Controller/PostController.php +++ b/src/app/Post/Presentation/Controller/PostController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Post\Application\UseCase\CreateUseCase; +use App\Post\Application\UseCase\GetOthersAllPostsUseCase; use App\Post\Application\UseCase\GetUserEachPostUseCase; use App\Post\Presentation\ViewModel\CreatePostViewModel; use App\Post\Application\UseCommand\CreatePostUseCommand; @@ -152,4 +153,45 @@ public function edit( ], 500); } } + + public function getOthersPosts( + Request $request, + int $userId, + GetOthersAllPostsUseCase $useCase + ): JsonResponse + { + try { + $userId = $request->route('userId', $userId); + $perPage = $request->get('per_page', 15); + $currentPage = $request->get('current_page', 1); + + $data = $useCase->handle( + userId: $userId, + perPage: $perPage, + currentPage: $currentPage + ); + + $viewModels = array_map( + fn(GetUserEachPostDto $dto) => GetAllUserPostViewModel::build($dto)->toArray(), + $data->getData() + ); + + $paginationViewModel = PaginationViewModelFactory::build( + $data, + $viewModels + )->toArray(); + + return response()->json([ + 'status' => 'success', + 'data' => $paginationViewModel['data'], + 'meta' => $paginationViewModel['meta'], + ], 200); + + } catch (Throwable $e) { + return response()->json([ + 'status' => 'error', + 'message' => $e->getMessage(), + ], 500); + } + } } diff --git a/src/app/Post/Presentation/PresentationTest/Controller/PostController_getOthersPostsTest.php b/src/app/Post/Presentation/PresentationTest/Controller/PostController_getOthersPostsTest.php new file mode 100644 index 0000000..3105cbe --- /dev/null +++ b/src/app/Post/Presentation/PresentationTest/Controller/PostController_getOthersPostsTest.php @@ -0,0 +1,87 @@ +currentPage = 1; + $this->perPage = 10; + $this->controller = new PostController(); + } + + protected function tearDown(): void + { + parent::tearDown(); + } + + private function mockUseCase(): GetOthersAllPostsUseCase + { + $useCase = Mockery::mock(GetOthersAllPostsUseCase::class); + + $useCase->shouldReceive('handle') + ->with( + Mockery::type('int'), + Mockery::type($this->perPage), + Mockery::type($this->currentPage) + ) + ->andReturn($this->mockPagination()); + + return $useCase; + } + + private function mockPagination(): PaginationDto + { + $paginationDto = Mockery::mock(PaginationDto::class); + + $paginationDto->shouldReceive('getCurrentPage') + ->andReturn($this->currentPage); + + $paginationDto->shouldReceive('getPerPage') + ->andReturn($this->perPage); + + return $paginationDto; + } + + private function mockRequest(): Request + { + $request = Mockery::mock(Request::class); + + $request->shouldReceive('input') + ->with('perPage') + ->andReturn($this->perPage); + + $request->shouldReceive('input') + ->with('currentPage') + ->andReturn($this->currentPage); + + return $request; + } + + public function test_controller_type_check(): void + { + $result = $this->controller->getOthersPosts( + $this->mockRequest(), + 1, + $this->mockUseCase() + ); + + $this->assertInstanceOf(JsonResponse::class, $result); + } +} \ No newline at end of file diff --git a/src/app/Post/Presentation/PresentationTest/GetPostViewModelCollectionTest.php b/src/app/Post/Presentation/PresentationTest/GetPostViewModelCollectionTest.php index 0eb9bde..f8cc774 100644 --- a/src/app/Post/Presentation/PresentationTest/GetPostViewModelCollectionTest.php +++ b/src/app/Post/Presentation/PresentationTest/GetPostViewModelCollectionTest.php @@ -84,31 +84,6 @@ private function arrayData(): array ]; } - private function mockEntityCollection(): PostEntityCollection - { - $collection = Mockery::mock(PostEntityCollection::class); - - $collection->shouldReceive('getPosts') - ->andReturn([ - new GetPostViewModel( - 1, - 1, - 'Sample content', - 'https://example.com/media.jpg', - 'public' - ), - new GetPostViewModel( - 2, - 1, - 'Another content', - 'https://example.com/another_media.jpg', - 'private' - ) - ]); - - return $collection; - } - public function test_view_model_collection_check_type(): void { $collection = new GetPostsViewModelCollection(