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
Expand Up @@ -4,6 +4,7 @@

use App\Packages\Domains\User\Interface\UserRepositroyInterface;
use App\Packages\Domains\User\Factory\UserEntityFactory;
use App\Packages\Domains\User\Subscription\SubscriptionTier;
use App\Packages\Features\CommandUseCases\UseCommand\User\CreateUserCommand;
use App\Packages\Features\QueryUseCases\Dto\User\UserDto;

Expand All @@ -21,7 +22,7 @@ public function handle(CreateUserCommand $command): UserDto
'last_name' => $command->lastName,
'email' => $command->email,
'age_range' => $command->ageRange,
'subscription_tier' => $command->subscriptionTier,
'subscription_tier' => $command->subscriptionTier ?? SubscriptionTier::Free->value,
'subscription_expires_at' => $command->subscriptionExpiresAt,
'password_hash' => bcrypt($command->password),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ final class CreateUserCommand
'email',
'password',
'age_range',
'subscription_tier',
];

private function __construct(
Expand All @@ -21,7 +20,7 @@ private function __construct(
public readonly string $email,
public readonly string $password,
public readonly string $ageRange,
public readonly string $subscriptionTier,
public readonly ?string $subscriptionTier,
public readonly ?string $subscriptionExpiresAt,
)
{
Expand All @@ -41,7 +40,7 @@ public static function fromArray(array $data): self
email: $data['email'],
password: $data['password'],
ageRange: $data['age_range'],
subscriptionTier: $data['subscription_tier'],
subscriptionTier: $data['subscription_tier'] ?? null,
subscriptionExpiresAt: $data['subscription_expires_at'] ?? null,
);
}
Expand Down
51 changes: 26 additions & 25 deletions src/app/Packages/Features/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@

class UserController extends Controller
{
public function createUser(
Request $request,
CreateUserUseCase $useCase,
): JsonResponse {
try {
$command = CreateUserCommand::fromArray($request->all());

$dto = $useCase->handle($command);

return response()->json([
'status' => 'success',
'data' => UserViewModelFactory::build($dto)->toArray(),
], 201);
} catch (\Throwable $throwable) {
Log::error('Failed to create user', [
'message' => $throwable->getMessage(),
'trace' => $throwable->getTraceAsString(),
]);

return response()->json([
'status' => 'error',
'message' => 'Internal Server Error',
], 500);
}
}

public function getUserById(
Request $request,
GetUserByIdUseCase $useCase,
Expand Down Expand Up @@ -111,29 +137,4 @@ public function deleteUser(
], 500);
}
}

public function createUser(
Request $request,
CreateUserUseCase $useCase,
): JsonResponse {
try {
$command = CreateUserCommand::fromArray($request->all());
$dto = $useCase->handle($command);

return response()->json([
'status' => 'success',
'data' => UserViewModelFactory::build($dto)->toArray(),
], 201);
} catch (\Throwable $throwable) {
Log::error('Failed to create user', [
'message' => $throwable->getMessage(),
'trace' => $throwable->getTraceAsString(),
]);

return response()->json([
'status' => 'error',
'message' => 'Internal Server Error',
], 500);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public static function missingKeyProvider(): array
['email'],
['password'],
['age_range'],
['subscription_tier'],
];
}

public function test_fromArray_allows_subscription_tier_to_be_omitted(): void
{
$data = $this->validData();
unset($data['subscription_tier']);

$command = CreateUserCommand::fromArray($data);

$this->assertNull($command->subscriptionTier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ protected function tearDown(): void
parent::tearDown();
}

private function buildCommand(): CreateUserCommand
private function buildCommand(array $overrides = []): CreateUserCommand
{
$faker = FakerFactory::create();

return CreateUserCommand::fromArray([
return CreateUserCommand::fromArray(array_merge([
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(),
'email' => $faker->unique()->safeEmail(),
'password' => $faker->password(8),
'age_range' => $faker->randomElement(['teens', '20s', '30s', '40s', '50s', '60plus']),
'subscription_tier' => 'free',
]);
], $overrides));
}

private function buildDto(): UserDto
Expand Down Expand Up @@ -66,4 +66,33 @@ public function test_handle_passes_entity_to_repository_and_returns_dto(): void

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

public function test_handle_defaults_subscription_tier_to_free_when_omitted(): void
{
$faker = FakerFactory::create();
$command = CreateUserCommand::fromArray([
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(),
'email' => $faker->unique()->safeEmail(),
'password' => $faker->password(8),
'age_range' => 'teens',
]);
$dto = $this->buildDto();
$passedEntity = null;

/** @var UserRepositroyInterface|MockInterface $repository */
$repository = Mockery::mock(UserRepositroyInterface::class);
$repository->shouldReceive('createUser')
->once()
->with(Mockery::on(function (UserEntity $entity) use (&$passedEntity) {
$passedEntity = $entity;

return true;
}))
->andReturn($dto);

(new CreateUserUseCase($repository))->handle($command);

$this->assertSame('free', $passedEntity->getSubscription()->getTier()->value);
}
}
Loading