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

@@ -4,17 +4,17 @@ declare(strict_types=1);
namespace Tests\Post;
use App\Post\Post;
use App\Post\Infrastructure\PdoPostRepository as PostRepository;
use App\Post\Infrastructure\PdoPostRepository;
use PDO;
use PDOStatement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Tests unitaires pour PostRepository.
* Tests unitaires pour PdoPostRepository.
*
* Vérifie que chaque méthode du dépôt construit le bon SQL,
* lie les bons paramètres et retourne les bonnes valeurs.
* Vérifie l'intention des requêtes et les valeurs retournées
* sans figer inutilement tous les détails d'implémentation SQL.
*
* PDO et PDOStatement sont mockés pour isoler complètement
* le dépôt de la base de données.
@@ -25,7 +25,7 @@ final class PostRepositoryTest extends TestCase
/** @var PDO&MockObject */
private PDO $db;
private PostRepository $repository;
private PdoPostRepository $repository;
/**
* Données représentant une ligne article en base de données (avec JOINs).
@@ -37,7 +37,7 @@ final class PostRepositoryTest extends TestCase
protected function setUp(): void
{
$this->db = $this->createMock(PDO::class);
$this->repository = new PostRepository($this->db);
$this->repository = new PdoPostRepository($this->db);
$this->rowPost = [
'id' => 1,
@@ -116,14 +116,20 @@ final class PostRepositoryTest extends TestCase
}
/**
* findAll() sans filtre appelle query() et non prepare()
* (pas de paramètre à lier).
* findAll() sans filtre interroge bien la table `posts`.
*/
public function testFindAllWithoutFilterUsesQueryNotPrepare(): void
public function testFindAllWithoutFilterRequestsPostsQuery(): void
{
$stmt = $this->stmtForRead([]);
$this->db->expects($this->once())->method('query')->willReturn($stmt);
$this->db->expects($this->never())->method('prepare');
$this->db->expects($this->once())
->method('query')
->with($this->callback(
static fn (string $sql): bool => str_contains(
strtolower(preg_replace('/\s+/', ' ', $sql)),
'from posts'
)
))
->willReturn($stmt);
$this->repository->findAll();
}
@@ -138,12 +144,17 @@ final class PostRepositoryTest extends TestCase
$this->db->expects($this->once())
->method('prepare')
->with($this->stringContains('category_id'))
->with($this->callback(
static fn (string $sql): bool => str_contains(
strtolower(preg_replace('/\s+/', ' ', $sql)),
'from posts'
) && str_contains($sql, 'category_id')
))
->willReturn($stmt);
$stmt->expects($this->once())
->method('execute')
->with([':category_id' => 3]);
->with($this->callback(fn (array $params): bool => in_array(3, $params, true)));
$this->repository->findAll(3);
}
@@ -215,7 +226,7 @@ final class PostRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':author_id' => 7]);
->with($this->callback(fn (array $params): bool => in_array(7, $params, true)));
$this->repository->findByUserId(7);
}
@@ -230,7 +241,7 @@ final class PostRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':author_id' => 7, ':category_id' => 3]);
->with($this->callback(fn (array $params): bool => count($params) === 2 && in_array(7, $params, true) && in_array(3, $params, true)));
$this->repository->findByUserId(7, 3);
}
@@ -273,7 +284,7 @@ final class PostRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':slug' => 'mon-article']);
->with($this->callback(fn (array $params): bool => in_array('mon-article', $params, true)));
$this->repository->findBySlug('mon-article');
}
@@ -316,7 +327,7 @@ final class PostRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 12]);
->with($this->callback(fn (array $params): bool => in_array(12, $params, true)));
$this->repository->findById(12);
}
@@ -455,7 +466,7 @@ final class PostRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 6]);
->with($this->callback(fn (array $params): bool => in_array(6, $params, true)));
$this->repository->delete(6);
}