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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Packages\Features\QueryUseCases\Tests\UseCase;

use App\Packages\Domains\Interface\UserRepositroyInterface;
use App\Packages\Features\QueryUseCases\Dto\User\UserDto;
use App\Packages\Features\QueryUseCases\UseCase\User\GetUserByIdUseCase;
use Exception;
use Faker\Factory as FakerFactory;
use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;

class GetUserByIdUseCaseTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}

private function buildDto(): UserDto
{
$faker = FakerFactory::create();

return new UserDto(
id: $faker->unique()->randomNumber(5),
firstName: $faker->firstName(),
lastName: $faker->lastName(),
email: $faker->unique()->safeEmail(),
ageRange: 'teens',
subscriptionTier: 'free',
subscriptionExpiresAt: null,
);
}

public function test_handle_returns_user_dto_when_user_exists(): void
{
$dto = $this->buildDto();

/** @var UserRepositroyInterface|MockInterface $repository */
$repository = Mockery::mock(UserRepositroyInterface::class);
$repository->shouldReceive('findById')
->once()
->with($dto->id)
->andReturn($dto);

$result = (new GetUserByIdUseCase($repository))->handle($dto->id);

$this->assertSame($dto, $result);
}

public function test_handle_throws_exception_when_user_not_found(): void
{
/** @var UserRepositroyInterface|MockInterface $repository */
$repository = Mockery::mock(UserRepositroyInterface::class);
$repository->shouldReceive('findById')
->once()
->with(999)
->andReturn(null);

$this->expectException(Exception::class);
$this->expectExceptionMessage('User not found.');

(new GetUserByIdUseCase($repository))->handle(999);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Packages\Features\QueryUseCases\UseCase\User;

use App\Packages\Domains\Interface\UserRepositroyInterface;
use App\Packages\Features\QueryUseCases\Dto\User\UserDto;
use Exception;

final class GetUserByIdUseCase
{
public function __construct(
private readonly UserRepositroyInterface $userRepository,
) {}

public function handle(int $id): UserDto
{
$userDto = $this->userRepository->findById($id);

if ($userDto === null) {
throw new Exception('User not found.');
}

return $userDto;
}
}
Loading