116 lines
4.3 KiB
PHP
116 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Post;
|
|
|
|
use App\Post\Application\PostApplicationService;
|
|
use App\Post\Application\UseCase\CreatePost;
|
|
use App\Post\Application\UseCase\DeletePost;
|
|
use App\Post\Application\UseCase\UpdatePost;
|
|
use App\Post\Domain\Entity\Post;
|
|
use App\Post\Domain\Repository\PostMediaUsageRepositoryInterface;
|
|
use App\Post\Domain\Repository\PostRepositoryInterface;
|
|
use App\Post\Domain\Service\PostMediaReferenceExtractorInterface;
|
|
use App\Post\Domain\Service\PostSlugGenerator;
|
|
use Netig\Netslim\Kernel\Html\Application\HtmlSanitizerInterface;
|
|
use Netig\Netslim\Kernel\Persistence\Application\TransactionManagerInterface;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
|
final class PostServiceCoverageTest extends TestCase
|
|
{
|
|
/** @var PostRepositoryInterface&MockObject */
|
|
private PostRepositoryInterface $repository;
|
|
|
|
/** @var HtmlSanitizerInterface&MockObject */
|
|
private HtmlSanitizerInterface $sanitizer;
|
|
|
|
private PostApplicationService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->repository = $this->createMock(PostRepositoryInterface::class);
|
|
$this->sanitizer = $this->createMock(HtmlSanitizerInterface::class);
|
|
$slugGenerator = new PostSlugGenerator();
|
|
$transactionManager = new class () implements TransactionManagerInterface {
|
|
public function run(callable $operation): mixed
|
|
{
|
|
return $operation();
|
|
}
|
|
};
|
|
$referenceExtractor = new class () implements PostMediaReferenceExtractorInterface {
|
|
public function extractMediaIds(string $html): array
|
|
{
|
|
return [];
|
|
}
|
|
};
|
|
$usageRepository = new class () implements PostMediaUsageRepositoryInterface {
|
|
public function countUsages(int $mediaId): int
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
public function countUsagesByMediaIds(array $mediaIds): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function findUsages(int $mediaId, int $limit = 5): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function findUsagesByMediaIds(array $mediaIds, int $limit = 5): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function syncPostMedia(int $postId, array $mediaIds): void {}
|
|
};
|
|
$this->service = new PostApplicationService(
|
|
$this->repository,
|
|
new CreatePost($this->repository, $this->sanitizer, $slugGenerator, $transactionManager, $referenceExtractor, $usageRepository),
|
|
new UpdatePost($this->repository, $this->sanitizer, $slugGenerator, $transactionManager, $referenceExtractor, $usageRepository),
|
|
new DeletePost($this->repository),
|
|
);
|
|
}
|
|
|
|
public function testGetAllPostsPassesCategoryIdToRepository(): void
|
|
{
|
|
$posts = [$this->makePost(1, 'Titre', 'titre')];
|
|
$this->repository->expects($this->once())->method('findAll')->with(9)->willReturn($posts);
|
|
|
|
self::assertSame($posts, $this->service->findAll(9));
|
|
}
|
|
|
|
public function testGetRecentPostsPassesLimitToRepository(): void
|
|
{
|
|
$posts = [$this->makePost(2, 'Titre', 'titre-2')];
|
|
$this->repository->expects($this->once())->method('findRecent')->with(7)->willReturn($posts);
|
|
|
|
self::assertSame($posts, $this->service->findRecent(7));
|
|
}
|
|
|
|
public function testCreatePostAddsNumericSuffixWhenBaseSlugAlreadyExists(): void
|
|
{
|
|
$this->sanitizer->expects($this->once())->method('sanitize')->with('<p>Brut</p>')->willReturn('<p>Sur</p>');
|
|
$this->repository->expects($this->exactly(2))
|
|
->method('slugExists')
|
|
->withAnyParameters()
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
$this->repository->expects($this->once())
|
|
->method('create')
|
|
->with($this->isInstanceOf(Post::class), 'mon-titre-1', 4, 8)
|
|
->willReturn(99);
|
|
|
|
self::assertSame(99, $this->service->create('Mon titre', '<p>Brut</p>', 4, 8));
|
|
}
|
|
|
|
private function makePost(int $id, string $title, string $slug, string $content = '<p>Contenu</p>'): Post
|
|
{
|
|
return new Post($id, $title, $content, $slug);
|
|
}
|
|
}
|