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

2
.idea/php.xml generated
View File

@@ -291,7 +291,7 @@
</component>
<component name="PhpUnit">
<phpunit_settings>
<phpunit_by_interpreter interpreter_id="96512cb2-7b9e-4e1d-bfa2-bf7f3be424c8" bootstrap_file_path="./tests/bootstrap.php" configuration_file_path="./phpunit.dist.xml" custom_loader_path="/app/vendor/autoload.php" phpunit_phar_path="" use_bootstrap_file="true" use_configuration_file="true" />
<phpunit_by_interpreter interpreter_id="96512cb2-7b9e-4e1d-bfa2-bf7f3be424c8" bootstrap_file_path="./tests/bootstrap.php" configuration_file_path="./phpunit.dist.xml" custom_loader_path="/app/vendor/autoload.php" paratest_path="$PROJECT_DIR$/vendor/bin/paratest_for_phpstorm" phpunit_phar_path="" use_bootstrap_file="true" use_configuration_file="true" />
</phpunit_settings>
</component>
<component name="Psalm">

View File

@@ -43,5 +43,5 @@ clean:
reload-tests:
@docker compose exec php bin/console --env=test doctrine:database:drop --if-exists --force
@docker compose exec php bin/console --env=test doctrine:database:create
@docker compose exec php bin/console --env=test doctrine:schema:create
@docker compose exec php bin/console --env=test doctrine:schema:create --quiet
@docker compose exec php bin/console --env=test doctrine:fixtures:load --no-interaction --group=test

View File

@@ -27,6 +27,9 @@
<include>
<directory>src</directory>
</include>
<exclude>
<directory>src/DataFixtures</directory>
</exclude>
</source>
<extensions>

View File

@@ -15,6 +15,8 @@ use Tvdt\Entity\Season;
final class KrtekFixtures extends Fixture implements FixtureGroupInterface
{
public const string KRTEK_SEASON = 'krtek-seaspm';
public static function getGroups(): array
{
return ['test', 'dev'];
@@ -47,6 +49,8 @@ final class KrtekFixtures extends Fixture implements FixtureGroupInterface
$season->addQuiz($this->createQuiz2($season));
$manager->flush();
$this->addReference(self::KRTEK_SEASON, $season);
}
private function createQuiz1(Season $season): Quiz

View File

@@ -6,11 +6,13 @@ namespace Tvdt\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Tvdt\Entity\Season;
use Tvdt\Entity\User;
final class TestFixtures extends Fixture implements FixtureGroupInterface
final class TestFixtures extends Fixture implements FixtureGroupInterface, DependentFixtureInterface
{
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher,
@@ -21,6 +23,11 @@ final class TestFixtures extends Fixture implements FixtureGroupInterface
return ['test'];
}
public function getDependencies(): array
{
return [KrtekFixtures::class];
}
public function load(ObjectManager $manager): void
{
$user = new User();
@@ -28,6 +35,39 @@ final class TestFixtures extends Fixture implements FixtureGroupInterface
$user->password = $this->passwordHasher->hashPassword($user, 'test1234');
$manager->persist($user);
$user = new User();
$user->email = 'krtek-admin@example.org';
$user->password = $this->passwordHasher->hashPassword($user, 'test1234');
$manager->persist($user);
$krtek = $this->getReference(KrtekFixtures::KRTEK_SEASON, Season::class);
$krtek->addOwner($user);
$anotherSeason = new Season();
$anotherSeason->name = 'Another Season';
$anotherSeason->seasonCode = 'bbbbb';
$manager->persist($anotherSeason);
$this->addReference('another-season', $anotherSeason);
$user = new User();
$user->email = 'user1@example.org';
$user->password = $this->passwordHasher->hashPassword($user, 'test1234');
$manager->persist($user);
$user->addSeason($anotherSeason);
$user = new User();
$user->email = 'user2@example.org';
$user->password = $this->passwordHasher->hashPassword($user, 'test1234');
$manager->persist($user);
$krtek->addOwner($user);
$anotherSeason->addOwner($user);
$manager->flush();
}
}

View File

@@ -98,7 +98,7 @@ class Season
return $this->owners->contains($user);
}
public function generateSeasonCode(): self
public function generateSeasonCode(): void
{
$code = '';
$len = mb_strlen(self::SEASON_CODE_CHARACTERS) - 1;
@@ -108,7 +108,5 @@ class Season
}
$this->seasonCode = $code;
return $this;
}
}

View File

@@ -41,10 +41,11 @@ final class ClaimSeasonCommandTest extends KernelTestCase
]);
$season = $this->seasonRepository->findOneBySeasonCode('krtek');
$this->assertInstanceOf(Season::class, $season);
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
$this->assertCount(1, $season->owners);
$this->assertCount(3, $season->owners);
}
public function testInvalidEmailFails(): void

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