mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-03-06 04:44:19 +01:00
Tests 3
This commit is contained in:
2
.idea/php.xml
generated
2
.idea/php.xml
generated
@@ -291,7 +291,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="PhpUnit">
|
<component name="PhpUnit">
|
||||||
<phpunit_settings>
|
<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>
|
</phpunit_settings>
|
||||||
</component>
|
</component>
|
||||||
<component name="Psalm">
|
<component name="Psalm">
|
||||||
|
|||||||
2
Justfile
2
Justfile
@@ -43,5 +43,5 @@ clean:
|
|||||||
reload-tests:
|
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:drop --if-exists --force
|
||||||
@docker compose exec php bin/console --env=test doctrine:database:create
|
@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
|
@docker compose exec php bin/console --env=test doctrine:fixtures:load --no-interaction --group=test
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
<include>
|
<include>
|
||||||
<directory>src</directory>
|
<directory>src</directory>
|
||||||
</include>
|
</include>
|
||||||
|
<exclude>
|
||||||
|
<directory>src/DataFixtures</directory>
|
||||||
|
</exclude>
|
||||||
</source>
|
</source>
|
||||||
|
|
||||||
<extensions>
|
<extensions>
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ use Tvdt\Entity\Season;
|
|||||||
|
|
||||||
final class KrtekFixtures extends Fixture implements FixtureGroupInterface
|
final class KrtekFixtures extends Fixture implements FixtureGroupInterface
|
||||||
{
|
{
|
||||||
|
public const string KRTEK_SEASON = 'krtek-seaspm';
|
||||||
|
|
||||||
public static function getGroups(): array
|
public static function getGroups(): array
|
||||||
{
|
{
|
||||||
return ['test', 'dev'];
|
return ['test', 'dev'];
|
||||||
@@ -47,6 +49,8 @@ final class KrtekFixtures extends Fixture implements FixtureGroupInterface
|
|||||||
$season->addQuiz($this->createQuiz2($season));
|
$season->addQuiz($this->createQuiz2($season));
|
||||||
|
|
||||||
$manager->flush();
|
$manager->flush();
|
||||||
|
|
||||||
|
$this->addReference(self::KRTEK_SEASON, $season);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createQuiz1(Season $season): Quiz
|
private function createQuiz1(Season $season): Quiz
|
||||||
|
|||||||
@@ -6,12 +6,16 @@ namespace Tvdt\DataFixtures;
|
|||||||
|
|
||||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||||
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
|
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
|
||||||
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||||
use Doctrine\Persistence\ObjectManager;
|
use Doctrine\Persistence\ObjectManager;
|
||||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||||
|
use Tvdt\Entity\Season;
|
||||||
use Tvdt\Entity\User;
|
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(
|
public function __construct(
|
||||||
private readonly UserPasswordHasherInterface $passwordHasher,
|
private readonly UserPasswordHasherInterface $passwordHasher,
|
||||||
) {}
|
) {}
|
||||||
@@ -21,13 +25,51 @@ final class TestFixtures extends Fixture implements FixtureGroupInterface
|
|||||||
return ['test'];
|
return ['test'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDependencies(): array
|
||||||
|
{
|
||||||
|
return [KrtekFixtures::class];
|
||||||
|
}
|
||||||
|
|
||||||
public function load(ObjectManager $manager): void
|
public function load(ObjectManager $manager): void
|
||||||
{
|
{
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$user->email = 'test@example.org';
|
$user->email = 'test@example.org';
|
||||||
$user->password = $this->passwordHasher->hashPassword($user, 'test1234');
|
$user->password = $this->passwordHasher->hashPassword($user, self::PASSWORD);
|
||||||
|
|
||||||
$manager->persist($user);
|
$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();
|
$manager->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ class Season
|
|||||||
return $this->owners->contains($user);
|
return $this->owners->contains($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateSeasonCode(): self
|
public function generateSeasonCode(): void
|
||||||
{
|
{
|
||||||
$code = '';
|
$code = '';
|
||||||
$len = mb_strlen(self::SEASON_CODE_CHARACTERS) - 1;
|
$len = mb_strlen(self::SEASON_CODE_CHARACTERS) - 1;
|
||||||
@@ -108,7 +108,5 @@ class Season
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->seasonCode = $code;
|
$this->seasonCode = $code;
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,10 +41,11 @@ final class ClaimSeasonCommandTest extends KernelTestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$season = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$season = $this->seasonRepository->findOneBySeasonCode('krtek');
|
||||||
|
|
||||||
$this->assertInstanceOf(Season::class, $season);
|
$this->assertInstanceOf(Season::class, $season);
|
||||||
|
|
||||||
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
|
$this->assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
|
||||||
$this->assertCount(1, $season->owners);
|
$this->assertCount(3, $season->owners);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvalidEmailFails(): void
|
public function testInvalidEmailFails(): void
|
||||||
|
|||||||
@@ -6,25 +6,12 @@ namespace Tvdt\Tests\Repository;
|
|||||||
|
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
||||||
use Tvdt\Entity\Candidate;
|
use Tvdt\Entity\Candidate;
|
||||||
use Tvdt\Entity\Season;
|
|
||||||
use Tvdt\Repository\CandidateRepository;
|
use Tvdt\Repository\CandidateRepository;
|
||||||
use Tvdt\Repository\SeasonRepository;
|
|
||||||
|
|
||||||
#[CoversClass(CandidateRepository::class)]
|
#[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}> */
|
/** @return iterable<string, array{0: string}> */
|
||||||
public static function candidateHashDataProvider(): iterable
|
public static function candidateHashDataProvider(): iterable
|
||||||
{
|
{
|
||||||
@@ -36,8 +23,7 @@ final class CandidateRepositoryTest extends KernelTestCase
|
|||||||
#[DataProvider('candidateHashDataProvider')]
|
#[DataProvider('candidateHashDataProvider')]
|
||||||
public function testGetCandidateByHash(string $hash): void
|
public function testGetCandidateByHash(string $hash): void
|
||||||
{
|
{
|
||||||
/** @var Season $krtekSeason */
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
|
||||||
$candidate = $this->candidateRepository->getCandidateByHash(
|
$candidate = $this->candidateRepository->getCandidateByHash(
|
||||||
$krtekSeason,
|
$krtekSeason,
|
||||||
$hash,
|
$hash,
|
||||||
@@ -50,8 +36,7 @@ final class CandidateRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testGetCandidateByHashUnknownHashReturnsNull(): void
|
public function testGetCandidateByHashUnknownHashReturnsNull(): void
|
||||||
{
|
{
|
||||||
/** @var Season $krtekSeason */
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
|
||||||
$result = $this->candidateRepository->getCandidateByHash(
|
$result = $this->candidateRepository->getCandidateByHash(
|
||||||
$krtekSeason,
|
$krtekSeason,
|
||||||
'TWFyaWpu',
|
'TWFyaWpu',
|
||||||
@@ -61,8 +46,7 @@ final class CandidateRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
|
public function testGetCandidateByHashInvalidBase64HashReturnsNull(): void
|
||||||
{
|
{
|
||||||
/** @var Season $krtekSeason */
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
|
||||||
$result = $this->candidateRepository->getCandidateByHash(
|
$result = $this->candidateRepository->getCandidateByHash(
|
||||||
$krtekSeason,
|
$krtekSeason,
|
||||||
'TWFyaWpu*',
|
'TWFyaWpu*',
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,46 +4,21 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tvdt\Tests\Repository;
|
namespace Tvdt\Tests\Repository;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
||||||
use Tvdt\Entity\Answer;
|
use Tvdt\Entity\Answer;
|
||||||
use Tvdt\Entity\Candidate;
|
use Tvdt\Entity\Candidate;
|
||||||
use Tvdt\Entity\GivenAnswer;
|
use Tvdt\Entity\GivenAnswer;
|
||||||
use Tvdt\Entity\Question;
|
use Tvdt\Entity\Question;
|
||||||
use Tvdt\Entity\Quiz;
|
use Tvdt\Entity\Quiz;
|
||||||
use Tvdt\Entity\Season;
|
|
||||||
use Tvdt\Repository\CandidateRepository;
|
|
||||||
use Tvdt\Repository\QuestionRepository;
|
use Tvdt\Repository\QuestionRepository;
|
||||||
use Tvdt\Repository\SeasonRepository;
|
|
||||||
|
|
||||||
#[CoversClass(QuestionRepository::class)]
|
#[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
|
public function testFindNextQuestionReturnsRightQuestion(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
|
|
||||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||||
$this->assertInstanceOf(Question::class, $question);
|
$this->assertInstanceOf(Question::class, $question);
|
||||||
@@ -71,10 +46,8 @@ final class QuestionRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswered(): void
|
public function testFindNextQuestionGivesNullWhenAllQuestionsAnswered(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
|
|
||||||
for ($i = 0; $i < 15; ++$i) {
|
for ($i = 0; $i < 15; ++$i) {
|
||||||
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
$question = $this->questionRepository->findNextQuestionForCandidate($candidate);
|
||||||
@@ -89,10 +62,8 @@ final class QuestionRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
|
public function testFindNextQuestionWithNoActiveQuizReturnsNull(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Tom');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Tom']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
|
|
||||||
$krtekSeason->activeQuiz = null;
|
$krtekSeason->activeQuiz = null;
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|||||||
@@ -5,39 +5,17 @@ declare(strict_types=1);
|
|||||||
namespace Tvdt\Tests\Repository;
|
namespace Tvdt\Tests\Repository;
|
||||||
|
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
||||||
use Tvdt\Entity\Candidate;
|
|
||||||
use Tvdt\Entity\Quiz;
|
use Tvdt\Entity\Quiz;
|
||||||
use Tvdt\Entity\QuizCandidate;
|
use Tvdt\Entity\QuizCandidate;
|
||||||
use Tvdt\Entity\Season;
|
|
||||||
use Tvdt\Repository\CandidateRepository;
|
|
||||||
use Tvdt\Repository\QuizCandidateRepository;
|
use Tvdt\Repository\QuizCandidateRepository;
|
||||||
use Tvdt\Repository\SeasonRepository;
|
|
||||||
|
|
||||||
#[CoversClass(QuizCandidateRepository::class)]
|
#[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
|
public function testCreateIfNotExists(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
$quiz = $krtekSeason->activeQuiz;
|
$quiz = $krtekSeason->activeQuiz;
|
||||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||||
|
|
||||||
@@ -57,10 +35,8 @@ final class QuizCandidateRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testSetCorrectionsForCandidateUpdatesCandidateCorrectly(): void
|
public function testSetCorrectionsForCandidateUpdatesCandidateCorrectly(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
$quiz = $krtekSeason->activeQuiz;
|
$quiz = $krtekSeason->activeQuiz;
|
||||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||||
|
|
||||||
@@ -82,10 +58,8 @@ final class QuizCandidateRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testCannotGiveCorrectionsToCandidateWithoutResult(): void
|
public function testCannotGiveCorrectionsToCandidateWithoutResult(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBySeasonCode('krtek');
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
$candidate = $this->getCandidateBySeasonAndName($krtekSeason, 'Myrthe');
|
||||||
$candidate = $this->candidateRepository->findOneBy(['season' => $krtekSeason, 'name' => 'Myrthe']);
|
|
||||||
$this->assertInstanceOf(Candidate::class, $candidate);
|
|
||||||
$quiz = $krtekSeason->activeQuiz;
|
$quiz = $krtekSeason->activeQuiz;
|
||||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||||
|
|
||||||
|
|||||||
@@ -4,37 +4,17 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tvdt\Tests\Repository;
|
namespace Tvdt\Tests\Repository;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
||||||
use Tvdt\Entity\Quiz;
|
use Tvdt\Entity\Quiz;
|
||||||
use Tvdt\Entity\Season;
|
|
||||||
use Tvdt\Repository\GivenAnswerRepository;
|
use Tvdt\Repository\GivenAnswerRepository;
|
||||||
use Tvdt\Repository\QuizRepository;
|
use Tvdt\Repository\QuizRepository;
|
||||||
use Tvdt\Repository\SeasonRepository;
|
|
||||||
|
|
||||||
#[CoversClass(QuizRepository::class)]
|
#[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
|
public function testClearQuiz(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBy(['seasonCode' => 'krtek']);
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
|
||||||
|
|
||||||
$quiz = $krtekSeason->activeQuiz;
|
$quiz = $krtekSeason->activeQuiz;
|
||||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||||
|
|
||||||
@@ -52,8 +32,7 @@ final class QuizRepositoryTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testDeleteQuiz(): void
|
public function testDeleteQuiz(): void
|
||||||
{
|
{
|
||||||
$krtekSeason = $this->seasonRepository->findOneBy(['seasonCode' => 'krtek']);
|
$krtekSeason = $this->getSeasonByCode('krtek');
|
||||||
$this->assertInstanceOf(Season::class, $krtekSeason);
|
|
||||||
$quiz = $krtekSeason->quizzes->last();
|
$quiz = $krtekSeason->quizzes->last();
|
||||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||||
|
|
||||||
|
|||||||
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user