-
Notifications
You must be signed in to change notification settings - Fork 0
Integrate infrastructure layer for post query services #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zigzagdev
merged 5 commits into
feature/see-other-posts
from
feature/see-other-posts-infra
Jul 5, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00fde8d
fix: Enum-relation-fix
zigzagdev 2246e87
feat: make-get-post-query-service-infra
zigzagdev 2cffe8d
feat: make-test
zigzagdev 40b370d
Merge pull request #336 from zigzagdev/feature/see-other-posts-infra-…
zigzagdev 20760c7
fix: argument-perPage_currentPage-change
zigzagdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/app/Post/Infrastructure/InfrastructureTest/GetOthersAllPostsQueryServiceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace App\Post\Infrastructure\InfrastructureTest; | ||
|
||
use App\Post\Domain\Entity\PostEntity; | ||
use App\Post\Infrastructure\QueryService\GetPostQueryService; | ||
use Tests\TestCase; | ||
use App\Post\Application\QueryServiceInterface\GetPostQueryServiceInterface; | ||
use App\Common\Application\DtoFactory\PaginationFactory; | ||
use App\Common\Application\Dto\Pagination as PaginationDto; | ||
use Mockery; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Models\User; | ||
use App\Models\Post; | ||
|
||
class GetOthersAllPostsQueryServiceTest extends TestCase | ||
{ | ||
|
||
private $user; | ||
private int $currentPage = 1; | ||
private int $perPage = 10; | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->refresh(); | ||
$this->user = $this->createUsersWithPosts(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->refresh(); | ||
parent::tearDown(); | ||
} | ||
|
||
private function refresh(): void | ||
{ | ||
if (env('APP_ENV') === 'testing') { | ||
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;'); | ||
User::truncate(); | ||
Post::truncate(); | ||
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;'); | ||
} | ||
} | ||
|
||
private function createUsersWithPosts(): User | ||
{ | ||
$users = []; | ||
|
||
for ($i = 1; $i <= 3; $i++) { | ||
$user = User::create([ | ||
'first_name' => "User{$i}", | ||
'last_name' => "Test{$i}", | ||
'email' => "user{$i}@example.com", | ||
'password' => bcrypt('password123'), | ||
'bio' => "This is user {$i}", | ||
'location' => "City{$i}", | ||
'skills' => ['Laravel', 'Vue'], | ||
'profile_image' => 'https://example.com/user.jpg', | ||
]); | ||
|
||
$visibility = $i % 2 === 0 ? 0 : 1; | ||
for ($j = 1; $j <= 20; $j++) { | ||
Post::create([ | ||
'user_id' => $user->id, | ||
'content' => "Post {$j} by User{$i}", | ||
'media_path' => null, | ||
'visibility' => $visibility, | ||
'created_at' => now()->subMinutes(rand(0, 1000)), | ||
'updated_at' => now(), | ||
]); | ||
} | ||
|
||
$users[] = $user; | ||
} | ||
|
||
return $users[0]; | ||
} | ||
|
||
public function test_check_query_service_return_type(): void | ||
{ | ||
$queryService = new GetPostQueryService( | ||
new Post(), | ||
new User(), | ||
); | ||
|
||
$result = $queryService->getOthersAllPosts( | ||
$this->user->id, | ||
$this->perPage, | ||
$this->currentPage, | ||
); | ||
|
||
$this->assertInstanceOf( | ||
PaginationDto::class, | ||
$result | ||
); | ||
} | ||
|
||
public function test_check_query_service_return_value(): void | ||
{ | ||
$queryService = new GetPostQueryService( | ||
new Post(), | ||
new User(), | ||
); | ||
|
||
$result = $queryService->getOthersAllPosts( | ||
$this->user->id, | ||
$this->perPage, | ||
$this->currentPage, | ||
); | ||
|
||
$this->assertEquals($this->currentPage, $result->getCurrentPage()); | ||
$this->assertEquals($this->perPage, $result->getPerPage()); | ||
foreach ($result->getData() as $post) { | ||
$this->assertInstanceOf( | ||
PostEntity::class, | ||
$post | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||||
use App\Common\Application\Dto\Pagination as PaginationDto; | ||||||
use App\Models\User; | ||||||
use ErrorException; | ||||||
use App\Common\Domain\Enum\PostVisibility as PostVisibilityEnum; | ||||||
|
||||||
class GetPostQueryService implements GetPostQueryServiceInterface | ||||||
{ | ||||||
|
@@ -84,4 +85,27 @@ private function paginationDto( | |||||
links: $data['links'] ?? null | ||||||
); | ||||||
} | ||||||
|
||||||
public function getOthersAllPosts( | ||||||
int $userId, | ||||||
int $perPage, | ||||||
int $currentPage | ||||||
): ?PaginationDto { | ||||||
|
||||||
$allPosts = $this->post | ||||||
->where('user_id', '!=', $userId) | ||||||
->where('visibility', PostVisibilityEnum::PUBLIC->value) | ||||||
->paginate( | ||||||
$perPage, | ||||||
['*'], | ||||||
'page', | ||||||
$currentPage | ||||||
); | ||||||
|
||||||
if (empty($allPosts)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using empty() on a LengthAwarePaginator will always return false; use $allPosts->isEmpty() to detect when no records are returned.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return null; | ||||||
} | ||||||
|
||||||
return $this->paginationDto($allPosts->toArray()); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default
PostVisibilityEnum::PUBLIC
is an enum instance, not its backing value; usePostVisibilityEnum::PUBLIC->value
or ensure only an int is passed tofrom()
.Copilot uses AI. Check for mistakes.