feat: add AwardTypeRepository::findWithMinActors()
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# define your env variables for the test env here
|
||||
KERNEL_CLASS='App\Kernel'
|
||||
APP_SECRET='$ecretf0rt3st'
|
||||
POSTGRES_DB=app_test
|
||||
|
||||
@@ -33,6 +33,11 @@ doctrine:
|
||||
numeric_functions:
|
||||
Random: App\Doctrine\Extension\Random
|
||||
|
||||
when@test:
|
||||
doctrine:
|
||||
dbal:
|
||||
dbname: app_test
|
||||
|
||||
when@prod:
|
||||
doctrine:
|
||||
orm:
|
||||
|
||||
@@ -21,4 +21,17 @@ class AwardTypeRepository extends ServiceEntityRepository
|
||||
{
|
||||
return parent::findAll();
|
||||
}
|
||||
|
||||
/** @return list<AwardType> */
|
||||
public function findWithMinActors(int $minActors): array
|
||||
{
|
||||
return $this->createQueryBuilder('at')
|
||||
->join('at.awards', 'a')
|
||||
->groupBy('at.id')
|
||||
->having('COUNT(DISTINCT a.actor) >= :minActors')
|
||||
->setParameter('minActors', $minActors)
|
||||
->orderBy('at.name', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
|
||||
68
tests/Repository/AwardTypeRepositoryTest.php
Normal file
68
tests/Repository/AwardTypeRepositoryTest.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Repository;
|
||||
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Award;
|
||||
use App\Entity\AwardType;
|
||||
use App\Repository\AwardTypeRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
class AwardTypeRepositoryTest extends KernelTestCase
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
private AwardTypeRepository $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
||||
$this->repo = self::getContainer()->get(AwardTypeRepository::class);
|
||||
|
||||
// Clean slate
|
||||
$this->em->createQuery('DELETE FROM App\Entity\Award')->execute();
|
||||
$this->em->createQuery('DELETE FROM App\Entity\AwardType')->execute();
|
||||
$this->em->createQuery('DELETE FROM App\Entity\Actor')->execute();
|
||||
}
|
||||
|
||||
public function testFindWithMinActorsFiltersCorrectly(): void
|
||||
{
|
||||
$smallType = new AwardType();
|
||||
$smallType->setName('Small Award')->setPattern('small');
|
||||
$this->em->persist($smallType);
|
||||
|
||||
$bigType = new AwardType();
|
||||
$bigType->setName('Big Award')->setPattern('big');
|
||||
$this->em->persist($bigType);
|
||||
|
||||
for ($i = 0; $i < 6; $i++) {
|
||||
$actor = new Actor();
|
||||
$actor->setName("Actor $i");
|
||||
$this->em->persist($actor);
|
||||
|
||||
$award = new Award();
|
||||
$award->setName("Big Award $i");
|
||||
$award->setActor($actor);
|
||||
$award->setAwardType($bigType);
|
||||
$this->em->persist($award);
|
||||
|
||||
if ($i < 3) {
|
||||
$awardSmall = new Award();
|
||||
$awardSmall->setName("Small Award $i");
|
||||
$awardSmall->setActor($actor);
|
||||
$awardSmall->setAwardType($smallType);
|
||||
$this->em->persist($awardSmall);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
$result = $this->repo->findWithMinActors(5);
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertSame('Big Award', $result[0]->getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user