Some tests

This commit is contained in:
2025-10-31 20:41:10 +01:00
parent fc273638ad
commit e41bedce8d
24 changed files with 562 additions and 81 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Tvdt\Entity\Season;
use Tvdt\Repository\SeasonRepository;
final class ClaimSeasonCommandTest extends KernelTestCase
{
private SeasonRepository $seasonRepository;
private CommandTester $commandTester;
protected function setUp(): void
{
$container = self::getContainer();
\assert(self::$kernel instanceof KernelInterface);
$this->seasonRepository = $container->get(SeasonRepository::class);
$application = new Application(self::$kernel);
$command = $application->find('tvdt:claim-season');
$this->commandTester = new CommandTester($command);
}
public function testSeasonClaim(): void
{
$this->commandTester->execute([
'season-code' => 'krtek',
'email' => 'test@example.org',
]);
$season = $this->seasonRepository->findOneBySeasonCode('krtek');
\assert($season instanceof Season);
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
$this->assertCount(1, $season->owners);
}
public function testInvalidEmalFails(): void
{
$this->commandTester->execute([
'season-code' => 'krtek',
'email' => 'nonexisting@example.org',
]);
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
$this->commandTester->getStatusCode();
}
public function testInvalidSeasonCodeFails(): void
{
$this->commandTester->execute([
'season-code' => 'dhadk',
'email' => 'test@example.org',
]);
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
$this->commandTester->getStatusCode();
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Tvdt\Entity\User;
use Tvdt\Repository\UserRepository;
final class MakeAdminCommandTest extends KernelTestCase
{
private UserRepository $userRepository;
private CommandTester $commandTester;
protected function setUp(): void
{
$container = self::getContainer();
\assert(self::$kernel instanceof KernelInterface);
$this->userRepository = $container->get(UserRepository::class);
$application = new Application(self::$kernel);
$command = $application->find('tvdt:make-admin');
$this->commandTester = new CommandTester($command);
}
public function testMakeAdmin(): void
{
$this->commandTester->execute([
'email' => 'test@example.org',
]);
$user = $this->userRepository->findOneBy(['email' => 'test@example.org']);
\assert($user instanceof User);
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
$this->assertContains('ROLE_ADMIN', $user->roles);
}
public function testInvalidEmalFails(): void
{
$this->commandTester->execute([
'email' => 'nonexisting@example.org',
]);
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
$this->commandTester->getStatusCode();
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Repository;
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;
class CandidateRepositoryTest extends KernelTestCase
{
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
{
yield 'Normal' => ['Q2xhdWRpYQ'];
yield 'lowercase' => ['Y2xhdWRpYQ'];
yield 'UPPERCASE' => ['Q0xBVURJQQ'];
}
#[DataProvider('candidateHashDataProvider')]
public function testGetCandidateByHash(string $hash): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$candidate = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
$hash,
);
$this->assertInstanceOf(Candidate::class, $candidate);
$this->assertSame('Claudia', $candidate->name);
}
public function testGetCandidateByHashUnknownHashReturnsNull(): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$result = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
'TWFyaWpu',
);
$this->assertNull($result);
}
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
{
/** @var Season $krtekSeason */
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
$result = $this->candidateRepository->getCandidateByHash(
$krtekSeason,
'TWFyaWpu*',
);
$this->assertNull($result);
}
public function testGetScores(): void
{
$this->markTestIncomplete('TODO: Make fixtures first and write good test.');
}
}

View File

@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace Tvdt\Tests\Repository;
use Doctrine\ORM\EntityManagerInterface;
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;
class QuestionRepositoryTest extends KernelTestCase
{
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');
\assert($krtekSeason instanceof Season);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
\assert($candidate instanceof Candidate);
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertInstanceOf(Question::class, $question);
$this->assertSame('Is de Krtek een man of een vrouw?', $question->question, 'Wrong first question');
$this->answerQuestion($question, $candidate);
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertInstanceOf(Question::class, $question);
$this->assertSame('Hoeveel broers heeft de Krtek?', $question->question, 'Wrong second question');
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertInstanceOf(Question::class, $question);
$this->assertSame('Hoeveel broers heeft de Krtek?', $question->question, 'Getting question a second time fails');
$quiz = $krtekSeason->quizzes->last();
\assert($quiz instanceof Quiz);
$krtekSeason->activeQuiz = $quiz;
$this->entityManager->flush();
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertInstanceOf(Question::class, $question);
$this->assertSame('Is de Krtek een man of een vrouw?', $question->question, 'Wrong question after switching season.');
}
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswred(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
\assert($krtekSeason instanceof Season);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
\assert($candidate instanceof Candidate);
for ($i = 0; $i < 15; ++$i) {
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
\assert($question instanceof Question);
$this->answerQuestion($question, $candidate);
}
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertNull($question);
}
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
{
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
\assert($krtekSeason instanceof Season);
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
\assert($candidate instanceof Candidate);
$krtekSeason->activeQuiz = null;
$this->entityManager->flush();
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
$this->assertNull($question);
}
private function answerQuestion(Question $question, Candidate $candidate): void
{
$answer = $question->answers->first();
\assert($answer instanceof Answer);
$this->entityManager->persist(new GivenAnswer(
$candidate,
$question->quiz,
$answer,
));
$this->entityManager->flush();
}
}