diff --git a/.idea/php.xml b/.idea/php.xml
index b2b17bf..9fac556 100644
--- a/.idea/php.xml
+++ b/.idea/php.xml
@@ -291,7 +291,7 @@
-
+
diff --git a/Justfile b/Justfile
index 91b613b..a22d394 100644
--- a/Justfile
+++ b/Justfile
@@ -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
diff --git a/phpunit.dist.xml b/phpunit.dist.xml
index 81218c2..5feec8b 100644
--- a/phpunit.dist.xml
+++ b/phpunit.dist.xml
@@ -27,6 +27,9 @@
src
+
+ src/DataFixtures
+
diff --git a/src/DataFixtures/KrtekFixtures.php b/src/DataFixtures/KrtekFixtures.php
index dab5226..29cfb9c 100644
--- a/src/DataFixtures/KrtekFixtures.php
+++ b/src/DataFixtures/KrtekFixtures.php
@@ -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
diff --git a/src/DataFixtures/TestFixtures.php b/src/DataFixtures/TestFixtures.php
index d233d4f..0c5be0a 100644
--- a/src/DataFixtures/TestFixtures.php
+++ b/src/DataFixtures/TestFixtures.php
@@ -6,12 +6,16 @@ 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 const string PASSWORD = 'test1234';
+
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher,
) {}
@@ -21,13 +25,51 @@ 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();
$user->email = 'test@example.org';
- $user->password = $this->passwordHasher->hashPassword($user, 'test1234');
+ $user->password = $this->passwordHasher->hashPassword($user, self::PASSWORD);
$manager->persist($user);
+
+ $user = new User();
+ $user->email = 'krtek-admin@example.org';
+ $user->password = $this->passwordHasher->hashPassword($user, self::PASSWORD);
+
+ $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, self::PASSWORD);
+
+ $manager->persist($user);
+ $user->addSeason($anotherSeason);
+
+ $user = new User();
+ $user->email = 'user2@example.org';
+ $user->password = $this->passwordHasher->hashPassword($user, self::PASSWORD);
+
+ $manager->persist($user);
+
+ $krtek->addOwner($user);
+ $anotherSeason->addOwner($user);
+
$manager->flush();
}
}
diff --git a/src/Entity/Season.php b/src/Entity/Season.php
index f7ac04a..a116000 100644
--- a/src/Entity/Season.php
+++ b/src/Entity/Season.php
@@ -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;
}
}
diff --git a/tests/Command/ClaimSeasonCommandTest.php b/tests/Command/ClaimSeasonCommandTest.php
index ebec6e7..4cd6634 100644
--- a/tests/Command/ClaimSeasonCommandTest.php
+++ b/tests/Command/ClaimSeasonCommandTest.php
@@ -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
diff --git a/tests/Repository/CandidateRepositoryTest.php b/tests/Repository/CandidateRepositoryTest.php
index 84b319a..b4c8530 100644
--- a/tests/Repository/CandidateRepositoryTest.php
+++ b/tests/Repository/CandidateRepositoryTest.php
@@ -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 */
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*',
diff --git a/tests/Repository/DatabaseTestCase.php b/tests/Repository/DatabaseTestCase.php
new file mode 100644
index 0000000..5687699
--- /dev/null
+++ b/tests/Repository/DatabaseTestCase.php
@@ -0,0 +1,70 @@
+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;
+ }
+}
diff --git a/tests/Repository/QuestionRepositoryTest.php b/tests/Repository/QuestionRepositoryTest.php
index 244740f..52f2b3e 100644
--- a/tests/Repository/QuestionRepositoryTest.php
+++ b/tests/Repository/QuestionRepositoryTest.php
@@ -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();
diff --git a/tests/Repository/QuizCandidateRepositoryTest.php b/tests/Repository/QuizCandidateRepositoryTest.php
index 780c3fe..8ee5aac 100644
--- a/tests/Repository/QuizCandidateRepositoryTest.php
+++ b/tests/Repository/QuizCandidateRepositoryTest.php
@@ -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);
diff --git a/tests/Repository/QuizRepositoryTest.php b/tests/Repository/QuizRepositoryTest.php
index a6462fa..af9dd97 100644
--- a/tests/Repository/QuizRepositoryTest.php
+++ b/tests/Repository/QuizRepositoryTest.php
@@ -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);
diff --git a/tests/Repository/SeasonRepositoryTest.php b/tests/Repository/SeasonRepositoryTest.php
new file mode 100644
index 0000000..589d1cc
--- /dev/null
+++ b/tests/Repository/SeasonRepositoryTest.php
@@ -0,0 +1,59 @@
+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);
+ }
+}
diff --git a/tests/Repository/UserRepositoryTest.php b/tests/Repository/UserRepositoryTest.php
new file mode 100644
index 0000000..8770e5b
--- /dev/null
+++ b/tests/Repository/UserRepositoryTest.php
@@ -0,0 +1,45 @@
+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');
+ }
+}