From b783afbd4a2a7b72807f36a4e201704b895b1e21 Mon Sep 17 00:00:00 2001 From: rigonlucas Date: Thu, 22 Aug 2024 08:49:26 -0300 Subject: [PATCH] fix tests --- .env.testing | 2 +- .../V1/User/ShowUserController.php | 5 ++++- app/Models/User.php | 1 + .../User/Repository/UserRepository.php | 19 ++++++++++++++----- routes/Api/V1/user.php | 4 ++-- routes/api.php | 17 +++++++++++++++-- .../e2e/User/CreateUserE2eTest.php | 14 +++++++------- .../Integration/e2e/User/ShowUserE2eTest.php | 2 +- .../e2e/User/UpdateUserE2eTest.php | 12 ++++++------ 9 files changed, 51 insertions(+), 25 deletions(-) diff --git a/.env.testing b/.env.testing index 8900f8e..d2cf80a 100644 --- a/.env.testing +++ b/.env.testing @@ -3,7 +3,7 @@ APP_ENV=testing APP_KEY=base64:dzLwRWVSB10asrK3As1sT/IsoV7MFQ5YGjxIVWfmlvs= APP_DEBUG=true APP_TIMEZONE=UTC -APP_URL=http://localhost +APP_URL=http://localhost:8080 APP_LOCALE=en APP_FALLBACK_LOCALE=en diff --git a/app/Http/Controllers/V1/User/ShowUserController.php b/app/Http/Controllers/V1/User/ShowUserController.php index 6bbe972..532c0b8 100644 --- a/app/Http/Controllers/V1/User/ShowUserController.php +++ b/app/Http/Controllers/V1/User/ShowUserController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\V1\User; use App\Http\Controllers\Controller; +use Core\Application\Account\Commons\Gateways\AccountRepositoryInterface; use Core\Application\User\Show\ShowUserUseCase; use Core\Presentation\Http\Errors\ErrorPresenter; use Core\Presentation\Http\User\UserDetaisPresenter; @@ -15,6 +16,7 @@ class ShowUserController extends Controller { public function __construct( private readonly UserRepository $userRepository, + private readonly AccountRepositoryInterface $accountRepository, private readonly FrameworkContract $frameworkService ) { } @@ -24,7 +26,8 @@ public function __invoke(string $uuid) try { $useCase = new ShowUserUseCase( framework: $this->frameworkService, - userRepository: $this->userRepository + userRepository: $this->userRepository, + accountRepository: $this->accountRepository ); $userEntity = $useCase->execute(uuid: $uuid); } catch (OutputErrorException $outputErrorException) { diff --git a/app/Models/User.php b/app/Models/User.php index 8ead170..d1bb5d8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -17,6 +17,7 @@ * @property mixed|string $email * @property mixed|string $name * @property mixed|string $uuid + * @property mixed|int $role * @property mixed|int $id * @property mixed accounts * @property int|mixed|null $account_id diff --git a/infra/Database/User/Repository/UserRepository.php b/infra/Database/User/Repository/UserRepository.php index 01035b0..3b2493e 100644 --- a/infra/Database/User/Repository/UserRepository.php +++ b/infra/Database/User/Repository/UserRepository.php @@ -18,7 +18,7 @@ class UserRepository implements UserRepositoryInterface public function findById(int $id): ?UserEntity { $userModel = User::query() - ->select(['id', 'name', 'email', 'birthday', 'uuid']) + ->select(['id', 'name', 'email', 'birthday', 'uuid', 'account_id', 'role']) ->find($id); if (!$userModel) { return null; @@ -30,14 +30,18 @@ public function findById(int $id): ?UserEntity email: $userModel->email, uuid: FrameworkService::getInstance()->uuid()->uuidFromString($userModel->uuid), account: AccountEntity::forIdentify($userModel->account_id), - birthday: new DateTime($userModel->birthday) + birthday: new DateTime($userModel->birthday), + role: $userModel->role ); } + /** + * @throws InvalidEmailException + */ public function findByUuid(string $uuid): ?UserEntity { $userModel = User::query() - ->select(['id', 'name', 'email', 'birthday', 'uuid', 'account_id']) + ->select(['id', 'name', 'email', 'birthday', 'uuid', 'account_id', 'role']) ->where('uuid', '=', $uuid) ->first(); if (!$userModel) { @@ -51,13 +55,17 @@ public function findByUuid(string $uuid): ?UserEntity uuid: FrameworkService::getInstance()->uuid()->uuidFromString($userModel->uuid), account: AccountEntity::forIdentify($userModel->account_id), birthday: new DateTime($userModel->birthday), + role: $userModel->role ); } + /** + * @throws InvalidEmailException + */ public function findByEmail(string $email): ?UserEntity { $userModel = User::query() - ->select(['id', 'name', 'email', 'birthday', 'uuid']) + ->select(['id', 'name', 'email', 'birthday', 'uuid', 'account_id', 'role']) ->where('email', '=', $email) ->first(); if (!$userModel) { @@ -70,7 +78,8 @@ public function findByEmail(string $email): ?UserEntity email: $userModel->email, uuid: FrameworkService::getInstance()->uuid()->uuidFromString($userModel->uuid), account: AccountEntity::forIdentify($userModel->account_id), - birthday: new DateTime($userModel->birthday) + birthday: new DateTime($userModel->birthday), + role: $userModel->role ); } diff --git a/routes/Api/V1/user.php b/routes/Api/V1/user.php index 12aa29a..8655226 100644 --- a/routes/Api/V1/user.php +++ b/routes/Api/V1/user.php @@ -11,8 +11,8 @@ }); Route::get('/show/{uuid}', [ShowUserController::class, '__invoke']) ->whereUuid('uuid') - ->name('v1.user.show'); + ->name('api.v1.user.show'); Route::put('/user/update/{uuid}', [UpdateUserController::class, '__invoke']) ->whereUuid('uuid') - ->name('v1.user.update'); + ->name('api.v1.user.update'); }); \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 4a935fa..7854450 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,14 +1,27 @@ name('v1.user.create'); + ->name('api.v1.user.create'); Route::prefix('v1') ->middleware('auth:sanctum') ->group(function () { - require __DIR__ . '/Api/V1/user.php'; + Route::prefix('user')->group(function () { + Route::get('/auth', function (Request $request) { + return $request; + }); + Route::get('/show/{uuid}', [ShowUserController::class, '__invoke']) + ->whereUuid('uuid') + ->name('api.v1.user.show'); + Route::put('/user/update/{uuid}', [UpdateUserController::class, '__invoke']) + ->whereUuid('uuid') + ->name('api.v1.user.update'); + }); }); diff --git a/tests/Integration/e2e/User/CreateUserE2eTest.php b/tests/Integration/e2e/User/CreateUserE2eTest.php index a64952e..01f5c4d 100644 --- a/tests/Integration/e2e/User/CreateUserE2eTest.php +++ b/tests/Integration/e2e/User/CreateUserE2eTest.php @@ -20,7 +20,7 @@ class CreateUserE2eTest extends TestCase public function test_create_user_success_case() { $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -47,7 +47,7 @@ public function test_create_user_join_into_an_account_success_case() ]); $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -74,7 +74,7 @@ public function test_create_user_join_into_an_account_code_already_used() ]); $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -97,7 +97,7 @@ public function test_create_user_join_into_an_account_code_expired() ]); $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -114,7 +114,7 @@ public function test_create_user_join_into_an_account_code_expired() public function test_create_user_join_into_an_account_code_not_found() { $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -134,7 +134,7 @@ public function test_create_user_fail_case_email_exists_and_birthdate_less_than_ 'email' => $this->faker->userName . '@gmail.com' ]); $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $userMoodel->name, 'email' => $userMoodel->email, @@ -157,7 +157,7 @@ public function test_create_user_fail_case_email_exists_and_birthdate_less_than_ public function test_create_user_fail_case_password_less_than_8_characters() { $response = $this->postJson( - route('v1.user.create'), + route('api.v1.user.create'), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', diff --git a/tests/Integration/e2e/User/ShowUserE2eTest.php b/tests/Integration/e2e/User/ShowUserE2eTest.php index aa90afc..61ca4d9 100644 --- a/tests/Integration/e2e/User/ShowUserE2eTest.php +++ b/tests/Integration/e2e/User/ShowUserE2eTest.php @@ -23,7 +23,7 @@ public function test_update_user_success_case() { //update user $response = $this->getJson( - route('v1.user.show', ['uuid' => $this->user->uuid]), + route('api.v1.user.show', ['uuid' => $this->user->uuid]), HttpApiHeaders::$headersJson ); //assert response diff --git a/tests/Integration/e2e/User/UpdateUserE2eTest.php b/tests/Integration/e2e/User/UpdateUserE2eTest.php index f97cee4..60ccbaf 100644 --- a/tests/Integration/e2e/User/UpdateUserE2eTest.php +++ b/tests/Integration/e2e/User/UpdateUserE2eTest.php @@ -24,7 +24,7 @@ public function test_update_user_success_case() { //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->user->uuid]), + route('api.v1.user.update', ['uuid' => $this->user->uuid]), [ 'name' => $this->faker->name . 'updated', 'email' => $this->faker->userName . 'email@gmail.com', @@ -52,7 +52,7 @@ public function test_update_user_fail_case_email_exists_and_birthdate_less_than_ //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->user->uuid]), + route('api.v1.user.update', ['uuid' => $this->user->uuid]), [ 'name' => $userMoodel->name, 'email' => $userMoodel->email, @@ -76,7 +76,7 @@ public function test_update_user_fail_case_password_less_than_8_characters() { //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->user->uuid]), + route('api.v1.user.update', ['uuid' => $this->user->uuid]), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com', @@ -105,7 +105,7 @@ public function test_update_user_fail_case_email_exists() //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->user->uuid]), + route('api.v1.user.update', ['uuid' => $this->user->uuid]), [ 'name' => $otherUser->name, 'email' => $otherUser->email, @@ -139,7 +139,7 @@ public function test_update_user_success_case_new_valid_email() //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->user->uuid]), + route('api.v1.user.update', ['uuid' => $this->user->uuid]), [ 'name' => $otherUser->name, 'email' => $this->faker->userName . '@gmail.com', @@ -161,7 +161,7 @@ public function test_update_user_fail_case_user_not_found() { //update user $response = $this->putJson( - route('v1.user.update', ['uuid' => $this->faker->uuid]), + route('api.v1.user.update', ['uuid' => $this->faker->uuid]), [ 'name' => $this->faker->name, 'email' => $this->faker->userName . '@gmail.com',