mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-03-06 04:44:19 +01:00
Create Testcoverage and upgrade Symfomy and PHP
* Some tests * More tests! * Tests 3 * Move getScores from Candidate to Quiz * Add some suggestions for future refactoring * - **Add Gedmo doctrine-extensions and Stof bundle integration** - Added `stof/doctrine-extensions-bundle` and `gedmo/doctrine-extensions` dependencies. - Integrated `Timestampable` behavior for `Created` fields in entities. - Updated `bundles.php` to register StofDoctrineExtensionsBundle. - Added configuration for the Stof bundle. - Simplified `SeasonVoter` with `match` expression and added new tests. - Minor fixes and adjustments across various files. * WIP * All the tests * Base64 tests * Symfomny 7.4.0 * Update * Update recipe * PHP 8.5 * Rector changes * More 8.5 * Things
This commit is contained in:
70
tests/Command/ClaimSeasonCommandTest.php
Normal file
70
tests/Command/ClaimSeasonCommandTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Command;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
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\Command\ClaimSeasonCommand;
|
||||
use Tvdt\Entity\Season;
|
||||
use Tvdt\Repository\SeasonRepository;
|
||||
|
||||
#[CoversClass(ClaimSeasonCommand::class)]
|
||||
final class ClaimSeasonCommandTest extends KernelTestCase
|
||||
{
|
||||
private SeasonRepository $seasonRepository;
|
||||
|
||||
private CommandTester $commandTester;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$container = self::getContainer();
|
||||
|
||||
$this->assertInstanceOf(KernelInterface::class, self::$kernel);
|
||||
|
||||
$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');
|
||||
|
||||
$this->assertInstanceOf(Season::class, $season);
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
|
||||
$this->assertCount(3, $season->owners);
|
||||
}
|
||||
|
||||
public function testInvalidEmailFails(): void
|
||||
{
|
||||
$this->commandTester->execute([
|
||||
'season-code' => 'krtek',
|
||||
'email' => 'nonexisting@example.org',
|
||||
]);
|
||||
|
||||
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
|
||||
}
|
||||
|
||||
public function testInvalidSeasonCodeFails(): void
|
||||
{
|
||||
$this->commandTester->execute([
|
||||
'season-code' => 'dhadk',
|
||||
'email' => 'test@example.org',
|
||||
]);
|
||||
|
||||
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
|
||||
}
|
||||
}
|
||||
57
tests/Command/MakeAdminCommandTest.php
Normal file
57
tests/Command/MakeAdminCommandTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Command;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
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\Command\MakeAdminCommand;
|
||||
use Tvdt\Entity\User;
|
||||
use Tvdt\Repository\UserRepository;
|
||||
|
||||
#[CoversClass(MakeAdminCommand::class)]
|
||||
final class MakeAdminCommandTest extends KernelTestCase
|
||||
{
|
||||
private UserRepository $userRepository;
|
||||
|
||||
private CommandTester $commandTester;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$container = self::getContainer();
|
||||
|
||||
$this->assertInstanceOf(KernelInterface::class, self::$kernel);
|
||||
|
||||
$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']);
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
|
||||
$this->assertContains('ROLE_ADMIN', $user->roles);
|
||||
}
|
||||
|
||||
public function testInvalidEmailFails(): void
|
||||
{
|
||||
$this->commandTester->execute([
|
||||
'email' => 'nonexisting@example.org',
|
||||
]);
|
||||
|
||||
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
|
||||
}
|
||||
}
|
||||
41
tests/Helpers/Base64Test.php
Normal file
41
tests/Helpers/Base64Test.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Helpers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Safe\Exceptions\UrlException;
|
||||
use Tvdt\Helpers\Base64;
|
||||
|
||||
final class Base64Test extends TestCase
|
||||
{
|
||||
public function testBase64UrlEncode(): void
|
||||
{
|
||||
$this->assertSame('TWFyaWpu', Base64::base64UrlEncode('Marijn'));
|
||||
$this->assertSame('UGhpbGluZQ', Base64::base64UrlEncode('Philine'));
|
||||
|
||||
$this->assertSame('_g', Base64::base64UrlEncode(\chr(254)));
|
||||
$this->assertSame('-g', Base64::base64UrlEncode(\chr(250)));
|
||||
}
|
||||
|
||||
public function testBase64UrlDecode(): void
|
||||
{
|
||||
$this->assertSame('Marijn', Base64::base64UrlDecode('TWFyaWpu'));
|
||||
$this->assertSame('Philine', Base64::base64UrlDecode('UGhpbGluZQ'));
|
||||
|
||||
$this->assertSame(\chr(254), Base64::base64UrlDecode('_g'));
|
||||
$this->assertSame(\chr(250), Base64::base64UrlDecode('-g'));
|
||||
}
|
||||
|
||||
public function testBase64UrlDecodeCanHandlePadding(): void
|
||||
{
|
||||
$this->assertSame('Philine', Base64::base64UrlDecode('UGhpbGluZQ=='));
|
||||
}
|
||||
|
||||
public function testBase64UrlDecodeThrowsExceptionOnInvalidInput(): void
|
||||
{
|
||||
$this->expectException(UrlException::class);
|
||||
Base64::base64UrlDecode('Philine==');
|
||||
}
|
||||
}
|
||||
56
tests/Repository/CandidateRepositoryTest.php
Normal file
56
tests/Repository/CandidateRepositoryTest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tvdt\Entity\Candidate;
|
||||
use Tvdt\Repository\CandidateRepository;
|
||||
|
||||
#[CoversClass(CandidateRepository::class)]
|
||||
final class CandidateRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
/** @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
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->candidateRepository->getCandidateByHash(
|
||||
$krtekSeason,
|
||||
$hash,
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
||||
|
||||
$this->assertSame('Claudia', $candidate->name);
|
||||
}
|
||||
|
||||
public function testGetCandidateByHashUnknownHashReturnsNull(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$result = $this->candidateRepository->getCandidateByHash(
|
||||
$krtekSeason,
|
||||
'TWFyaWpu',
|
||||
);
|
||||
$this->assertNotInstanceOf(Candidate::class, $result);
|
||||
}
|
||||
|
||||
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$result = $this->candidateRepository->getCandidateByHash(
|
||||
$krtekSeason,
|
||||
'TWFyaWpu*',
|
||||
);
|
||||
$this->assertNotInstanceOf(Candidate::class, $result);
|
||||
}
|
||||
}
|
||||
70
tests/Repository/DatabaseTestCase.php
Normal file
70
tests/Repository/DatabaseTestCase.php
Normal 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;
|
||||
}
|
||||
}
|
||||
87
tests/Repository/QuestionRepositoryTest.php
Normal file
87
tests/Repository/QuestionRepositoryTest.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Tvdt\Entity\Answer;
|
||||
use Tvdt\Entity\Candidate;
|
||||
use Tvdt\Entity\GivenAnswer;
|
||||
use Tvdt\Entity\Question;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Repository\QuestionRepository;
|
||||
|
||||
#[CoversClass(QuestionRepository::class)]
|
||||
final class QuestionRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
public function testFindNextQuestionReturnsRightQuestion(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||
|
||||
$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();
|
||||
$this->assertInstanceOf(Quiz::class, $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 testFindNextQuestionGivesNullWhenAllQuestionsAnswered(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
$this->answerQuestion($question, $candidate);
|
||||
}
|
||||
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
|
||||
$this->assertNotInstanceOf(Question::class, $question);
|
||||
}
|
||||
|
||||
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||
|
||||
$krtekSeason->activeQuiz = null;
|
||||
$this->entityManager->flush();
|
||||
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
|
||||
$this->assertNotInstanceOf(Question::class, $question);
|
||||
}
|
||||
|
||||
private function answerQuestion(Question $question, Candidate $candidate): void
|
||||
{
|
||||
$answer = $question->answers->first();
|
||||
$this->assertInstanceOf(Answer::class, $answer);
|
||||
$this->entityManager->persist(new GivenAnswer(
|
||||
$candidate,
|
||||
$question->quiz,
|
||||
$answer,
|
||||
));
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
}
|
||||
72
tests/Repository/QuizCandidateRepositoryTest.php
Normal file
72
tests/Repository/QuizCandidateRepositoryTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\QuizCandidate;
|
||||
use Tvdt\Repository\QuizCandidateRepository;
|
||||
|
||||
#[CoversClass(QuizCandidateRepository::class)]
|
||||
final class QuizCandidateRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
public function testCreateIfNotExists(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||
$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->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||
$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->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$this->quizCandidateRepository->setCorrectionsForCandidate(
|
||||
$quiz, $candidate, 3.5,
|
||||
);
|
||||
}
|
||||
}
|
||||
214
tests/Repository/QuizRepositoryTest.php
Normal file
214
tests/Repository/QuizRepositoryTest.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Psr\Clock\ClockInterface;
|
||||
use Symfony\Component\Clock\MockClock;
|
||||
use Tvdt\Entity\Answer;
|
||||
use Tvdt\Entity\GivenAnswer;
|
||||
use Tvdt\Entity\Question;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\QuizCandidate;
|
||||
use Tvdt\Repository\GivenAnswerRepository;
|
||||
use Tvdt\Repository\QuizRepository;
|
||||
|
||||
#[CoversClass(QuizRepository::class)]
|
||||
final class QuizRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
public function testClearQuiz(): void
|
||||
{
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$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->getSeasonByCode('krtek');
|
||||
$quiz = $krtekSeason->quizzes->last();
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$this->quizRepository->deleteQuiz($quiz);
|
||||
|
||||
$this->entityManager->refresh($krtekSeason);
|
||||
|
||||
$this->assertCount(1, $krtekSeason->quizzes);
|
||||
}
|
||||
|
||||
public function testTimeForCandidate(): void
|
||||
{
|
||||
$clock = new MockClock('2025-11-01 16:00:00');
|
||||
self::getContainer()->set(ClockInterface::class, $clock);
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Iris');
|
||||
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
// Start Quiz
|
||||
$qc = new QuizCandidate($quiz, $candidate);
|
||||
$this->entityManager->persist($qc);
|
||||
$this->entityManager->flush();
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
|
||||
$answer = $question->answers->first();
|
||||
$this->assertInstanceOf(Answer::class, $answer);
|
||||
|
||||
$clock->sleep(10 + $i);
|
||||
$qa = new GivenAnswer($candidate, $quiz, $answer);
|
||||
$this->entityManager->persist($qa);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$result = $this->quizRepository->getScores($quiz);
|
||||
|
||||
$this->assertSame('Iris', $result[0]->name);
|
||||
$this->assertSame(5, $result[0]->correct);
|
||||
$this->assertEqualsWithDelta(5.0, $result[0]->score, \PHP_FLOAT_EPSILON);
|
||||
|
||||
$this->assertSame(4, $result[0]->time->i);
|
||||
$this->assertSame(15, $result[0]->time->s);
|
||||
}
|
||||
|
||||
public function testScoresAreCalculatedCorrectly(): void
|
||||
{
|
||||
$clock = new MockClock('2025-11-01 16:00:00');
|
||||
self::getContainer()->set(ClockInterface::class, $clock);
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate1 = $this->getCandidateBySeasonAndName($krtekSeason, 'Iris');
|
||||
$candidate2 = $this->getCandidateBySeasonAndName($krtekSeason, 'Philine');
|
||||
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$qc1 = new QuizCandidate($quiz, $candidate1);
|
||||
$qc2 = new QuizCandidate($quiz, $candidate2);
|
||||
$this->entityManager->persist($qc1);
|
||||
$this->entityManager->persist($qc2);
|
||||
$this->entityManager->flush();
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate1);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
|
||||
$answer1 = $question->answers->first();
|
||||
$answer2 = $question->answers[intdiv(\count($question->answers), 2)];
|
||||
$this->assertInstanceOf(Answer::class, $answer1);
|
||||
$this->assertInstanceOf(Answer::class, $answer2);
|
||||
|
||||
$clock->sleep(10);
|
||||
|
||||
$qa = new GivenAnswer($candidate1, $quiz, $answer1);
|
||||
$this->entityManager->persist($qa);
|
||||
$qa = new GivenAnswer($candidate2, $quiz, $answer2);
|
||||
$this->entityManager->persist($qa);
|
||||
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$scores = $this->quizRepository->getScores($quiz);
|
||||
$this->assertCount(2, $scores);
|
||||
$this->assertSame('Iris', $scores[0]->name);
|
||||
$this->assertSame('Philine', $scores[1]->name);
|
||||
$this->assertEqualsWithDelta(5.0, $scores[0]->score, \PHP_FLOAT_EPSILON);
|
||||
$this->assertEqualsWithDelta(4.0, $scores[1]->score, \PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
public function testCorrectionsCalculatedCorrectly(): void
|
||||
{
|
||||
$clock = new MockClock('2025-11-01 16:00:00');
|
||||
self::getContainer()->set(ClockInterface::class, $clock);
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Iris');
|
||||
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$qc = new QuizCandidate($quiz, $candidate);
|
||||
$this->entityManager->persist($qc);
|
||||
$this->entityManager->flush();
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
|
||||
$answer = $question->answers->first();
|
||||
$this->assertInstanceOf(Answer::class, $answer);
|
||||
|
||||
$clock->sleep(10);
|
||||
$qa = new GivenAnswer($candidate, $quiz, $answer);
|
||||
$this->entityManager->persist($qa);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$qc->corrections = 2;
|
||||
$this->entityManager->flush();
|
||||
|
||||
$result = $this->quizRepository->getScores($quiz);
|
||||
|
||||
$this->assertEqualsWithDelta(7.0, $result[0]->score, \PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
public function testCandidatesWithSameScoreAreSortedCorrectlyByTime(): void
|
||||
{
|
||||
$clock = new MockClock('2025-11-01 16:00:00');
|
||||
self::getContainer()->set(ClockInterface::class, $clock);
|
||||
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||
$candidate1 = $this->getCandidateBySeasonAndName($krtekSeason, 'Iris');
|
||||
$candidate2 = $this->getCandidateBySeasonAndName($krtekSeason, 'Philine');
|
||||
|
||||
$quiz = $krtekSeason->activeQuiz;
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$qc1 = new QuizCandidate($quiz, $candidate1);
|
||||
$this->entityManager->persist($qc1);
|
||||
$clock->sleep(10);
|
||||
$qc2 = new QuizCandidate($quiz, $candidate2);
|
||||
$this->entityManager->persist($qc2);
|
||||
$this->entityManager->flush();
|
||||
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate1);
|
||||
$this->assertInstanceOf(Question::class, $question);
|
||||
|
||||
$answer1 = $question->answers->first();
|
||||
$answer2 = $question->answers->last();
|
||||
$this->assertInstanceOf(Answer::class, $answer1);
|
||||
$this->assertInstanceOf(Answer::class, $answer2);
|
||||
|
||||
$clock->sleep(10);
|
||||
|
||||
$qa = new GivenAnswer($candidate1, $quiz, $answer1);
|
||||
$this->entityManager->persist($qa);
|
||||
|
||||
$qa = new GivenAnswer($candidate2, $quiz, $answer2);
|
||||
$this->entityManager->persist($qa);
|
||||
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$result = $this->quizRepository->getScores($quiz);
|
||||
|
||||
$this->assertEqualsWithDelta(5.0, $result[0]->score, \PHP_FLOAT_EPSILON);
|
||||
$this->assertEqualsWithDelta(5.0, $result[1]->score, \PHP_FLOAT_EPSILON);
|
||||
$this->assertSame('Philine', $result[0]->name);
|
||||
$this->assertSame('Iris', $result[1]->name);
|
||||
}
|
||||
}
|
||||
59
tests/Repository/SeasonRepositoryTest.php
Normal file
59
tests/Repository/SeasonRepositoryTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
45
tests/Repository/UserRepositoryTest.php
Normal file
45
tests/Repository/UserRepositoryTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Repository;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Tvdt\DataFixtures\TestFixtures;
|
||||
use Tvdt\Repository\UserRepository;
|
||||
|
||||
use function PHPUnit\Framework\assertEmpty;
|
||||
|
||||
#[CoversClass(UserRepository::class)]
|
||||
final class UserRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
public function testUpgradePassword(): void
|
||||
{
|
||||
$passwordHasher = self::getContainer()->get(UserPasswordHasherInterface::class);
|
||||
$user = $this->getUserByEmail('user1@example.org');
|
||||
|
||||
$newHash = $passwordHasher->hashPassword($user, TestFixtures::PASSWORD);
|
||||
|
||||
$this->assertNotSame($newHash, $user->password);
|
||||
$this->userRepository->upgradePassword($user, $newHash);
|
||||
|
||||
$this->entityManager->refresh($user);
|
||||
$this->assertSame($newHash, $user->password);
|
||||
}
|
||||
|
||||
public function testMakeAdmin(): void
|
||||
{
|
||||
$user = $this->getUserByEmail('test@example.org');
|
||||
assertEmpty($user->roles);
|
||||
$this->userRepository->makeAdmin('test@example.org');
|
||||
$this->entityManager->refresh($user);
|
||||
$this->assertSame(['ROLE_ADMIN'], $user->roles);
|
||||
}
|
||||
|
||||
public function testMakeAdminInvalidEmail(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->userRepository->makeAdmin('invalid@example.org');
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Security\Voter;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\MockObject\Stub;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Tvdt\Entity\Answer;
|
||||
use Tvdt\Entity\Candidate;
|
||||
use Tvdt\Entity\Elimination;
|
||||
@@ -18,6 +20,7 @@ use Tvdt\Entity\Season;
|
||||
use Tvdt\Entity\User;
|
||||
use Tvdt\Security\Voter\SeasonVoter;
|
||||
|
||||
#[CoversClass(SeasonVoter::class)]
|
||||
final class SeasonVoterTest extends TestCase
|
||||
{
|
||||
private SeasonVoter $seasonVoter;
|
||||
@@ -51,27 +54,58 @@ final class SeasonVoterTest extends TestCase
|
||||
{
|
||||
$season = self::createStub(Season::class);
|
||||
$season->method('isOwner')->willReturn(true);
|
||||
|
||||
$quiz = self::createStub(Quiz::class);
|
||||
$quiz->season = $season;
|
||||
|
||||
$elimination = self::createStub(Elimination::class);
|
||||
$elimination->quiz = $quiz;
|
||||
yield 'Season' => [$season];
|
||||
|
||||
$candidate = self::createStub(Candidate::class);
|
||||
$candidate->season = $season;
|
||||
yield 'Candidate' => [$candidate];
|
||||
|
||||
$quiz = self::createStub(Quiz::class);
|
||||
$quiz->season = $season;
|
||||
yield 'Quiz' => [$quiz];
|
||||
|
||||
$elimination = self::createStub(Elimination::class);
|
||||
$elimination->quiz = $quiz;
|
||||
yield 'Elimination' => [$elimination];
|
||||
|
||||
$question = self::createStub(Question::class);
|
||||
$question->quiz = $quiz;
|
||||
yield 'Question' => [$question];
|
||||
|
||||
$answer = self::createStub(Answer::class);
|
||||
$answer->question = $question;
|
||||
|
||||
yield 'Season' => [$season];
|
||||
yield 'Elimination' => [$elimination];
|
||||
yield 'Quiz' => [$quiz];
|
||||
yield 'Candidate' => [$candidate];
|
||||
yield 'Question' => [$question];
|
||||
yield 'Answer' => [$answer];
|
||||
}
|
||||
|
||||
public function testWrongUserTypeReturnFalse(): void
|
||||
{
|
||||
$user = self::createStub(UserInterface::class);
|
||||
$token = $this->createStub(TokenInterface::class);
|
||||
$token->method('getUser')->willReturn($user);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->seasonVoter->vote($token, new Season(), ['SEASON_EDIT']));
|
||||
}
|
||||
|
||||
public function testAdminCanDoAnything(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->roles = ['ROLE_ADMIN'];
|
||||
|
||||
$token = $this->createStub(TokenInterface::class);
|
||||
$token->method('getUser')->willReturn($user);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->seasonVoter->vote($token, new Season(), ['SEASON_EDIT']));
|
||||
}
|
||||
|
||||
public function testRandomClassWillAbstain(): void
|
||||
{
|
||||
$subject = new \stdClass();
|
||||
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->seasonVoter->vote($this->token, $subject, ['SEASON_EDIT']));
|
||||
}
|
||||
|
||||
public function testRandomSunjectWillAbstain(): void
|
||||
{
|
||||
$subject = new Season();
|
||||
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->seasonVoter->vote($this->token, $subject, ['DO_NOTHING']));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user