From 9df28c9308c8eb75732c5aa16fe211ee710d42d3 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sat, 21 Jun 2025 18:34:52 +0900 Subject: [PATCH] feat: add-conttroller-edit --- .../Controller/PostController.php | 38 +++++ .../Controller/PostController_editTest.php | 137 ++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 src/app/Post/Presentation/PresentationTest/Controller/PostController_editTest.php diff --git a/src/app/Post/Presentation/Controller/PostController.php b/src/app/Post/Presentation/Controller/PostController.php index c8d066a..1410b9e 100644 --- a/src/app/Post/Presentation/Controller/PostController.php +++ b/src/app/Post/Presentation/Controller/PostController.php @@ -4,8 +4,11 @@ use App\Http\Controllers\Controller; use App\Post\Application\UseCase\CreateUseCase; +use App\Post\Application\UseCase\EditUseCase; +use App\Post\Application\UseCommand\EditPostUseCommand; use App\Post\Presentation\ViewModel\CreatePostViewModel; use App\Post\Application\UseCommand\CreatePostUseCommand; +use App\Post\Presentation\ViewModel\EditPostViewModel; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -45,4 +48,39 @@ public function create( ], 500); } } + + public function edit( + Request $request, + int $user_id, + int $post_id, + EditUseCase $useCase + ): JsonResponse { + DB::connection('mysql')->beginTransaction(); + try { + + $command = EditPostUseCommand::build( + array_merge( + $request->toArray(), + ['user_id' => $user_id], + ['post_id' => $post_id] + ) + ); + + $dto = $useCase->handle($command); + $viewModel = new EditPostViewModel($dto); + + DB::connection('mysql')->commit(); + + return response()->json([ + 'status' => 'success', + 'data' => $viewModel->toArray(), + ], 200); + } catch (Throwable $e) { + DB::connection('mysql')->rollBack(); + return response()->json([ + 'status' => 'error', + 'message' => $e->getMessage(), + ], 500); + } + } } diff --git a/src/app/Post/Presentation/PresentationTest/Controller/PostController_editTest.php b/src/app/Post/Presentation/PresentationTest/Controller/PostController_editTest.php new file mode 100644 index 0000000..4bfff27 --- /dev/null +++ b/src/app/Post/Presentation/PresentationTest/Controller/PostController_editTest.php @@ -0,0 +1,137 @@ +controller = new PostController(); + $this->userId = 1; + } + + protected function tearDown(): void + { + parent::tearDown(); + } + + private function mockUseCommand(): EditPostUseCommand + { + $command = Mockery::mock(EditPostUseCommand::class); + + $command + ->shouldReceive('getId') + ->andReturn($this->arrayData()['id']); + + $command + ->shouldReceive('getUserId') + ->andReturn($this->arrayData()['userId']); + + $command + ->shouldReceive('getContent') + ->andReturn($this->arrayData()['content']); + + $command + ->shouldReceive('getMediaPath') + ->andReturn($this->arrayData()['mediaPath']); + + $command + ->shouldReceive('getVisibility') + ->andReturn(new Postvisibility(PostVisibilityEnum::fromString($this->arrayData()['visibility']))); + + $command + ->shouldReceive('toArray') + ->andReturn($this->arrayData()); + + return $command; + } + + private function mockDto(): EditPostDto + { + $dto = Mockery::mock(EditPostDto::class); + + $dto + ->shouldReceive('getId') + ->andReturn(new PostId($this->arrayData()['id'])); + + $dto + ->shouldReceive('getUserId') + ->andReturn(new UserId(1)); + + $dto + ->shouldReceive('getContent') + ->andReturn($this->arrayData()['content']); + + $dto + ->shouldReceive('getMediaPath') + ->andReturn($this->arrayData()['mediaPath']); + + $dto + ->shouldReceive('getVisibility') + ->andReturn(new Postvisibility(PostVisibilityEnum::fromString($this->arrayData()['visibility']))); + + return $dto; + } + + private function mockUseCase(): EditUseCase + { + $useCase = Mockery::mock(EditUseCase::class); + + $useCase + ->shouldReceive('handle') + ->with(Mockery::type(EditPostUseCommand::class)) + ->andReturn($this->mockDto()); + + return $useCase; + } + + private function arrayData(): array + { + return [ + 'id' => 1, + 'userId' => $this->userId, + 'content' => 'Updated content', + 'mediaPath' => 'https://example.com/updated_media.jpg', + 'visibility' => 'public', + ]; + } + + private function mockRequest(): Request + { + $request = Mockery::mock(Request::class); + + $request + ->shouldReceive('all') + ->andReturn($this->arrayData()); + + return $request; + } + + public function test_controller(): void + { + $result = $this->controller->edit( + $this->mockRequest(), + $this->userId, + $this->arrayData()['id'], + $this->mockUseCase() + ); + + $this->assertInstanceOf(JsonResponse::class, $result); + } +} \ No newline at end of file