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
8 changes: 6 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 src/app/Post/Application/ApplicationTest/GetAllUserPostDtoTest.php
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 src/app/Post/Application/ApplicationTest/GetAllUserPostUseCaseTest.php
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,
Copy link

Copilot AI Jun 14, 2025

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.

'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);
}
}
36 changes: 36 additions & 0 deletions src/app/Post/Application/Dto/GetAllUserPostDto.php
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 src/app/Post/Application/Dto/GetAllUserPostDtoCollection.php
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 src/app/Post/Application/UseCase/GetAllUserPostUseCase.php
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
);
}
}