Skip to content

Commit

Permalink
feature: add a controller that can delete a user
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimorillo committed Mar 17, 2024
1 parent c9d0fd3 commit 46f03c7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions configuration/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

use Source\User\Application\Controller\ChangePasswordController;
use Source\User\Application\Controller\CreateAUserController;
use Source\User\Application\Controller\DeleteUserController;

// Add the routes here like in the following example:
$app->post('/user', CreateAUserController::class);
$app->put('/user', ChangePasswordController::class);
$app->delete('/user', DeleteUserController::class);
4 changes: 3 additions & 1 deletion src/User/Application/Command/DeleteUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Source\User\Application\Command;

class DeleteUserCommand
use Source\Shared\CQRS\CommandBus\Command;

class DeleteUserCommand implements Command
{
private string $userId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Source\User\Domain\ValueObject\UserId;
use Source\User\Domain\ValueObject\UserRepositoryInterface;

class DeleteUserCommandHanler
class DeleteUserCommandHandler
{
private UserRepositoryInterface $repository;

Expand Down
36 changes: 36 additions & 0 deletions src/User/Application/Controller/DeleteUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Source\User\Application\Controller;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Source\Shared\Application\Controller;
use Source\Shared\CQRS\CommandBus\CommandBus;
use Source\User\Application\Command\DeleteUserCommand;

class DeleteUserController implements Controller
{
private CommandBus $commandBus;

public function __construct(CommandBus $commandBus) {
$this->commandBus = $commandBus;
}

public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
try {
$params = $request->getParams();
$command = new DeleteUserCommand($params['user_id']);
$this->commandBus->handle($command);
return $response->withStatus(200);
} catch (\Exception $e) {
$code = $e->getCode() > 0 ? $e->getCode() : 503;
return $response->withStatus($code, $e->getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Source\User\Application\Command\DeleteUserCommand;
use Source\User\Application\Command\DeleteUserCommandHanler;
use Source\User\Application\Command\DeleteUserCommandHandler;
use Source\User\Infrastructure\Repository\UserRepositoryInMemory;
use Tests\Fixtures\Users;

Expand All @@ -19,7 +19,7 @@ public function setUp(): void
$this->repository = new UserRepositoryInMemory([
$this->user->getId()->toString() => $this->user,
]);
$this->commandHandler = new DeleteUserCommandHanler($this->repository);
$this->commandHandler = new DeleteUserCommandHandler($this->repository);
}

public function testCanDeleteAUser(): void
Expand Down
26 changes: 26 additions & 0 deletions tests/User/Application/Controller/DeleteUserControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\User\Application\Controller;

use Source\Shared\MysqlClient\MysqlClient;
use Source\User\Infrastructure\Repository\UserRepositoryInMySQL;
use Tests\Acceptance\AcceptanceTestCase;
use Tests\Fixtures\Users;

class DeleteUserControllerTest extends AcceptanceTestCase
{
public function testCanDeleteAUser(): void
{
$user = Users::aUser();
$userRepository = new UserRepositoryInMySQL(MysqlClient::getConnection());
$userRepository->save($user);
$response = $this->client->delete('/user', [
'form_params' => [
'user_id' => $user->getId()->toString(),
]
]);
self::assertEquals(200, $response->getStatusCode());
}
}

0 comments on commit 46f03c7

Please sign in to comment.