This commit is contained in:
2025-11-01 10:59:26 +01:00
parent f886f0f6c2
commit 9315b27898
13 changed files with 203 additions and 120 deletions

View File

@@ -6,25 +6,12 @@ namespace Tvdt\Tests\Repository;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tvdt\Entity\Candidate;
use Tvdt\Entity\Season;
use Tvdt\Repository\CandidateRepository;
use Tvdt\Repository\SeasonRepository;
#[CoversClass(CandidateRepository::class)]
final class CandidateRepositoryTest extends KernelTestCase
final class CandidateRepositoryTest extends DatabaseTestCase
{
private SeasonRepository $seasonRepository;
private CandidateRepository $candidateRepository;
protected function setUp(): void
{
$this->seasonRepository = self::getContainer()->get(SeasonRepository::class);
$this->candidateRepository = self::getContainer()->get(CandidateRepository::class);
}
/** @return iterable<string, array{0: string}> */
public static function candidateHashDataProvider(): iterable
{
@@ -36,8 +23,7 @@ final class CandidateRepositoryTest extends KernelTestCase
#[DataProvider('candidateHashDataProvider')]
public function testGetCandidateByHash(string $hash): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
$hash,
@@ -50,8 +36,7 @@ final class CandidateRepositoryTest extends KernelTestCase
public function testGetCandidateByHashUnknownHashReturnsNull(): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$krtekSeason = $this->getSeasonByCode('krtek');
$result = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
'TWFyaWpu',
@@ -61,8 +46,7 @@ final class CandidateRepositoryTest extends KernelTestCase
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$krtekSeason = $this->getSeasonByCode('krtek');
$result = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
'TWFyaWpu*',

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tvdt\Entity\Candidate;
use Tvdt\Entity\Season;
use Tvdt\Entity\User;
use Tvdt\Repository\CandidateRepository;
use Tvdt\Repository\QuestionRepository;
use Tvdt\Repository\QuizCandidateRepository;
use Tvdt\Repository\QuizRepository;
use Tvdt\Repository\SeasonRepository;
use Tvdt\Repository\UserRepository;
abstract class DatabaseTestCase extends KernelTestCase
{
protected private(set) EntityManagerInterface $entityManager;
protected private(set) CandidateRepository $candidateRepository;
protected private(set) QuestionRepository $questionRepository;
protected private(set) QuizCandidateRepository $quizCandidateRepository;
protected private(set) QuizRepository $quizRepository;
protected private(set) SeasonRepository $seasonRepository;
protected private(set) UserRepository $userRepository;
protected function setUp(): void
{
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->candidateRepository = self::getContainer()->get(CandidateRepository::class);
$this->questionRepository = self::getContainer()->get(QuestionRepository::class);
$this->quizCandidateRepository = self::getContainer()->get(QuizCandidateRepository::class);
$this->quizRepository = self::getContainer()->get(QuizRepository::class);
$this->seasonRepository = self::getContainer()->get(SeasonRepository::class);
$this->userRepository = self::getContainer()->get(UserRepository::class);
}
protected function getUserByEmail(string $email): User
{
$user = $this->userRepository->findOneBy(['email' => $email]);
$this->assertInstanceOf(User::class, $user);
return $user;
}
protected function getSeasonByCode(string $code): Season
{
$season = $this->seasonRepository->findOneBySeasonCode($code);
$this->assertInstanceOf(Season::class, $season);
return $season;
}
protected function getCandidateBySeasonAndName(Season $season, string $name): Candidate
{
$candidate = $this->candidateRepository->findOneBy(['season' => $season, 'name' => $name]);
$this->assertInstanceOf(Candidate::class, $candidate);
return $candidate;
}
}

View File

@@ -4,46 +4,21 @@ declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tvdt\Entity\Answer;
use Tvdt\Entity\Candidate;
use Tvdt\Entity\GivenAnswer;
use Tvdt\Entity\Question;
use Tvdt\Entity\Quiz;
use Tvdt\Entity\Season;
use Tvdt\Repository\CandidateRepository;
use Tvdt\Repository\QuestionRepository;
use Tvdt\Repository\SeasonRepository;
#[CoversClass(QuestionRepository::class)]
final class QuestionRepositoryTest extends KernelTestCase
final class QuestionRepositoryTest extends DatabaseTestCase
{
private EntityManagerInterface $entityManager;
private QuestionRepository $questionRepository;
private SeasonRepository $seasonRepository;
private CandidateRepository $candidateRepository;
protected function setUp(): void
{
$container = self::getContainer();
$this->entityManager = $container->get(EntityManagerInterface::class);
$this->questionRepository = $container->get(QuestionRepository::class);
$this->seasonRepository = $container->get(SeasonRepository::class);
$this->candidateRepository = $container->get(CandidateRepository::class);
}
public function testFindNextQuestionReturnsRightQuestion(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertInstanceOf(Question::class, $question);
@@ -71,10 +46,8 @@ final class QuestionRepositoryTest extends KernelTestCase
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswered(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
for ($i = 0; $i < 15; ++$i) {
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
@@ -89,10 +62,8 @@ final class QuestionRepositoryTest extends KernelTestCase
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
$krtekSeason->activeQuiz = null;
$this->entityManager->flush();

View File

@@ -5,39 +5,17 @@ declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tvdt\Entity\Candidate;
use Tvdt\Entity\Quiz;
use Tvdt\Entity\QuizCandidate;
use Tvdt\Entity\Season;
use Tvdt\Repository\CandidateRepository;
use Tvdt\Repository\QuizCandidateRepository;
use Tvdt\Repository\SeasonRepository;
#[CoversClass(QuizCandidateRepository::class)]
final class QuizCandidateRepositoryTest extends KernelTestCase
final class QuizCandidateRepositoryTest extends DatabaseTestCase
{
private SeasonRepository $seasonRepository;
private QuizCandidateRepository $quizCandidateRepository;
private CandidateRepository $candidateRepository;
protected function setUp(): void
{
$container = self::getContainer();
$this->seasonRepository = $container->get(SeasonRepository::class);
$this->quizCandidateRepository = $container->get(QuizCandidateRepository::class);
$this->candidateRepository = $container->get(CandidateRepository::class);
}
public function testCreateIfNotExists(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
$quiz = $krtekSeason->activeQuiz;
$this->assertInstanceOf(Quiz::class, $quiz);
@@ -57,10 +35,8 @@ final class QuizCandidateRepositoryTest extends KernelTestCase
public function testSetCorrectionsForCandidateUpdatesCandidateCorrectly(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
$quiz = $krtekSeason->activeQuiz;
$this->assertInstanceOf(Quiz::class, $quiz);
@@ -82,10 +58,8 @@ final class QuizCandidateRepositoryTest extends KernelTestCase
public function testCannotGiveCorrectionsToCandidateWithoutResult(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $krtekSeason);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
$this->assertInstanceOf(Candidate::class, $candidate);
$krtekSeason = $this->getSeasonByCode('krtek');
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
$quiz = $krtekSeason->activeQuiz;
$this->assertInstanceOf(Quiz::class, $quiz);

View File

@@ -4,37 +4,17 @@ declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tvdt\Entity\Quiz;
use Tvdt\Entity\Season;
use Tvdt\Repository\GivenAnswerRepository;
use Tvdt\Repository\QuizRepository;
use Tvdt\Repository\SeasonRepository;
#[CoversClass(QuizRepository::class)]
final class QuizRepositoryTest extends KernelTestCase
final class QuizRepositoryTest extends DatabaseTestCase
{
private EntityManagerInterface $entityManager;
private SeasonRepository $seasonRepository;
private QuizRepository $quizRepository;
protected function setUp(): void
{
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->seasonRepository = self::getContainer()->get(SeasonRepository::class);
$this->quizRepository = self::getContainer()->get(QuizRepository::class);
parent::setUp();
}
public function testClearQuiz(): void
{
$krtekSeason = $this->seasonRepository->findOneBy(['seasonCode' => 'krtek']);
$this->assertInstanceOf(Season::class, $krtekSeason);
$krtekSeason = $this->getSeasonByCode('krtek');
$quiz = $krtekSeason->activeQuiz;
$this->assertInstanceOf(Quiz::class, $quiz);
@@ -52,8 +32,7 @@ final class QuizRepositoryTest extends KernelTestCase
public function testDeleteQuiz(): void
{
$krtekSeason = $this->seasonRepository->findOneBy(['seasonCode' => 'krtek']);
$this->assertInstanceOf(Season::class, $krtekSeason);
$krtekSeason = $this->getSeasonByCode('krtek');
$quiz = $krtekSeason->quizzes->last();
$this->assertInstanceOf(Quiz::class, $quiz);

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use PHPUnit\Framework\Attributes\CoversClass;
use Tvdt\Entity\Season;
use Tvdt\Repository\SeasonRepository;
#[CoversClass(SeasonRepository::class)]
final class SeasonRepositoryTest extends DatabaseTestCase
{
public function testGetSeasonsForUser(): void
{
$user = $this->getUserByEmail('krtek-admin@example.org');
$seasons = $this->seasonRepository->getSeasonsForUser($user);
$this->assertCount(1, $seasons);
$this->assertSame('krtek', $seasons[0]->seasonCode);
$user = $this->getUserByEmail('user1@example.org');
$seasons = $this->seasonRepository->getSeasonsForUser($user);
$this->assertCount(1, $seasons);
$this->assertSame('bbbbb', $seasons[0]->seasonCode);
}
public function testUserWithMultipleSeasons(): void
{
$user = $this->getUserByEmail('user2@example.org');
$seasons = $this->seasonRepository->getSeasonsForUser($user);
$this->assertCount(2, $seasons);
$this->assertSame('bbbbb', $seasons[0]->seasonCode);
$this->assertSame('krtek', $seasons[1]->seasonCode);
}
public function testGetSeasonsForUserWithoutSeasonsReturnsEmpty(): void
{
$user = $this->getUserByEmail('test@example.org');
$seasons = $this->seasonRepository->getSeasonsForUser($user);
$this->assertEmpty($seasons);
}
public function testFindOneBySeasonCode(): void
{
$season = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $season);
$this->assertSame('krtek', $season->seasonCode);
}
public function testFindOneBySeasonCodeUnknownSeasonReturnsNull(): void
{
$season = $this->seasonRepository->findOneBySeasonCode('invalid');
$this->assertNotInstanceOf(Season::class, $season);
}
}