From 5fbac8359fbc6696a07e0aead0669af48daa1d1a Mon Sep 17 00:00:00 2001 From: thibaud-leclere Date: Wed, 1 Apr 2026 22:58:04 +0200 Subject: [PATCH] feat: add AwardRepository::findOneRandomByActorAndTypes() --- src/Repository/AwardRepository.php | 21 ++++++++ tests/Repository/AwardRepositoryTest.php | 63 ++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/Repository/AwardRepositoryTest.php diff --git a/src/Repository/AwardRepository.php b/src/Repository/AwardRepository.php index 2ec010e..252f8a1 100644 --- a/src/Repository/AwardRepository.php +++ b/src/Repository/AwardRepository.php @@ -26,4 +26,25 @@ class AwardRepository extends ServiceEntityRepository ->getQuery() ->getOneOrNullResult(); } + + /** + * @param list|null $awardTypeIds null means all types + */ + public function findOneRandomByActorAndTypes(int $actorId, ?array $awardTypeIds): ?Award + { + $qb = $this->createQueryBuilder('a') + ->andWhere('a.actor = :actorId') + ->setParameter('actorId', $actorId); + + if ($awardTypeIds !== null) { + $qb->andWhere('a.awardType IN (:typeIds)') + ->setParameter('typeIds', $awardTypeIds); + } + + return $qb + ->orderBy('RANDOM()') + ->setMaxResults(1) + ->getQuery() + ->getOneOrNullResult(); + } } diff --git a/tests/Repository/AwardRepositoryTest.php b/tests/Repository/AwardRepositoryTest.php new file mode 100644 index 0000000..4d27914 --- /dev/null +++ b/tests/Repository/AwardRepositoryTest.php @@ -0,0 +1,63 @@ +em = self::getContainer()->get(EntityManagerInterface::class); + $this->repo = self::getContainer()->get(AwardRepository::class); + } + + public function testFindOneRandomByActorAndTypesFiltersCorrectly(): void + { + $actor = new Actor(); + $actor->setName('Award Actor Test'); + $this->em->persist($actor); + + $oscarType = new AwardType(); + $oscarType->setName('Oscar')->setPattern('oscar'); + $this->em->persist($oscarType); + + $globeType = new AwardType(); + $globeType->setName('Golden Globe')->setPattern('globe'); + $this->em->persist($globeType); + + $oscar = new Award(); + $oscar->setName('Best Actor Oscar'); + $oscar->setActor($actor); + $oscar->setAwardType($oscarType); + $this->em->persist($oscar); + + $globe = new Award(); + $globe->setName('Best Actor Globe'); + $globe->setActor($actor); + $globe->setAwardType($globeType); + $this->em->persist($globe); + + $this->em->flush(); + + for ($i = 0; $i < 10; $i++) { + $result = $this->repo->findOneRandomByActorAndTypes($actor->getId(), [$oscarType->getId()]); + $this->assertNotNull($result); + $this->assertSame('Best Actor Oscar', $result->getName()); + } + + $result = $this->repo->findOneRandomByActorAndTypes($actor->getId(), null); + $this->assertNotNull($result); + } +}