Working state

This commit is contained in:
julien
2026-03-16 13:40:18 +01:00
parent dec76fa2c7
commit 557360dfde
57 changed files with 1044 additions and 1668 deletions

View File

@@ -11,9 +11,10 @@ use App\Media\MediaController;
use App\Media\MediaServiceInterface;
use App\Shared\Http\FlashServiceInterface;
use App\Shared\Http\SessionManagerInterface;
use App\Shared\Pagination\PaginatedResult;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\UploadedFileInterface;
use Tests\ControllerTestCase;
use Tests\ControllerTestBase;
/**
* Tests unitaires pour MediaController.
@@ -24,7 +25,7 @@ use Tests\ControllerTestCase;
* - delete : introuvable, non-propriétaire, succès propriétaire, succès admin
*/
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class MediaControllerTest extends ControllerTestCase
final class MediaControllerTest extends ControllerTestBase
{
/** @var \Slim\Views\Twig&MockObject */
private \Slim\Views\Twig $view;
@@ -65,8 +66,8 @@ final class MediaControllerTest extends ControllerTestCase
$this->sessionManager->method('isAdmin')->willReturn(true);
$this->sessionManager->method('isEditor')->willReturn(false);
$this->mediaService->expects($this->once())->method('findAll')->willReturn([]);
$this->mediaService->expects($this->never())->method('findByUserId');
$this->mediaService->expects($this->once())->method('findPaginated')->with(1, 12)->willReturn(new PaginatedResult([], 0, 1, 12));
$this->mediaService->expects($this->never())->method('findByUserIdPaginated');
$this->view->expects($this->once())
->method('render')
@@ -86,8 +87,8 @@ final class MediaControllerTest extends ControllerTestCase
$this->sessionManager->method('isAdmin')->willReturn(false);
$this->sessionManager->method('isEditor')->willReturn(true);
$this->mediaService->expects($this->once())->method('findAll')->willReturn([]);
$this->mediaService->expects($this->never())->method('findByUserId');
$this->mediaService->expects($this->once())->method('findPaginated')->with(1, 12)->willReturn(new PaginatedResult([], 0, 1, 12));
$this->mediaService->expects($this->never())->method('findByUserIdPaginated');
$res = $this->controller->index($this->makeGet('/admin/media'), $this->makeResponse());
@@ -103,8 +104,8 @@ final class MediaControllerTest extends ControllerTestCase
$this->sessionManager->method('isEditor')->willReturn(false);
$this->sessionManager->method('getUserId')->willReturn(42);
$this->mediaService->expects($this->once())->method('findByUserId')->with(42)->willReturn([]);
$this->mediaService->expects($this->never())->method('findAll');
$this->mediaService->expects($this->once())->method('findByUserIdPaginated')->with(42, 1, 12)->willReturn(new PaginatedResult([], 0, 1, 12));
$this->mediaService->expects($this->never())->method('findPaginated');
$this->controller->index($this->makeGet('/admin/media'), $this->makeResponse());
}

View File

@@ -6,6 +6,7 @@ namespace Tests\Media;
use App\Media\Media;
use App\Media\MediaRepositoryInterface;
use App\Media\MediaService;
use App\Post\PostRepositoryInterface;
use PDOException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
@@ -19,6 +20,8 @@ final class MediaServiceDuplicateAfterInsertRaceTest extends TestCase
/** @var MediaRepositoryInterface&MockObject */
private MediaRepositoryInterface $repository;
private PostRepositoryInterface $postRepository;
private string $uploadDir;
private MediaService $service;
@@ -26,10 +29,11 @@ final class MediaServiceDuplicateAfterInsertRaceTest extends TestCase
protected function setUp(): void
{
$this->repository = $this->createMock(MediaRepositoryInterface::class);
$this->postRepository = $this->createMock(PostRepositoryInterface::class);
$this->uploadDir = sys_get_temp_dir() . '/slim_media_race_' . uniqid('', true);
@mkdir($this->uploadDir, 0755, true);
$this->service = new MediaService($this->repository, $this->uploadDir, '/media', 5 * 1024 * 1024);
$this->service = new MediaService($this->repository, $this->postRepository, $this->uploadDir, '/media', 5 * 1024 * 1024);
}
protected function tearDown(): void
@@ -49,8 +53,8 @@ final class MediaServiceDuplicateAfterInsertRaceTest extends TestCase
$duplicate = new Media(77, 'existing.gif', '/media/existing.gif', $hash, 1);
$this->repository->expects($this->exactly(2))
->method('findByHash')
->with($hash)
->method('findByHashForUser')
->with($hash, 1)
->willReturnOnConsecutiveCalls(null, $duplicate);
$this->repository->expects($this->once())

View File

@@ -3,26 +3,27 @@ declare(strict_types=1);
namespace Tests\Media;
use App\Media\MediaService;
use App\Media\MediaRepositoryInterface;
use App\Media\Exception\FileTooLargeException;
use App\Media\Exception\StorageException;
use App\Media\MediaRepositoryInterface;
use App\Media\MediaService;
use App\Post\PostRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class MediaServiceEdgeCasesTest extends TestCase
{
public function testRejectsWhenSizeUnknown(): void
{
$repo = $this->createMock(MediaRepositoryInterface::class);
$postRepo = $this->createMock(PostRepositoryInterface::class);
$file = $this->createMock(UploadedFileInterface::class);
$file->method('getSize')->willReturn(null);
$service = new MediaService($repo, '/tmp', '/media', 1000);
$service = new MediaService($repo, $postRepo, '/tmp', '/media', 1000);
$this->expectException(StorageException::class);
$service->store($file, 1);
@@ -31,6 +32,7 @@ final class MediaServiceEdgeCasesTest extends TestCase
public function testRejectsWhenFileTooLarge(): void
{
$repo = $this->createMock(MediaRepositoryInterface::class);
$postRepo = $this->createMock(PostRepositoryInterface::class);
$stream = $this->createMock(StreamInterface::class);
$stream->method('getMetadata')->willReturn('/tmp/file');
@@ -39,7 +41,7 @@ final class MediaServiceEdgeCasesTest extends TestCase
$file->method('getSize')->willReturn(999999);
$file->method('getStream')->willReturn($stream);
$service = new MediaService($repo, '/tmp', '/media', 100);
$service = new MediaService($repo, $postRepo, '/tmp', '/media', 100);
$this->expectException(FileTooLargeException::class);
$service->store($file, 1);

View File

@@ -6,17 +6,18 @@ namespace Tests\Media;
use App\Media\Exception\InvalidMimeTypeException;
use App\Media\MediaRepositoryInterface;
use App\Media\MediaService;
use App\Post\PostRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class MediaServiceInvalidMimeTest extends TestCase
{
public function testRejectsNonImageContentEvenWithImageLikeFilename(): void
{
$repo = $this->createMock(MediaRepositoryInterface::class);
$postRepo = $this->createMock(PostRepositoryInterface::class);
$tmpFile = tempnam(sys_get_temp_dir(), 'upload_');
self::assertNotFalse($tmpFile);
@@ -30,7 +31,7 @@ final class MediaServiceInvalidMimeTest extends TestCase
$file->method('getStream')->willReturn($stream);
$file->method('getClientFilename')->willReturn('photo.png');
$service = new MediaService($repo, sys_get_temp_dir(), '/media', 500000);
$service = new MediaService($repo, $postRepo, sys_get_temp_dir(), '/media', 500000);
try {
$this->expectException(InvalidMimeTypeException::class);

View File

@@ -6,6 +6,7 @@ namespace Tests\Media;
use App\Media\Exception\StorageException;
use App\Media\MediaRepositoryInterface;
use App\Media\MediaService;
use App\Post\PostRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
@@ -25,7 +26,9 @@ final class MediaServiceInvalidTempPathTest extends TestCase
$file->method('getSize')->willReturn(128);
$file->method('getStream')->willReturn($stream);
$service = new MediaService($repository, sys_get_temp_dir(), '/media', 500000);
$postRepo = $this->createMock(PostRepositoryInterface::class);
$service = new MediaService($repository, $postRepo, sys_get_temp_dir(), '/media', 500000);
$this->expectException(StorageException::class);
$this->expectExceptionMessage('Impossible de localiser le fichier temporaire uploadé');

View File

@@ -8,6 +8,7 @@ use App\Media\Exception\InvalidMimeTypeException;
use App\Media\Media;
use App\Media\MediaRepositoryInterface;
use App\Media\MediaService;
use App\Post\PostRepositoryInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
@@ -33,6 +34,8 @@ final class MediaServiceTest extends TestCase
/** @var MediaRepositoryInterface&MockObject */
private MediaRepositoryInterface $repository;
private PostRepositoryInterface $postRepository;
private string $uploadDir;
private MediaService $service;
@@ -40,11 +43,13 @@ final class MediaServiceTest extends TestCase
protected function setUp(): void
{
$this->repository = $this->createMock(MediaRepositoryInterface::class);
$this->postRepository = $this->createMock(PostRepositoryInterface::class);
$this->uploadDir = sys_get_temp_dir() . '/slim_media_test_' . uniqid();
@mkdir($this->uploadDir, 0755, true);
$this->service = new MediaService(
mediaRepository: $this->repository,
postRepository: $this->postRepository,
uploadDir: $this->uploadDir,
uploadUrl: '/media',
maxSize: 5 * 1024 * 1024,
@@ -104,10 +109,13 @@ final class MediaServiceTest extends TestCase
public function testStoreReturnsDuplicateUrl(): void
{
$tmpFile = $this->createMinimalJpeg();
$hash = hash_file('sha256', $tmpFile);
$existing = new Media(7, 'existing.jpg', '/media/existing.jpg', $hash, 1);
$this->repository->method('findByHash')->willReturn($existing);
$existing = new Media(7, 'existing.jpg', '/media/existing.jpg', 'existing-hash', 1);
$this->repository
->expects($this->once())
->method('findByHashForUser')
->with($this->callback(static fn (mixed $value): bool => is_string($value) && $value !== ''), 1)
->willReturn($existing);
$this->repository->expects($this->never())->method('create');
$file = $this->makeUploadedFileFromPath($tmpFile, filesize($tmpFile));
@@ -128,7 +136,7 @@ final class MediaServiceTest extends TestCase
{
$tmpFile = $this->createMinimalJpeg();
$this->repository->method('findByHash')->willReturn(null);
$this->repository->method('findByHashForUser')->willReturn(null);
$this->repository->expects($this->once())->method('create');
$file = $this->makeUploadedFileFromPath($tmpFile, filesize($tmpFile));