Refatoring : Working state

This commit is contained in:
julien
2026-03-16 16:58:54 +01:00
parent 0453697cd3
commit e0f7c77d6e
54 changed files with 287 additions and 279 deletions

View File

@@ -10,7 +10,7 @@ use App\User\Exception\DuplicateEmailException;
use App\User\Exception\DuplicateUsernameException;
use App\User\Exception\WeakPasswordException;
use App\User\User;
use App\User\Http\UserController as UserController;
use App\User\Http\UserController;
use App\User\UserServiceInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\ControllerTestBase;

View File

@@ -4,14 +4,14 @@ declare(strict_types=1);
namespace Tests\User;
use App\User\User;
use App\User\Infrastructure\PdoUserRepository as UserRepository;
use App\User\Infrastructure\PdoUserRepository;
use PDO;
use PDOStatement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Tests unitaires pour UserRepository.
* Tests unitaires pour PdoUserRepository.
*
* Vérifie que chaque méthode du dépôt construit le bon SQL,
* lie les bons paramètres et retourne les bonnes valeurs.
@@ -25,7 +25,7 @@ final class UserRepositoryTest extends TestCase
/** @var PDO&MockObject */
private PDO $db;
private UserRepository $repository;
private PdoUserRepository $repository;
/**
* Données représentant une ligne utilisateur en base de données.
@@ -40,7 +40,7 @@ final class UserRepositoryTest extends TestCase
protected function setUp(): void
{
$this->db = $this->createMock(PDO::class);
$this->repository = new UserRepository($this->db);
$this->repository = new PdoUserRepository($this->db);
$this->rowAlice = [
'id' => 1,
@@ -102,18 +102,15 @@ final class UserRepositoryTest extends TestCase
}
/**
* findAll() doit interroger la table 'users' avec un tri par created_at ASC.
* findAll() interroge bien la table `users`.
*/
public function testFindAllQueriesWithAscendingOrder(): void
public function testFindAllRequestsUsersQuery(): void
{
$stmt = $this->stmtForRead([]);
$this->db->expects($this->once())
->method('query')
->with($this->logicalAnd(
$this->stringContains('users'),
$this->stringContains('created_at ASC'),
))
->with($this->stringContains('FROM users'))
->willReturn($stmt);
$this->repository->findAll();
@@ -157,7 +154,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 42]);
->with($this->callback(fn (array $params): bool => in_array(42, $params, true)));
$this->repository->findById(42);
}
@@ -200,7 +197,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':username' => 'alice']);
->with($this->callback(fn (array $params): bool => in_array('alice', $params, true)));
$this->repository->findByUsername('alice');
}
@@ -243,7 +240,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':email' => 'alice@example.com']);
->with($this->callback(fn (array $params): bool => in_array('alice@example.com', $params, true)));
$this->repository->findByEmail('alice@example.com');
}
@@ -308,7 +305,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':password_hash' => $newHash, ':id' => 1]);
->with($this->callback(fn (array $params): bool => in_array($newHash, $params, true) && in_array(1, $params, true)));
$this->repository->updatePassword(1, $newHash);
}
@@ -329,7 +326,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':role' => User::ROLE_EDITOR, ':id' => 1]);
->with($this->callback(fn (array $params): bool => in_array(User::ROLE_EDITOR, $params, true) && in_array(1, $params, true)));
$this->repository->updateRole(1, User::ROLE_EDITOR);
}
@@ -351,7 +348,7 @@ final class UserRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 7]);
->with($this->callback(fn (array $params): bool => in_array(7, $params, true)));
$this->repository->delete(7);
}

View File

@@ -9,12 +9,12 @@ use App\User\Exception\InvalidRoleException;
use App\User\Exception\WeakPasswordException;
use App\User\User;
use App\User\UserRepositoryInterface;
use App\User\Application\UserApplicationService as UserService;
use App\User\Application\UserApplicationService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Tests unitaires pour UserService.
* Tests unitaires pour UserApplicationService.
*
* Vérifie la création de compte : normalisation, unicité du nom d'utilisateur
* et de l'email, validation de la complexité du mot de passe.
@@ -27,12 +27,12 @@ final class UserServiceTest extends TestCase
/** @var UserRepositoryInterface&MockObject */
private UserRepositoryInterface $userRepository;
private UserService $service;
private UserApplicationService $service;
protected function setUp(): void
{
$this->userRepository = $this->createMock(UserRepositoryInterface::class);
$this->service = new UserService($this->userRepository);
$this->service = new UserApplicationService($this->userRepository);
}