Refatoring : Working state
This commit is contained in:
@@ -7,7 +7,7 @@ use App\Media\Exception\FileTooLargeException;
|
||||
use App\Media\Exception\InvalidMimeTypeException;
|
||||
use App\Media\Exception\StorageException;
|
||||
use App\Media\Media;
|
||||
use App\Media\Http\MediaController as MediaController;
|
||||
use App\Media\Http\MediaController;
|
||||
use App\Media\MediaServiceInterface;
|
||||
use App\Shared\Http\FlashServiceInterface;
|
||||
use App\Shared\Http\SessionManagerInterface;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Tests\Media;
|
||||
|
||||
use App\Media\Media;
|
||||
use App\Media\MediaRepositoryInterface;
|
||||
use App\Media\Application\MediaApplicationService as MediaService;
|
||||
use App\Media\Application\MediaApplicationService;
|
||||
use App\Media\Infrastructure\LocalMediaStorage;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use PDOException;
|
||||
@@ -25,7 +25,7 @@ final class MediaServiceDuplicateAfterInsertRaceTest extends TestCase
|
||||
|
||||
private string $uploadDir;
|
||||
|
||||
private MediaService $service;
|
||||
private MediaApplicationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -34,7 +34,7 @@ final class MediaServiceDuplicateAfterInsertRaceTest extends TestCase
|
||||
$this->uploadDir = sys_get_temp_dir() . '/slim_media_race_' . uniqid('', true);
|
||||
@mkdir($this->uploadDir, 0755, true);
|
||||
|
||||
$this->service = new MediaService($this->repository, $this->postRepository, new LocalMediaStorage($this->uploadDir), '/media', 5 * 1024 * 1024);
|
||||
$this->service = new MediaApplicationService($this->repository, $this->postRepository, new LocalMediaStorage($this->uploadDir), '/media', 5 * 1024 * 1024);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Tests\Media;
|
||||
use App\Media\Exception\FileTooLargeException;
|
||||
use App\Media\Exception\StorageException;
|
||||
use App\Media\MediaRepositoryInterface;
|
||||
use App\Media\Application\MediaApplicationService as MediaService;
|
||||
use App\Media\Application\MediaApplicationService;
|
||||
use App\Media\Infrastructure\LocalMediaStorage;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -24,7 +24,7 @@ final class MediaServiceEdgeCasesTest extends TestCase
|
||||
$file = $this->createMock(UploadedFileInterface::class);
|
||||
$file->method('getSize')->willReturn(null);
|
||||
|
||||
$service = new MediaService($repo, $postRepo, new LocalMediaStorage('/tmp'), '/media', 1000);
|
||||
$service = new MediaApplicationService($repo, $postRepo, new LocalMediaStorage('/tmp'), '/media', 1000);
|
||||
|
||||
$this->expectException(StorageException::class);
|
||||
$service->store($file, 1);
|
||||
@@ -42,7 +42,7 @@ final class MediaServiceEdgeCasesTest extends TestCase
|
||||
$file->method('getSize')->willReturn(999999);
|
||||
$file->method('getStream')->willReturn($stream);
|
||||
|
||||
$service = new MediaService($repo, $postRepo, new LocalMediaStorage('/tmp'), '/media', 100);
|
||||
$service = new MediaApplicationService($repo, $postRepo, new LocalMediaStorage('/tmp'), '/media', 100);
|
||||
|
||||
$this->expectException(FileTooLargeException::class);
|
||||
$service->store($file, 1);
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Tests\Media;
|
||||
|
||||
use App\Media\Exception\InvalidMimeTypeException;
|
||||
use App\Media\MediaRepositoryInterface;
|
||||
use App\Media\Application\MediaApplicationService as MediaService;
|
||||
use App\Media\Application\MediaApplicationService;
|
||||
use App\Media\Infrastructure\LocalMediaStorage;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -32,7 +32,7 @@ final class MediaServiceInvalidMimeTest extends TestCase
|
||||
$file->method('getStream')->willReturn($stream);
|
||||
$file->method('getClientFilename')->willReturn('photo.png');
|
||||
|
||||
$service = new MediaService($repo, $postRepo, new LocalMediaStorage(sys_get_temp_dir()), '/media', 500000);
|
||||
$service = new MediaApplicationService($repo, $postRepo, new LocalMediaStorage(sys_get_temp_dir()), '/media', 500000);
|
||||
|
||||
try {
|
||||
$this->expectException(InvalidMimeTypeException::class);
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Tests\Media;
|
||||
|
||||
use App\Media\Exception\StorageException;
|
||||
use App\Media\MediaRepositoryInterface;
|
||||
use App\Media\Application\MediaApplicationService as MediaService;
|
||||
use App\Media\Application\MediaApplicationService;
|
||||
use App\Media\Infrastructure\LocalMediaStorage;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -29,7 +29,7 @@ final class MediaServiceInvalidTempPathTest extends TestCase
|
||||
|
||||
$postRepo = $this->createMock(PostRepositoryInterface::class);
|
||||
|
||||
$service = new MediaService($repository, $postRepo, new LocalMediaStorage(sys_get_temp_dir()), '/media', 500000);
|
||||
$service = new MediaApplicationService($repository, $postRepo, new LocalMediaStorage(sys_get_temp_dir()), '/media', 500000);
|
||||
|
||||
$this->expectException(StorageException::class);
|
||||
$this->expectExceptionMessage('Impossible de localiser le fichier temporaire uploadé');
|
||||
|
||||
@@ -7,7 +7,7 @@ use App\Media\Exception\FileTooLargeException;
|
||||
use App\Media\Exception\InvalidMimeTypeException;
|
||||
use App\Media\Media;
|
||||
use App\Media\MediaRepositoryInterface;
|
||||
use App\Media\Application\MediaApplicationService as MediaService;
|
||||
use App\Media\Application\MediaApplicationService;
|
||||
use App\Media\Infrastructure\LocalMediaStorage;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
@@ -16,7 +16,7 @@ use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour MediaService.
|
||||
* Tests unitaires pour MediaApplicationService.
|
||||
*
|
||||
* Stratégie : les opérations sur le système de fichiers réel (finfo, GD,
|
||||
* copy, moveTo) sont exercées via de vrais fichiers JPEG temporaires ;
|
||||
@@ -39,7 +39,7 @@ final class MediaServiceTest extends TestCase
|
||||
|
||||
private string $uploadDir;
|
||||
|
||||
private MediaService $service;
|
||||
private MediaApplicationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -48,7 +48,7 @@ final class MediaServiceTest extends TestCase
|
||||
$this->uploadDir = sys_get_temp_dir() . '/slim_media_test_' . uniqid();
|
||||
@mkdir($this->uploadDir, 0755, true);
|
||||
|
||||
$this->service = new MediaService(
|
||||
$this->service = new MediaApplicationService(
|
||||
mediaRepository: $this->repository,
|
||||
postRepository: $this->postRepository,
|
||||
mediaStorage: new LocalMediaStorage($this->uploadDir),
|
||||
|
||||
Reference in New Issue
Block a user