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,14 +4,14 @@ declare(strict_types=1);
namespace Tests\Media;
use App\Media\Media;
use App\Media\Infrastructure\PdoMediaRepository as MediaRepository;
use App\Media\Infrastructure\PdoMediaRepository;
use PDO;
use PDOStatement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Tests unitaires pour MediaRepository.
* Tests unitaires pour PdoMediaRepository.
*
* 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 MediaRepositoryTest extends TestCase
/** @var PDO&MockObject */
private PDO $db;
private MediaRepository $repository;
private PdoMediaRepository $repository;
/**
* Données représentant une ligne média en base de données.
@@ -37,7 +37,7 @@ final class MediaRepositoryTest extends TestCase
protected function setUp(): void
{
$this->db = $this->createMock(PDO::class);
$this->repository = new MediaRepository($this->db);
$this->repository = new PdoMediaRepository($this->db);
$this->rowImage = [
'id' => 1,
@@ -70,6 +70,7 @@ final class MediaRepositoryTest extends TestCase
return $stmt;
}
// ── findAll ────────────────────────────────────────────────────
/**
@@ -100,23 +101,21 @@ final class MediaRepositoryTest extends TestCase
}
/**
* findAll() interroge la table 'media' triée par id DESC.
* findAll() interroge bien la table `media`.
*/
public function testFindAllQueriesWithDescendingOrder(): void
public function testFindAllRequestsMediaQuery(): void
{
$stmt = $this->stmtForRead([]);
$this->db->expects($this->once())
->method('query')
->with($this->logicalAnd(
$this->stringContains('media'),
$this->stringContains('id DESC'),
))
->with($this->stringContains('FROM media'))
->willReturn($stmt);
$this->repository->findAll();
}
// ── findByUserId ───────────────────────────────────────────────
/**
@@ -154,11 +153,12 @@ final class MediaRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':user_id' => 5]);
->with($this->callback(fn (array $params): bool => in_array(5, $params, true)));
$this->repository->findByUserId(5);
}
// ── findById ───────────────────────────────────────────────────
/**
@@ -196,13 +196,58 @@ final class MediaRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 8]);
->with($this->callback(fn (array $params): bool => in_array(8, $params, true)));
$this->repository->findById(8);
}
// ── findByHash ─────────────────────────────────────────────────
/**
* findByHash() retourne null si aucun média ne correspond au hash.
*/
public function testFindByHashReturnsNullWhenMissing(): void
{
$stmt = $this->stmtForRead(row: false);
$this->db->method('prepare')->willReturn($stmt);
$this->assertNull($this->repository->findByHash(str_repeat('b', 64)));
}
/**
* findByHash() retourne une instance Media si le hash existe (doublon détecté).
*/
public function testFindByHashReturnsDuplicateMedia(): void
{
$stmt = $this->stmtForRead(row: $this->rowImage);
$this->db->method('prepare')->willReturn($stmt);
$result = $this->repository->findByHash(str_repeat('a', 64));
$this->assertInstanceOf(Media::class, $result);
$this->assertSame(str_repeat('a', 64), $result->getHash());
}
/**
* findByHash() exécute avec le bon hash.
*/
public function testFindByHashQueriesWithCorrectHash(): void
{
$hash = str_repeat('c', 64);
$stmt = $this->stmtForRead(row: false);
$this->db->method('prepare')->willReturn($stmt);
$stmt->expects($this->once())
->method('execute')
->with($this->callback(fn (array $params): bool => in_array($hash, $params, true)));
$this->repository->findByHash($hash);
}
// ── create ─────────────────────────────────────────────────────
/**
* create() prépare un INSERT avec les bonnes colonnes.
*/
@@ -243,6 +288,7 @@ final class MediaRepositoryTest extends TestCase
$this->assertSame(15, $this->repository->create($media));
}
// ── delete ─────────────────────────────────────────────────────
/**
@@ -259,7 +305,7 @@ final class MediaRepositoryTest extends TestCase
$stmt->expects($this->once())
->method('execute')
->with([':id' => 4]);
->with($this->callback(fn (array $params): bool => in_array(4, $params, true)));
$this->repository->delete(4);
}