mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-03-06 20:54:20 +01:00
More tests!
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Tvdt\Entity\Candidate;
|
||||
@@ -11,9 +12,11 @@ use Tvdt\Entity\Season;
|
||||
use Tvdt\Repository\CandidateRepository;
|
||||
use Tvdt\Repository\SeasonRepository;
|
||||
|
||||
class CandidateRepositoryTest extends KernelTestCase
|
||||
#[CoversClass(CandidateRepository::class)]
|
||||
final class CandidateRepositoryTest extends KernelTestCase
|
||||
{
|
||||
private SeasonRepository $seasonRepository;
|
||||
|
||||
private CandidateRepository $candidateRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
@@ -53,7 +56,7 @@ class CandidateRepositoryTest extends KernelTestCase
|
||||
$krtekSeason,
|
||||
'TWFyaWpu',
|
||||
);
|
||||
$this->assertNull($result);
|
||||
$this->assertNotInstanceOf(Candidate::class, $result);
|
||||
}
|
||||
|
||||
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
|
||||
@@ -64,7 +67,7 @@ class CandidateRepositoryTest extends KernelTestCase
|
||||
$krtekSeason,
|
||||
'TWFyaWpu*',
|
||||
);
|
||||
$this->assertNull($result);
|
||||
$this->assertNotInstanceOf(Candidate::class, $result);
|
||||
}
|
||||
|
||||
public function testGetScores(): void
|
||||
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
@@ -16,11 +17,15 @@ use Tvdt\Repository\CandidateRepository;
|
||||
use Tvdt\Repository\QuestionRepository;
|
||||
use Tvdt\Repository\SeasonRepository;
|
||||
|
||||
class QuestionRepositoryTest extends KernelTestCase
|
||||
#[CoversClass(QuestionRepository::class)]
|
||||
final class QuestionRepositoryTest extends KernelTestCase
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private QuestionRepository $questionRepository;
|
||||
|
||||
private SeasonRepository $seasonRepository;
|
||||
|
||||
private CandidateRepository $candidateRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
@@ -36,9 +41,9 @@ class QuestionRepositoryTest extends KernelTestCase
|
||||
public function testFindNextQuestionReturnsRightQuestion(): void
|
||||
{
|
||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
||||
\assert($krtekSeason instanceof Season);
|
||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
||||
\assert($candidate instanceof Candidate);
|
||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
||||
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
@@ -55,7 +60,7 @@ class QuestionRepositoryTest extends KernelTestCase
|
||||
$this->assertSame('Hoeveel broers heeft de Krtek?', $question->question, 'Getting question a second time fails');
|
||||
|
||||
$quiz = $krtekSeason->quizzes->last();
|
||||
\assert($quiz instanceof Quiz);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
$krtekSeason->activeQuiz = $quiz;
|
||||
$this->entityManager->flush();
|
||||
|
||||
@@ -64,42 +69,43 @@ class QuestionRepositoryTest extends KernelTestCase
|
||||
$this->assertSame('Is de Krtek een man of een vrouw?', $question->question, 'Wrong question after switching season.');
|
||||
}
|
||||
|
||||
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswred(): void
|
||||
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswered(): void
|
||||
{
|
||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
||||
\assert($krtekSeason instanceof Season);
|
||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
||||
\assert($candidate instanceof Candidate);
|
||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
\assert($question instanceof Question);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
$this->answerQuestion($question, $candidate);
|
||||
}
|
||||
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
|
||||
$this->assertNull($question);
|
||||
$this->assertNotInstanceOf(Question::class, $question);
|
||||
}
|
||||
|
||||
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
|
||||
{
|
||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
||||
\assert($krtekSeason instanceof Season);
|
||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
||||
\assert($candidate instanceof Candidate);
|
||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
||||
|
||||
$krtekSeason->activeQuiz = null;
|
||||
$this->entityManager->flush();
|
||||
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
|
||||
$this->assertNull($question);
|
||||
$this->assertNotInstanceOf(Question::class, $question);
|
||||
}
|
||||
|
||||
private function answerQuestion(Question $question, Candidate $candidate): void
|
||||
{
|
||||
$answer = $question->answers->first();
|
||||
\assert($answer instanceof Answer);
|
||||
$this->assertInstanceOf(Answer::class, $answer);
|
||||
$this->entityManager->persist(new GivenAnswer(
|
||||
$candidate,
|
||||
$question->quiz,
|
||||
|
||||
98
tests/Repository/QuizCandidateRepositoryTest.php
Normal file
98
tests/Repository/QuizCandidateRepositoryTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
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);
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$result = $this->quizCandidateRepository->createIfNotExist($quiz, $candidate);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$quizCandidate = $this->quizCandidateRepository->findOneBy([
|
||||
'candidate' => $candidate,
|
||||
'quiz' => $quiz,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(QuizCandidate::class, $quizCandidate);
|
||||
|
||||
$result = $this->quizCandidateRepository->createIfNotExist($quiz, $candidate);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
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);
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->quizCandidateRepository->createIfNotExist($quiz, $candidate);
|
||||
|
||||
$this->quizCandidateRepository->setCorrectionsForCandidate(
|
||||
$quiz, $candidate, 3.5,
|
||||
);
|
||||
|
||||
$quizCandidate = $this->quizCandidateRepository->findOneBy([
|
||||
'candidate' => $candidate,
|
||||
'quiz' => $quiz,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(QuizCandidate::class, $quizCandidate);
|
||||
|
||||
$this->assertEqualsWithDelta(3.5, $quizCandidate->corrections, 0.1);
|
||||
}
|
||||
|
||||
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);
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->quizCandidateRepository->setCorrectionsForCandidate(
|
||||
$quiz, $candidate, 3.5,
|
||||
);
|
||||
}
|
||||
}
|
||||
66
tests/Repository/QuizRepositoryTest.php
Normal file
66
tests/Repository/QuizRepositoryTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
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);
|
||||
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->quizRepository->clearQuiz($quiz);
|
||||
|
||||
$this->entityManager->refresh($krtekSeason);
|
||||
|
||||
$this->assertEmpty($quiz->candidateData);
|
||||
$this->assertEmpty($quiz->eliminations);
|
||||
|
||||
/** @var GivenAnswerRepository $givenAnswerRepository */
|
||||
$givenAnswerRepository = self::getContainer()->get(GivenAnswerRepository::class);
|
||||
$this->assertEmpty($givenAnswerRepository->findBy(['quiz' => $quiz]));
|
||||
}
|
||||
|
||||
public function testDeleteQuiz(): void
|
||||
{
|
||||
$krtekSeason = $this->seasonRepository->findOneBy(['seasonCode' => 'krtek']);
|
||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
||||
$quiz = $krtekSeason->quizzes->last();
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->quizRepository->deleteQuiz($quiz);
|
||||
|
||||
$this->entityManager->refresh($krtekSeason);
|
||||
|
||||
$this->assertCount(1, $krtekSeason->quizzes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user