ltbxd-actorle/tests/Import/ActorSyncerTest.php
2026-04-11 11:55:31 +02:00

116 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Import;
use App\Context\TMDB\MovieCreditsContext;
use App\Entity\Actor;
use App\Entity\Movie;
use App\Entity\MovieRole;
use App\Gateway\TMDBGateway;
use App\Import\ActorSyncer;
use App\Model\TMDB\TMDBMovieCredit;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
class ActorSyncerTest extends TestCase
{
public function testSyncActorsForMovieKeepsMovieActorCollectionInMemory(): void
{
$movie = (new Movie())
->setTmdbId(123)
->setTitle('Inception')
->setLtbxdRef('film/inception');
$tmdbGateway = $this->createMock(TMDBGateway::class);
$tmdbGateway->method('getMovieCredits')->with(123)->willReturn(
new MovieCreditsContext([
new TMDBMovieCredit(
id: 42,
name: 'Leonardo DiCaprio',
popularity: 99.0,
character: 'Cobb',
),
]),
);
$actorRepository = new class implements ObjectRepository {
public function findAll(): array
{
return [];
}
public function findBy(array $criteria, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): array
{
return [];
}
public function findOneBy(array $criteria): ?object
{
return null;
}
public function getClassName(): string
{
return Actor::class;
}
};
$movieRoleRepository = new class implements ObjectRepository {
public function findAll(): array
{
return [];
}
public function findBy(array $criteria, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): array
{
return [];
}
public function findOneBy(array $criteria): ?object
{
return null;
}
public function getClassName(): string
{
return MovieRole::class;
}
public function count(array $criteria = []): int
{
return 0;
}
};
$persisted = [];
$em = $this->createMock(EntityManagerInterface::class);
$em->method('getRepository')->willReturnCallback(
static fn (string $class): ObjectRepository => match ($class) {
Actor::class => $actorRepository,
MovieRole::class => $movieRoleRepository,
default => throw new \InvalidArgumentException("Unexpected repository for {$class}"),
},
);
$em->method('persist')->willReturnCallback(function (object $entity) use (&$persisted): void {
$persisted[] = $entity;
});
$syncer = new ActorSyncer($tmdbGateway, $em);
$syncer->syncActorsForMovie($movie);
self::assertCount(2, $persisted);
self::assertInstanceOf(Actor::class, $persisted[0]);
self::assertInstanceOf(MovieRole::class, $persisted[1]);
self::assertCount(1, $movie->getActors());
$role = $movie->getActors()->first();
self::assertInstanceOf(MovieRole::class, $role);
self::assertSame($movie, $role->getMovie());
self::assertSame($persisted[0], $role->getActor());
self::assertCount(1, $persisted[0]->getMovieRoles());
}
}