Skip to content

Commit

Permalink
Merge pull request #3 from yoanbernabeu/2-add-tests
Browse files Browse the repository at this point in the history
Add some unit tests
  • Loading branch information
yoanbernabeu authored Dec 21, 2024
2 parents e26a93f + 3d02a93 commit eb31035
Show file tree
Hide file tree
Showing 15 changed files with 843 additions and 2 deletions.
29 changes: 28 additions & 1 deletion .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Quality Assurance

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

Expand All @@ -12,6 +11,15 @@ jobs:
steps:
- uses: actions/checkout@master

- name: Create Database Directory
run: |
mkdir -p var
touch var/test.db
chmod -R 777 var
- name: Copy .env.test
run: cp .env.test .env.test.local

- name: PHP CS Fixer
uses: docker://jakzal/phpqa:php8.3
with:
Expand Down Expand Up @@ -42,7 +50,26 @@ jobs:
with:
args: bin/console lint:container

- name: Run Migrations
uses: docker://jakzal/phpqa:php8.3
env:
DATABASE_URL: "sqlite:///%kernel.project_dir%/var/test.db"
APP_ENV: test
with:
args: bin/console doctrine:migrations:migrate --no-interaction --env=test

- name: Load Fixtures
uses: docker://jakzal/phpqa:php8.3
env:
DATABASE_URL: "sqlite:///%kernel.project_dir%/var/test.db"
APP_ENV: test
with:
args: bin/console doctrine:fixtures:load --no-interaction --env=test

- name: Run Tests
uses: docker://jakzal/phpqa:php8.3
env:
DATABASE_URL: "sqlite:///%kernel.project_dir%/var/test.db"
APP_ENV: test
with:
args: bin/phpunit --testdox
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
}
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^4.0",
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "7.2.*",
"symfony/css-selector": "7.2.*",
Expand Down
171 changes: 170 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfonycasts\TailwindBundle\SymfonycastsTailwindBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
];
5 changes: 5 additions & 0 deletions config/packages/test/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

doctrine:
dbal:
driver: 'pdo_sqlite'
path: '%kernel.project_dir%/var/test.db'
104 changes: 104 additions & 0 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace App\DataFixtures;

use App\Entity\Poll;
use App\Entity\User;
use App\Entity\Vote;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class AppFixtures extends Fixture
{
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher,
) {
}

public function load(ObjectManager $manager): void
{
$this->loadUsers($manager);
$this->loadPolls($manager);
$manager->flush();
}

private function loadUsers(ObjectManager $manager): void
{
$admin = $this->createUser('admin', 'adminpass', ['ROLE_ADMIN']);
$manager->persist($admin);

$user = $this->createUser('user', 'userpass', ['ROLE_USER']);
$manager->persist($user);
}

/**
* @param list<string> $roles
*/
private function createUser(string $username, string $password, array $roles): User
{
$user = new User();
$user->setUsername($username);
$user->setPassword($this->passwordHasher->hashPassword($user, $password));
$user->setRoles($roles);

return $user;
}

private function loadPolls(ObjectManager $manager): void
{
$activePoll = $this->createActivePoll();
$manager->persist($activePoll);

$expiredPoll = $this->createExpiredPoll();
$manager->persist($expiredPoll);

// Créer plus de votes pour le sondage actif
$this->createVotesForPoll($manager, $activePoll, 10);
// Quelques votes pour le sondage expiré
$this->createVotesForPoll($manager, $expiredPoll, 5);
}

private function createActivePoll(): Poll
{
$poll = new Poll();
$poll
->setTitle('Active Poll')
->setDescription('This is an active poll')
->setShortCode('abc123')
->setStartAt(new \DateTimeImmutable('now'))
->setEndAt(new \DateTimeImmutable('+1 hour'))
->setQuestion1('Choice 1')
->setQuestion2('Choice 2')
->setQuestion3('Choice 3');

return $poll;
}

private function createExpiredPoll(): Poll
{
$poll = new Poll();
$poll
->setTitle('Expired Poll')
->setDescription('This is an expired poll')
->setShortCode('xyz789')
->setStartAt(new \DateTimeImmutable('-2 hours'))
->setEndAt(new \DateTimeImmutable('-1 hour'))
->setQuestion1('Choice 1')
->setQuestion2('Choice 2');

return $poll;
}

private function createVotesForPoll(ObjectManager $manager, Poll $poll, int $numberOfVotes): void
{
for ($i = 1; $i <= $numberOfVotes; ++$i) {
$vote = new Vote();
$vote->setPoll($poll);
$vote->setChoice(($i % 3) + 1); // Répartir les votes entre les choix 1, 2 et 3
$vote->setVoterId('visitor_'.$i);
$vote->setCreatedAt(new \DateTimeImmutable());
$manager->persist($vote);
}
}
}
12 changes: 12 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
"src/Repository/.gitignore"
]
},
"doctrine/doctrine-fixtures-bundle": {
"version": "4.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "3.0",
"ref": "1f5514cfa15b947298df4d771e694e578d4c204d"
},
"files": [
"src/DataFixtures/AppFixtures.php"
]
},
"doctrine/doctrine-migrations-bundle": {
"version": "3.3",
"recipe": {
Expand Down
Loading

0 comments on commit eb31035

Please sign in to comment.