Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/app/Post/Presentation/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Post\Presentation\PresentationTest\Controller;

use App\Post\Application\UseCase\GetOthersAllPostsUseCase;
use App\Post\Presentation\Controller\PostController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Tests\TestCase;
use Mockery;
use App\Common\Application\Dto\Pagination as PaginationDto;

class PostController_getOthersPostsTest extends TestCase
{

private $currentPage;
private $perPage;

private $controller;

protected function setUp(): void
{
parent::setUp();
$this->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),
Copy link

Copilot AI Jul 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Mockery::type($this->perPage) will not match the integer type; use Mockery::type('int') or a more specific matcher instead.

Suggested change
Mockery::type($this->perPage),
Mockery::type('int'),

Copilot uses AI. Check for mistakes.
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')
Copy link

Copilot AI Jul 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test mocks input('perPage') but the controller uses get('per_page'); update the mock to input('per_page') or align the parameter naming between test and controller.

Suggested change
->with('perPage')
->with('per_page')

Copilot uses AI. Check for mistakes.
->andReturn($this->perPage);

$request->shouldReceive('input')
->with('currentPage')
Copy link

Copilot AI Jul 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test mocks input('currentPage') but the controller uses get('current_page'); update the mock to input('current_page') or make naming consistent.

Suggested change
->with('currentPage')
->with('current_page')

Copilot uses AI. Check for mistakes.
->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down