-
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 to change a user password
- Loading branch information
1 parent
2ce9067
commit c9d0fd3
Showing
8 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
<?php | ||
|
||
require dirname(__DIR__) . '/src/Shared/kernel.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
37 changes: 37 additions & 0 deletions
37
src/User/Application/Controller/ChangePasswordController.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,37 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Source\User\Application\Controller; | ||
|
||
use Exception; | ||
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\ChangePasswordCommand; | ||
|
||
class ChangePasswordController 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 ChangePasswordCommand($params['email'], $params['password']); | ||
$this->commandBus->handle($command); | ||
return $response->withStatus(200); | ||
} catch (Exception $e) { | ||
return $response->withStatus($e->getCode(), $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
27 changes: 27 additions & 0 deletions
27
tests/User/Application/Controller/ChangePasswordControllerTest.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,27 @@ | ||
<?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 ChangePasswordControllerTest extends AcceptanceTestCase | ||
{ | ||
public function testCanChangeAPassword(): void | ||
{ | ||
$user = Users::aUser(); | ||
$userRepository = new UserRepositoryInMySQL(MysqlClient::getConnection()); | ||
$userRepository->save($user); | ||
$response = $this->client->put('/user', [ | ||
'form_params' => [ | ||
'email' => $user->getEmail()->toString(), | ||
'password' => 'changed-password', | ||
], | ||
]); | ||
self::assertEquals(200, $response->getStatusCode()); | ||
} | ||
} |