Files
netslim-blog/tests/Post/PostMediaUsageReaderTest.php
2026-03-20 22:16:20 +01:00

90 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Post;
use App\Post\Domain\Repository\PostMediaUsageRepositoryInterface;
use App\Post\Domain\ValueObject\PostMediaUsageReference;
use App\Post\Infrastructure\PostMediaUsageReader;
use Netig\Netslim\Media\Contracts\MediaUsageReference;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class PostMediaUsageReaderTest extends TestCase
{
/** @var PostMediaUsageRepositoryInterface&MockObject */
private PostMediaUsageRepositoryInterface $repository;
private PostMediaUsageReader $reader;
protected function setUp(): void
{
$this->repository = $this->createMock(PostMediaUsageRepositoryInterface::class);
$this->reader = new PostMediaUsageReader($this->repository);
}
public function testCountUsagesDelegatesToRepository(): void
{
$this->repository
->expects($this->once())
->method('countUsages')
->with(12)
->willReturn(4);
self::assertSame(4, $this->reader->countUsages(12));
}
public function testCountUsagesByMediaIdsDelegatesToRepository(): void
{
$this->repository
->expects($this->once())
->method('countUsagesByMediaIds')
->with([12, 18])
->willReturn([12 => 4, 18 => 1]);
self::assertSame([12 => 4, 18 => 1], $this->reader->countUsagesByMediaIds([12, 18]));
}
public function testFindUsagesDelegatesToRepository(): void
{
$repositoryReferences = [
new PostMediaUsageReference(8, 'Titre', '/admin/posts/edit/8'),
];
$expectedReferences = [
new MediaUsageReference(8, 'Titre', '/admin/posts/edit/8'),
];
$this->repository
->expects($this->once())
->method('findUsages')
->with(12, 3)
->willReturn($repositoryReferences);
self::assertEquals($expectedReferences, $this->reader->findUsages(12, 3));
}
public function testFindUsagesByMediaIdsDelegatesToRepository(): void
{
$repositoryReferences = [
12 => [new PostMediaUsageReference(8, 'Titre', '/admin/posts/edit/8')],
18 => [new PostMediaUsageReference(14, 'Autre', '/admin/posts/edit/14')],
];
$expectedReferences = [
12 => [new MediaUsageReference(8, 'Titre', '/admin/posts/edit/8')],
18 => [new MediaUsageReference(14, 'Autre', '/admin/posts/edit/14')],
];
$this->repository
->expects($this->once())
->method('findUsagesByMediaIds')
->with([12, 18], 3)
->willReturn($repositoryReferences);
self::assertEquals($expectedReferences, $this->reader->findUsagesByMediaIds([12, 18], 3));
}
}