-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add a controller that can delete a user
- Loading branch information
1 parent
c9d0fd3
commit 46f03c7
Showing
6 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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()); | ||
} | ||
} | ||
} |
This file contains 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
26 changes: 26 additions & 0 deletions
26
tests/User/Application/Controller/DeleteUserControllerTest.php
This file contains 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,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()); | ||
} | ||
} |