Working state
This commit is contained in:
@@ -9,7 +9,7 @@ use App\Shared\Http\FlashServiceInterface;
|
||||
use App\Shared\Http\SessionManagerInterface;
|
||||
use App\User\Exception\WeakPasswordException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour AccountController.
|
||||
@@ -19,7 +19,7 @@ use Tests\ControllerTestCase;
|
||||
* incorrect, erreur inattendue et succès.
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class AccountControllerTest extends ControllerTestCase
|
||||
final class AccountControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
|
||||
@@ -9,7 +9,7 @@ use App\Shared\Http\ClientIpResolver;
|
||||
use App\Shared\Http\FlashServiceInterface;
|
||||
use App\User\User;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour AuthController.
|
||||
@@ -19,7 +19,7 @@ use Tests\ControllerTestCase;
|
||||
* aucune base de données, aucun serveur HTTP n'est requis.
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class AuthControllerTest extends ControllerTestCase
|
||||
final class AuthControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
|
||||
@@ -12,7 +12,7 @@ use App\Shared\Http\FlashServiceInterface;
|
||||
use App\User\Exception\WeakPasswordException;
|
||||
use App\User\User;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour PasswordResetController.
|
||||
@@ -29,7 +29,7 @@ use Tests\ControllerTestCase;
|
||||
* - reset() couvre 5 chemins de sortie (token vide, mismatch, trop court, invalide, succès)
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class PasswordResetControllerTest extends ControllerTestCase
|
||||
final class PasswordResetControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
|
||||
@@ -7,8 +7,9 @@ use App\Category\Category;
|
||||
use App\Category\CategoryController;
|
||||
use App\Category\CategoryServiceInterface;
|
||||
use App\Shared\Http\FlashServiceInterface;
|
||||
use App\Shared\Pagination\PaginatedResult;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour CategoryController.
|
||||
@@ -18,7 +19,7 @@ use Tests\ControllerTestCase;
|
||||
* suppression avec catégorie introuvable, succès et erreur métier.
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class CategoryControllerTest extends ControllerTestCase
|
||||
final class CategoryControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
@@ -51,7 +52,7 @@ final class CategoryControllerTest extends ControllerTestCase
|
||||
*/
|
||||
public function testIndexRendersWithCategories(): void
|
||||
{
|
||||
$this->categoryService->method('findAll')->willReturn([]);
|
||||
$this->categoryService->method('findPaginated')->willReturn(new PaginatedResult([], 0, 1, 20));
|
||||
|
||||
$this->view->expects($this->once())
|
||||
->method('render')
|
||||
|
||||
@@ -19,7 +19,7 @@ use Slim\Psr7\Response as SlimResponse;
|
||||
* sans passer par le routeur Slim — les middlewares sont testés séparément.
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
abstract class ControllerTestCase extends TestCase
|
||||
abstract class ControllerTestBase extends TestCase
|
||||
{
|
||||
// ── Factories ────────────────────────────────────────────────────
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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é');
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -37,10 +37,16 @@ final class PostConcurrentUpdateIntegrationTest extends TestCase
|
||||
$realRepo = new PostRepository($this->db);
|
||||
$repo = new class($realRepo) implements PostRepositoryInterface {
|
||||
private bool $deleted = false;
|
||||
|
||||
public function __construct(private readonly PostRepository $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); }
|
||||
public function countAll(?int $categoryId = null): int { return $this->inner->countAll($categoryId); }
|
||||
public function findRecent(int $limit): array { return $this->inner->findRecent($limit); }
|
||||
public function findByUserId(int $userId, ?int $categoryId = null): array { return $this->inner->findByUserId($userId, $categoryId); }
|
||||
public function findByUserPage(int $userId, int $limit, int $offset, ?int $categoryId = null): array { return $this->inner->findByUserPage($userId, $limit, $offset, $categoryId); }
|
||||
public function countByUserId(int $userId, ?int $categoryId = null): int { return $this->inner->countByUserId($userId, $categoryId); }
|
||||
public function findBySlug(string $slug): ?Post { return $this->inner->findBySlug($slug); }
|
||||
public function findById(int $id): ?Post { return $this->inner->findById($id); }
|
||||
public function create(Post $post, string $slug, int $authorId, ?int $categoryId): int { return $this->inner->create($post, $slug, $authorId, $categoryId); }
|
||||
@@ -53,7 +59,11 @@ final class PostConcurrentUpdateIntegrationTest extends TestCase
|
||||
}
|
||||
public function delete(int $id): int { return $this->inner->delete($id); }
|
||||
public function search(string $query, ?int $categoryId = null, ?int $authorId = null): array { return $this->inner->search($query, $categoryId, $authorId); }
|
||||
public function searchPage(string $query, int $limit, int $offset, ?int $categoryId = null, ?int $authorId = null): array { return $this->inner->searchPage($query, $limit, $offset, $categoryId, $authorId); }
|
||||
public function countSearch(string $query, ?int $categoryId = null, ?int $authorId = null): int { return $this->inner->countSearch($query, $categoryId, $authorId); }
|
||||
public function slugExists(string $slug, ?int $excludeId = null): bool { return $this->inner->slugExists($slug, $excludeId); }
|
||||
public function countByEmbeddedMediaUrl(string $url): int { return $this->inner->countByEmbeddedMediaUrl($url); }
|
||||
public function findByEmbeddedMediaUrl(string $url, int $limit = 5): array { return $this->inner->findByEmbeddedMediaUrl($url, $limit); }
|
||||
};
|
||||
|
||||
$sanitizer = new class implements HtmlSanitizerInterface {
|
||||
|
||||
@@ -11,9 +11,10 @@ use App\Post\PostServiceInterface;
|
||||
use App\Shared\Exception\NotFoundException;
|
||||
use App\Shared\Http\FlashServiceInterface;
|
||||
use App\Shared\Http\SessionManagerInterface;
|
||||
use App\Shared\Pagination\PaginatedResult;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour PostController.
|
||||
@@ -28,7 +29,7 @@ use Tests\ControllerTestCase;
|
||||
* - delete() : 404, droits insuffisants, succès
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class PostControllerTest extends ControllerTestCase
|
||||
final class PostControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
@@ -73,8 +74,8 @@ final class PostControllerTest extends ControllerTestCase
|
||||
*/
|
||||
public function testIndexCallsGetAllPostsWithNoFilter(): void
|
||||
{
|
||||
$this->postService->expects($this->once())->method('getAllPosts')->with(null)->willReturn([]);
|
||||
$this->postService->expects($this->never())->method('searchPosts');
|
||||
$this->postService->expects($this->once())->method('getAllPostsPaginated')->with(1, 6, null)->willReturn(new PaginatedResult([], 0, 1, 6));
|
||||
$this->postService->expects($this->never())->method('searchPostsPaginated');
|
||||
|
||||
$res = $this->controller->index($this->makeGet('/'), $this->makeResponse());
|
||||
|
||||
@@ -87,10 +88,10 @@ final class PostControllerTest extends ControllerTestCase
|
||||
public function testIndexCallsSearchPostsWhenQueryParamPresent(): void
|
||||
{
|
||||
$this->postService->expects($this->once())
|
||||
->method('searchPosts')
|
||||
->with('php', null)
|
||||
->willReturn([]);
|
||||
$this->postService->expects($this->never())->method('getAllPosts');
|
||||
->method('searchPostsPaginated')
|
||||
->with('php', 1, 6, null)
|
||||
->willReturn(new PaginatedResult([], 0, 1, 6));
|
||||
$this->postService->expects($this->never())->method('getAllPostsPaginated');
|
||||
|
||||
$this->controller->index($this->makeGet('/', ['q' => 'php']), $this->makeResponse());
|
||||
}
|
||||
@@ -104,9 +105,9 @@ final class PostControllerTest extends ControllerTestCase
|
||||
$this->categoryService->expects($this->once())->method('findBySlug')->with('php')->willReturn($category);
|
||||
|
||||
$this->postService->expects($this->once())
|
||||
->method('getAllPosts')
|
||||
->with(3)
|
||||
->willReturn([]);
|
||||
->method('getAllPostsPaginated')
|
||||
->with(1, 6, 3)
|
||||
->willReturn(new PaginatedResult([], 0, 1, 6));
|
||||
|
||||
$this->controller->index(
|
||||
$this->makeGet('/', ['categorie' => 'php']),
|
||||
@@ -165,8 +166,8 @@ final class PostControllerTest extends ControllerTestCase
|
||||
$this->sessionManager->method('isAdmin')->willReturn(true);
|
||||
$this->sessionManager->method('isEditor')->willReturn(false);
|
||||
|
||||
$this->postService->expects($this->once())->method('getAllPosts')->willReturn([]);
|
||||
$this->postService->expects($this->never())->method('getPostsByUserId');
|
||||
$this->postService->expects($this->once())->method('getAllPostsPaginated')->with(1, 12, null)->willReturn(new PaginatedResult([], 0, 1, 12));
|
||||
$this->postService->expects($this->never())->method('getPostsByUserIdPaginated');
|
||||
|
||||
$res = $this->controller->admin($this->makeGet('/admin/posts'), $this->makeResponse());
|
||||
|
||||
@@ -182,8 +183,8 @@ final class PostControllerTest extends ControllerTestCase
|
||||
$this->sessionManager->method('isEditor')->willReturn(false);
|
||||
$this->sessionManager->method('getUserId')->willReturn(5);
|
||||
|
||||
$this->postService->expects($this->once())->method('getPostsByUserId')->with(5, null)->willReturn([]);
|
||||
$this->postService->expects($this->never())->method('getAllPosts');
|
||||
$this->postService->expects($this->once())->method('getPostsByUserIdPaginated')->with(5, 1, 12, null)->willReturn(new PaginatedResult([], 0, 1, 12));
|
||||
$this->postService->expects($this->never())->method('getAllPostsPaginated');
|
||||
|
||||
$this->controller->admin($this->makeGet('/admin/posts'), $this->makeResponse());
|
||||
}
|
||||
@@ -197,9 +198,9 @@ final class PostControllerTest extends ControllerTestCase
|
||||
$this->sessionManager->method('isEditor')->willReturn(false);
|
||||
|
||||
$this->postService->expects($this->once())
|
||||
->method('searchPosts')
|
||||
->with('php', null, null)
|
||||
->willReturn([]);
|
||||
->method('searchPostsPaginated')
|
||||
->with('php', 1, 12, null, null)
|
||||
->willReturn(new PaginatedResult([], 0, 1, 12));
|
||||
|
||||
$this->controller->admin(
|
||||
$this->makeGet('/admin/posts', ['q' => 'php']),
|
||||
@@ -492,7 +493,7 @@ final class PostControllerTest extends ControllerTestCase
|
||||
* Crée une entité Post de test avec les paramètres minimaux.
|
||||
*
|
||||
* Nommé buildPostEntity (et non makePost) pour ne pas masquer
|
||||
* ControllerTestCase::makePost() qui forge une requête HTTP.
|
||||
* ControllerTestBase::makePost() qui forge une requête HTTP.
|
||||
*/
|
||||
private function buildPostEntity(
|
||||
int $id,
|
||||
|
||||
@@ -143,9 +143,7 @@ final class PostRepositoryTest extends TestCase
|
||||
|
||||
$stmt->expects($this->once())
|
||||
->method('execute')
|
||||
->with($this->callback(fn (array $p): bool =>
|
||||
isset($p[':category_id']) && $p[':category_id'] === 3
|
||||
));
|
||||
->with([':category_id' => 3]);
|
||||
|
||||
$this->repository->findAll(3);
|
||||
}
|
||||
@@ -217,9 +215,7 @@ final class PostRepositoryTest extends TestCase
|
||||
|
||||
$stmt->expects($this->once())
|
||||
->method('execute')
|
||||
->with($this->callback(fn (array $p): bool =>
|
||||
isset($p[':author_id']) && $p[':author_id'] === 7
|
||||
));
|
||||
->with([':author_id' => 7]);
|
||||
|
||||
$this->repository->findByUserId(7);
|
||||
}
|
||||
@@ -234,11 +230,7 @@ final class PostRepositoryTest extends TestCase
|
||||
|
||||
$stmt->expects($this->once())
|
||||
->method('execute')
|
||||
->with($this->callback(fn (array $p): bool =>
|
||||
isset($p[':author_id'], $p[':category_id'])
|
||||
&& $p[':author_id'] === 7
|
||||
&& $p[':category_id'] === 3
|
||||
));
|
||||
->with([':author_id' => 7, ':category_id' => 3]);
|
||||
|
||||
$this->repository->findByUserId(7, 3);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use App\Post\Post;
|
||||
use App\Post\PostServiceInterface;
|
||||
use App\Post\RssController;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour RssController.
|
||||
@@ -20,7 +20,7 @@ use Tests\ControllerTestCase;
|
||||
* - Appel à getRecentPosts() avec la constante FEED_LIMIT (20)
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class RssControllerTest extends ControllerTestCase
|
||||
final class RssControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var PostServiceInterface&MockObject */
|
||||
private PostServiceInterface $postService;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Tests\User;
|
||||
|
||||
use App\Shared\Http\FlashServiceInterface;
|
||||
use App\Shared\Http\SessionManagerInterface;
|
||||
use App\Shared\Pagination\PaginatedResult;
|
||||
use App\User\Exception\DuplicateEmailException;
|
||||
use App\User\Exception\DuplicateUsernameException;
|
||||
use App\User\Exception\WeakPasswordException;
|
||||
@@ -12,7 +13,7 @@ use App\User\User;
|
||||
use App\User\UserController;
|
||||
use App\User\UserServiceInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\ControllerTestCase;
|
||||
use Tests\ControllerTestBase;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour UserController.
|
||||
@@ -25,7 +26,7 @@ use Tests\ControllerTestCase;
|
||||
* - delete() : introuvable, cible admin, soi-même, succès
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class UserControllerTest extends ControllerTestCase
|
||||
final class UserControllerTest extends ControllerTestBase
|
||||
{
|
||||
/** @var \Slim\Views\Twig&MockObject */
|
||||
private \Slim\Views\Twig $view;
|
||||
@@ -63,7 +64,7 @@ final class UserControllerTest extends ControllerTestCase
|
||||
*/
|
||||
public function testIndexRendersWithUserList(): void
|
||||
{
|
||||
$this->userService->method('findAll')->willReturn([]);
|
||||
$this->userService->method('findPaginated')->willReturn(new PaginatedResult([], 0, 1, 20));
|
||||
$this->sessionManager->method('getUserId')->willReturn(1);
|
||||
|
||||
$this->view->expects($this->once())
|
||||
|
||||
@@ -235,7 +235,7 @@ final class UserServiceTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* updateRole() accepte les trois rôles valides sans lever d'exception.
|
||||
* updateRole() accepte uniquement les rôles attribuables depuis l'interface.
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('validRolesProvider')]
|
||||
public function testUpdateRoleAcceptsAllValidRoles(string $role): void
|
||||
@@ -254,7 +254,6 @@ final class UserServiceTest extends TestCase
|
||||
return [
|
||||
'user' => [User::ROLE_USER],
|
||||
'editor' => [User::ROLE_EDITOR],
|
||||
'admin' => [User::ROLE_ADMIN],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user