feat: add AwardRepository::findOneRandomByActorAndTypes()
This commit is contained in:
@@ -26,4 +26,25 @@ class AwardRepository extends ServiceEntityRepository
|
|||||||
->getQuery()
|
->getQuery()
|
||||||
->getOneOrNullResult();
|
->getOneOrNullResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int>|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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
63
tests/Repository/AwardRepositoryTest.php
Normal file
63
tests/Repository/AwardRepositoryTest.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\Repository;
|
||||||
|
|
||||||
|
use App\Entity\Actor;
|
||||||
|
use App\Entity\Award;
|
||||||
|
use App\Entity\AwardType;
|
||||||
|
use App\Repository\AwardRepository;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
|
||||||
|
class AwardRepositoryTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
private EntityManagerInterface $em;
|
||||||
|
private AwardRepository $repo;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user