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 @@ -23,4 +23,10 @@ public function savePasswordResetToken(
UserId $userId,
PasswordResetToken $token
): void;

public function resetPassword(
UserId $userId,
PasswordResetToken $token,
string $newPassword
): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\User\Infrastructure\InfrastructureTest;

use App\Common\Domain\ValueObject\UserId;
use App\Models\PasswordResetRequest;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
use App\User\Infrastructure\Repository\UserRepository;
use Illuminate\Support\Facades\Hash;
use App\User\Infrastructure\Service\BcryptPasswordHasher;
use App\User\Domain\ValueObject\PasswordResetToken;
use Exception;

class UserRepository_resetPasswordTest extends TestCase
{
private $user;
private $repository;
protected function setUp(): void
{
parent::setUp();
$this->refresh();
$this->user = $this->createUser();
$this->repository = new UserRepository(
new BcryptPasswordHasher(),
$this->user
);
}

protected function tearDown(): void
{
$this->refresh();
parent::tearDown();
}

private function refresh(): void
{
if (env('APP_ENV') === 'testing') {
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;');
User::truncate();
PasswordResetRequest::truncate();
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;');
}
}

private function createUser(): User
{
return User::create([
'first_name' => 'Sergio',
'last_name' => 'Ramos',
'email' => 'real-madrid15@test.com',
'password' => 'el-capitán-1234',
'bio' => 'Real Madrid player',
'location' => 'Madrid',
'skills' => ['Football', 'Leadership'],
'profile_image' => 'https://example.com/sergio.jpg'
]);
}

public function test_reset_password_ok(): void
{
$objectId = new UserId($this->user->id);
$token = new PasswordResetToken(bin2hex(random_bytes(32)));
$newPassword = 'test1234test1234test1234test1234';

$this->repository->savePasswordResetToken($objectId, $token);

$this->repository->resetPassword($objectId, $token, $newPassword);

$updatedUser = $this->user->find($this->user->id);

$this->assertTrue(
Hash::check($newPassword, $updatedUser->password)
);
}

public function test_reset_password_invalid_userId(): void
{
$this->expectException(Exception::class);

$objectId = new UserId(100);
$token = new PasswordResetToken(bin2hex(random_bytes(32)));
$newPassword = 'test1234test1234test1234test1234';

$this->repository->savePasswordResetToken($objectId, $token);

$this->repository->resetPassword($objectId, $token, $newPassword);

$updatedUser = $this->user->find($this->user->id);

$this->assertTrue(
Hash::check($newPassword, $updatedUser->password)
);
Comment on lines +89 to +94
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code following the expected exception call in the invalid user test is unreachable; consider removing these assertions to clarify the test intent.

Suggested change
$updatedUser = $this->user->find($this->user->id);
$this->assertTrue(
Hash::check($newPassword, $updatedUser->password)
);

Copilot uses AI. Check for mistakes.
}
}
18 changes: 18 additions & 0 deletions src/app/User/Infrastructure/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,22 @@ public function savePasswordResetToken(
]);
}
}

public function resetPassword(
UserId $userId,
PasswordResetToken $token,
string $newPassword
): void {
$userModel = $this->user->find($userId->getValue());

if ($userModel === null) {
throw new Exception('User not found');
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a domain-specific exception (e.g., UserNotFoundException) instead of a generic Exception to improve error handling clarity.

Copilot uses AI. Check for mistakes.
}

$hashedPassword = $this->hasher->hash($newPassword);

$userModel->update([
'password' => $hashedPassword
]);
}
}