fix: keep synced actors available for award import
This commit is contained in:
parent
62a0fdb4c6
commit
d9b8853d11
2 changed files with 123 additions and 2 deletions
|
|
@ -41,10 +41,15 @@ readonly class ActorSyncer
|
|||
$existingRole = $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $movie]);
|
||||
if (0 === $existingRole) {
|
||||
$role = new MovieRole()
|
||||
->setMovie($movie)
|
||||
->setActor($actor)
|
||||
->setCharacter($actorModel->character);
|
||||
|
||||
// Keep both sides in sync so downstream code can rely on in-memory collections
|
||||
// before Doctrine flushes and reloads the entities.
|
||||
$role->setMovie($movie);
|
||||
$role->setActor($actor);
|
||||
$movie->addActor($role);
|
||||
$actor->addMovieRole($role);
|
||||
|
||||
$this->em->persist($role);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
116
tests/Import/ActorSyncerTest.php
Normal file
116
tests/Import/ActorSyncerTest.php
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue