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,9 +4,9 @@ declare(strict_types=1);
namespace Tests\Post;
use App\Post\Post;
use App\Post\Infrastructure\PdoPostRepository as PostRepository;
use App\Post\Infrastructure\PdoPostRepository;
use App\Post\PostRepositoryInterface;
use App\Post\Application\PostApplicationService as PostService;
use App\Post\Application\PostApplicationService;
use App\Shared\Database\Migrator;
use App\Shared\Exception\NotFoundException;
use App\Shared\Html\HtmlSanitizerInterface;
@@ -34,11 +34,11 @@ final class PostConcurrentUpdateIntegrationTest extends TestCase
public function testUpdatePostThrowsWhenRowDisappearsBetweenReadAndWrite(): void
{
$realRepo = new PostRepository($this->db);
$realRepo = new PdoPostRepository($this->db);
$repo = new class($realRepo) implements PostRepositoryInterface {
private bool $deleted = false;
public function __construct(private readonly PostRepository $inner) {}
public function __construct(private readonly PdoPostRepository $inner) {}
public function findAll(?int $categoryId = null): array { return $this->inner->findAll($categoryId); }
public function findPage(int $limit, int $offset, ?int $categoryId = null): array { return $this->inner->findPage($limit, $offset, $categoryId); }
@@ -70,7 +70,7 @@ final class PostConcurrentUpdateIntegrationTest extends TestCase
public function sanitize(string $html): string { return $html; }
};
$service = new PostService($repo, $sanitizer);
$service = new PostApplicationService($repo, $sanitizer);
$this->expectException(NotFoundException::class);
$service->updatePost(1, 'Titre modifié', '<p>Contenu modifié</p>');

View File

@@ -6,7 +6,7 @@ namespace Tests\Post;
use App\Category\Category;
use App\Category\CategoryServiceInterface;
use App\Post\Post;
use App\Post\Http\PostController as PostController;
use App\Post\Http\PostController;
use App\Post\PostServiceInterface;
use App\Shared\Exception\NotFoundException;
use App\Shared\Http\FlashServiceInterface;

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Tests\Post;
use App\Post\Post;
use App\Post\Infrastructure\TwigPostExtension as PostExtension;
use App\Post\Infrastructure\TwigPostExtension;
use PHPUnit\Framework\TestCase;
use Twig\TwigFunction;
@@ -16,7 +16,7 @@ final class PostExtensionTest extends TestCase
protected function setUp(): void
{
$extension = new PostExtension();
$extension = new TwigPostExtension();
$this->functions = [];
foreach ($extension->getFunctions() as $function) {

View File

@@ -3,7 +3,7 @@ declare(strict_types=1);
namespace Tests\Post;
use App\Post\Infrastructure\PdoPostRepository as PostRepository;
use App\Post\Infrastructure\PdoPostRepository;
use App\Shared\Database\Migrator;
use PDO;
use PHPUnit\Framework\TestCase;
@@ -31,7 +31,7 @@ final class PostFtsUsernameSyncIntegrationTest extends TestCase
{
$this->db->exec("UPDATE users SET username = 'alice_renamed' WHERE id = 1");
$results = (new PostRepository($this->db))->search('alice_renamed');
$results = (new PdoPostRepository($this->db))->search('alice_renamed');
self::assertCount(1, $results);
self::assertSame('alice_renamed', $results[0]->getAuthorUsername());

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);
}

View File

@@ -5,7 +5,7 @@ namespace Tests\Post;
use App\Post\Post;
use App\Post\PostRepositoryInterface;
use App\Post\Application\PostApplicationService as PostService;
use App\Post\Application\PostApplicationService;
use App\Shared\Html\HtmlSanitizerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
@@ -19,13 +19,13 @@ final class PostServiceCoverageTest extends TestCase
/** @var HtmlSanitizerInterface&MockObject */
private HtmlSanitizerInterface $sanitizer;
private PostService $service;
private PostApplicationService $service;
protected function setUp(): void
{
$this->repository = $this->createMock(PostRepositoryInterface::class);
$this->sanitizer = $this->createMock(HtmlSanitizerInterface::class);
$this->service = new PostService($this->repository, $this->sanitizer);
$this->service = new PostApplicationService($this->repository, $this->sanitizer);
}
public function testGetAllPostsPassesCategoryIdToRepository(): void

View File

@@ -5,14 +5,14 @@ namespace Tests\Post;
use App\Post\Post;
use App\Post\PostRepositoryInterface;
use App\Post\Application\PostApplicationService as PostService;
use App\Post\Application\PostApplicationService;
use App\Shared\Exception\NotFoundException;
use App\Shared\Html\HtmlSanitizerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Tests unitaires pour PostService.
* Tests unitaires pour PostApplicationService.
*
* Couvre la création, la mise à jour, la suppression et les lectures.
* HtmlSanitizerInterface et PostRepository sont mockés pour isoler la logique métier.
@@ -26,13 +26,13 @@ final class PostServiceTest extends TestCase
/** @var HtmlSanitizerInterface&MockObject */
private HtmlSanitizerInterface $sanitizer;
private PostService $service;
private PostApplicationService $service;
protected function setUp(): void
{
$this->repository = $this->createMock(PostRepositoryInterface::class);
$this->sanitizer = $this->createMock(HtmlSanitizerInterface::class);
$this->service = new PostService($this->repository, $this->sanitizer);
$this->service = new PostApplicationService($this->repository, $this->sanitizer);
}

View File

@@ -5,7 +5,7 @@ namespace Tests\Post;
use App\Post\Post;
use App\Post\PostServiceInterface;
use App\Post\Http\RssController as RssController;
use App\Post\Http\RssController;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\ControllerTestBase;