Create Testcoverage and upgrade Symfomy and PHP
Some checks failed
CI / Tests (push) Failing after 1m8s
CI / Build and deploy to ${{ startsWith(github.ref, 'refs/tags/') && 'production' || (github.ref == 'refs/heads/main' && 'acceptance' || '') }} (push) Has been skipped

* 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:
2025-11-28 22:56:09 +01:00
committed by GitHub
parent fc273638ad
commit bcd6a157a8
56 changed files with 4324 additions and 1424 deletions

View 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);
}
}

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

@@ -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();
}
}

View 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,
);
}
}

View 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);
}
}

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);
}
}

View 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');
}
}