Merge branch 'refs/heads/tests'

# Conflicts:
#	src/Controller/PostController.php
#	src/Controller/ProfilController.php
pull/27/head
Dorian HODIN 10 months ago
commit a6bc54b187

@ -0,0 +1 @@
{"version":1,"defects":{"ProfilTest::test_add_and_remove_follower":3,"ProfilTest::test_add_and_remove_following":3},"times":{"ProfilTest::test_it_can_be_instantiated":0.003,"ProfilTest::test_name":0,"ProfilTest::test_description":0,"ProfilTest::test_password":0,"ProfilTest::test_roles":0,"ProfilTest::test_user_identifier":0,"ProfilTest::test_add_and_remove_post":0.003,"ProfilTest::test_add_and_remove_commentary":0.001,"ProfilTest::test_add_and_remove_follower":0.001,"ProfilTest::test_add_and_remove_following":0.003}}

@ -98,7 +98,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^11.2",
"symfony/browser-kit": "7.0.7",
"symfony/css-selector": "7.0.7",
"symfony/debug-bundle": "7.0.7",

662
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -1,38 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
<extensions>
</extensions>
</phpunit>

@ -7,6 +7,7 @@ use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
@ -22,6 +23,11 @@ class RegistrationController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$plainPassword = $form->get('plainPassword')->getData();
if (strlen($plainPassword) < 6) {
$form->get('plainPassword')->addError(new FormError('Your password should be at least 6 characters'));
}
$user->setName($form->get('name')->getData());
$hashedPassword = $userPasswordHasher->hashPassword(

@ -211,4 +211,12 @@ class Post
return $this;
}
/**
* @param int|null $id
*/
public function setId(?int $id): void
{
$this->id = $id;
}
}

@ -208,7 +208,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface
public function addFollower(self $follower): static
{
if (!$this->followers->contains($follower) && $follower!=$this) {
if (!$this->followers->contains($follower) && $follower !== $this) {
$this->followers->add($follower);
}
@ -232,7 +232,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface
public function addFollowing(self $following): static
{
if (!$this->following->contains($following) && $following!=$this) {
if (!$this->following->contains($following) && $following !== $this) {
$this->following->add($following);
$following->addFollower($this);
}

@ -20,28 +20,12 @@ class RegistrationFormType extends AbstractType
->add('name')
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
'invalid_message' => 'You should agree to our terms.',
])
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
'invalid_message' => 'Please enter a password',
])
;
}

@ -0,0 +1,84 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Profil;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\Post;
class PostControllerTest extends WebTestCase
{
private KernelBrowser $client;
private mixed $em;
protected function setUp(): void
{
$this->client = static::createClient();
$this->em = static::getContainer()->get(EntityManagerInterface::class);
}
public function testGetPost()
{
$post = new Post();
$post->setTitle('Test Post');
$post->setText('This is a test post.');
$post->setId(66666);
$post->setProfil($this->createUser());
$post->setDream(true);
$this->em->persist($post);
$this->em->flush();
$crawler = $this->client->request('GET', '/post/' . $post->getId());
$this->assertResponseIsSuccessful();
}
public function testAddPost()
{
$this->client->loginUser($this->createUser());
$crawler = $this->client->request('GET', '/post/new/');
$this->assertResponseIsSuccessful();
$form = $crawler->selectButton('Submit')->form([
'post[title]' => 'New Post',
'post[text]' => 'Content of the new post',
]);
$this->client->submit($form);
$post = $this->em->getRepository(Post::class)->findOneBy(['title' => 'New Post']);
$this->assertNotNull($post);
}
public function testRemovePost()
{
$post = new Post();
$post->setTitle('Post to be deleted');
$post->setText('This post will be deleted.');
$post->setProfil($this->createUser());
$post->setDream(true);
$this->em->persist($post);
$this->em->flush();
$postId = $post->getId();
$this->client->request('DELETE', '/post/' . $postId);
$this->assertNull($this->em->getRepository(Post::class)->find($postId));
}
private function createUser(): Profil
{
$user = new Profil();
$user->setName('testuser');
$user->setPassword(password_hash('password', PASSWORD_BCRYPT));
$user->setId(666666);
$this->em->persist($user);
$this->em->flush();
return $user;
}
}

@ -0,0 +1,44 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Commentary;
use PHPUnit\Framework\TestCase;
use App\Entity\Post;
use App\Entity\Profil;
class CommentaryTest extends TestCase
{
public function testGetId()
{
$commentary = new Commentary();
$this->assertNull($commentary->getId());
}
public function testGetSetText()
{
$commentary = new Commentary();
$text = 'This is a test commentary.';
$commentary->setText($text);
$this->assertSame($text, $commentary->getText());
}
public function testGetSetPost()
{
$commentary = new Commentary();
$post = new Post();
$commentary->setPost($post);
$this->assertSame($post, $commentary->getPost());
}
public function testGetSetProfil()
{
$commentary = new Commentary();
$profil = new Profil();
$commentary->setProfil($profil);
$this->assertSame($profil, $commentary->getProfil());
}
}

@ -0,0 +1,103 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Post;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use App\Entity\Profil;
use App\Entity\Commentary;
use App\Entity\Tags;
class PostTest extends TestCase
{
public function testGetId()
{
$post = new Post();
$this->assertNull($post->getId());
}
public function testGetSetTitle()
{
$post = new Post();
$title = 'Test Title';
$post->setTitle($title);
$this->assertSame($title, $post->getTitle());
}
public function testGetSetText()
{
$post = new Post();
$text = 'This is a test post.';
$post->setText($text);
$this->assertSame($text, $post->getText());
}
public function testGetSetIsDream()
{
$post = new Post();
$post->setDream(true);
$this->assertTrue($post->isDream());
}
public function testGetSetUpVote()
{
$post = new Post();
$post->setUpVote(10);
$this->assertSame(10, $post->getUpVote());
}
public function testGetSetDownVote()
{
$post = new Post();
$post->setDownVote(5);
$this->assertSame(5, $post->getDownVote());
}
public function testGetSetProfil()
{
$post = new Post();
$profil = new Profil();
$post->setProfil($profil);
$this->assertSame($profil, $post->getProfil());
}
public function testAddRemoveCommentary()
{
$post = new Post();
$commentary = new Commentary();
$post->addCommentary($commentary);
$this->assertTrue($post->getCommentaries()->contains($commentary));
$post->removeCommentary($commentary);
$this->assertFalse($post->getCommentaries()->contains($commentary));
}
public function testAddRemoveTag()
{
$post = new Post();
$tag = new Tags();
$post->addTag($tag);
$this->assertTrue($post->getTags()->contains($tag));
$post->removeTag($tag);
$this->assertFalse($post->getTags()->contains($tag));
}
public function testGetSetCreatedAt()
{
$post = new Post();
$createdAt = new DateTimeImmutable('now');
$post->setCreatedAt($createdAt);
$this->assertSame($createdAt, $post->getCreatedAt());
}
}

@ -0,0 +1,101 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Commentary;
use App\Entity\Post;
use App\Entity\Profil;
use PHPUnit\Framework\TestCase;
class ProfilTest extends TestCase
{
public function test_it_can_be_instantiated(): void
{
$profil = new Profil();
$this->assertInstanceOf(Profil::class, $profil);
}
public function test_name()
{
$profil = new Profil();
$profil->setName('John Doe');
$this->assertEquals('John Doe', $profil->getName());
}
public function test_description()
{
$profil = new Profil();
$profil->setDescription('Lorem ipsum');
$this->assertEquals('Lorem ipsum', $profil->getDescription());
}
public function test_password()
{
$profil = new Profil();
$profil->setPassword('password123');
$this->assertEquals('password123', $profil->getPassword());
}
public function test_roles()
{
$profil = new Profil();
$roles = ['ROLE_USER', 'ROLE_ADMIN'];
$profil->setRoles($roles);
$this->assertEquals($roles, $profil->getRoles());
}
public function test_user_identifier()
{
$profil = new Profil();
$profil->setName('johndoe');
$this->assertEquals('johndoe', $profil->getUserIdentifier());
}
public function test_add_and_remove_post()
{
$profil = new Profil();
$post = new Post();
$profil->addPost($post);
$this->assertTrue($profil->getPosts()->contains($post));
$profil->removePost($post);
$this->assertFalse($profil->getPosts()->contains($post));
}
public function test_add_and_remove_commentary()
{
$profil = new Profil();
$commentary = new Commentary();
$profil->addCommentary($commentary);
$this->assertTrue($profil->getCommentaries()->contains($commentary));
$profil->removeCommentary($commentary);
$this->assertFalse($profil->getCommentaries()->contains($commentary));
}
public function test_add_and_remove_follower()
{
$profil1 = new Profil();
$profil2 = new Profil();
$profil1->addFollower($profil2);
$this->assertTrue($profil1->getFollowers()->contains($profil2));
$profil1->removeFollower($profil2);
$this->assertFalse($profil1->getFollowers()->contains($profil2));
}
public function test_add_and_remove_following()
{
$profil1 = new Profil();
$profil2 = new Profil();
$profil1->addFollowing($profil2);
$this->assertTrue($profil1->getFollowing()->contains($profil2));
$this->assertTrue($profil2->getFollowers()->contains($profil1));
$profil1->removeFollowing($profil2);
$this->assertFalse($profil1->getFollowing()->contains($profil2));
$this->assertFalse($profil2->getFollowers()->contains($profil1));
}
}

@ -0,0 +1,46 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Tags;
use PHPUnit\Framework\TestCase;
use App\Entity\Post;
class TagsTest extends TestCase
{
public function testGetId()
{
$tags = new Tags();
$this->assertNull($tags->getId());
}
public function testGetSetName()
{
$tags = new Tags();
$name = 'Test Tag';
$tags->setName($name);
$this->assertSame($name, $tags->getName());
}
public function testGetSetColor()
{
$tags = new Tags();
$color = '#FF0000';
$tags->setColor($color);
$this->assertSame($color, $tags->getColor());
}
public function testAddRemovePost()
{
$tags = new Tags();
$post = new Post();
$tags->addPost($post);
$this->assertTrue($tags->getPosts()->contains($post));
$tags->removePost($post);
$this->assertFalse($tags->getPosts()->contains($post));
}
}

@ -0,0 +1,40 @@
<?php
namespace App\Tests\Form;
use App\Form\ProfilType;
use App\Entity\Profil;
use Symfony\Component\Form\Test\TypeTestCase;
class ProfilTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$formData = [
'name' => 'Test Name',
'description' => 'Lorem ipsum dolor sit amet.',
];
$objectToCompare = new Profil();
$form = $this->factory->create(ProfilType::class, $objectToCompare);
$object = new Profil();
$object->setName('Test Name');
$object->setDescription('Lorem ipsum dolor sit amet.');
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($object, $objectToCompare);
}
public function testFormFields()
{
$form = $this->factory->create(ProfilType::class);
$this->assertTrue($form->has('name'));
$this->assertTrue($form->has('description'));
}
}

@ -0,0 +1,46 @@
<?php
namespace App\Tests\Form;
use App\Form\RegistrationFormType;
use App\Entity\Profil;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\Validation;
class RegistrationFormTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$formData = [
'name' => 'Test Name',
'agreeTerms' => true,
'plainPassword' => 'testpassword',
];
$form = $this->factory->create(RegistrationFormType::class);
$objectToCompare = new Profil();
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($formData['name'], $form->get('name')->getData());
$this->assertEquals($formData['agreeTerms'], $form->get('agreeTerms')->getData());
$this->assertEquals($formData['plainPassword'], $form->get('plainPassword')->getData());
$validator = Validation::createValidator();
$violations = $validator->validate($objectToCompare);
$this->assertCount(0, $violations);
}
public function testFormFields()
{
$form = $this->factory->create(RegistrationFormType::class);
$this->assertTrue($form->has('name'));
$this->assertTrue($form->has('agreeTerms'));
$this->assertTrue($form->has('plainPassword'));
}
}

@ -0,0 +1,37 @@
<?php
namespace App\Tests\Form\Type;
use App\Form\Type\PostType;
use Symfony\Component\Form\Test\TypeTestCase;
class PostTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$formData = [
'title' => 'Test Title',
'text' => 'Lorem ipsum dolor sit amet.',
'dream' => true,
];
$form = $this->factory->create(PostType::class);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($formData['title'], $form->get('title')->getData());
$this->assertEquals($formData['text'], $form->get('text')->getData());
$this->assertEquals($formData['dream'], $form->get('dream')->getData());
}
public function testFormFields()
{
$form = $this->factory->create(PostType::class);
$this->assertTrue($form->has('title'));
$this->assertTrue($form->has('text'));
$this->assertTrue($form->has('dream'));
$this->assertTrue($form->has('submit'));
}
}

@ -0,0 +1,97 @@
<?php
namespace App\Tests\Repository;
use App\Entity\Profil;
use App\Repository\ProfilRepository;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ProfilRepositoryTest extends WebTestCase
{
private ProfilRepository $profilRepository;
private mixed $em;
protected function setUp(): void
{
self::bootKernel();
$this->em = static::getContainer()->get(EntityManagerInterface::class);
$this->profilRepository = $this->em->getRepository(Profil::class);
}
public function testFindById()
{
// Créer un profil pour tester
$profil = new Profil();
$profil->setName('John Doe');
$profil->setDescription('Jean Dupont');
$this->em->persist($profil);
$this->em->flush();
$profilId = $profil->getId();
$foundProfil = $this->profilRepository->find($profilId);
$this->assertEquals('John Doe', $foundProfil->getName());
$this->assertEquals('Jean Dupont', $foundProfil->getDescription());
}
public function testFindByExampleField()
{
// Créer plusieurs profils pour tester
$profil1 = new Profil();
$profil1->setName('Alice');
$profil1->setDescription('Alice\'s profile');
$this->em->persist($profil1);
$profil2 = new Profil();
$profil2->setName('Bob');
$profil2->setDescription('Bob\'s profile');
$this->em->persist($profil2);
$this->em->flush();
// Rechercher les profils par nom
$foundProfils = $this->profilRepository->findBy(['name' => 'Alice']);
// Vérifier si le bon profil a été trouvé
$this->assertCount(1, $foundProfils);
$this->assertEquals('Alice', $foundProfils[0]->getName());
$this->assertEquals('Alice\'s profile', $foundProfils[0]->getDescription());
}
public function testFindOneBySomeField()
{
// Créer un profil pour tester
$profil = new Profil();
$profil->setName('John Doe');
$profil->setDescription('Jean Dupont');
$this->em->persist($profil);
$this->em->flush();
// Rechercher le profil par nom
$foundProfil = $this->profilRepository->findOneBy(['name' => 'John Doe']);
$this->assertEquals('John Doe', $foundProfil->getName());
$this->assertEquals('Jean Dupont', $foundProfil->getDescription());
}
/**
* @throws Exception
*/
protected function tearDown(): void
{
parent::tearDown();
$this->em->getConnection()->executeStatement('DELETE FROM profil');
$this->em->close();
}
}
Loading…
Cancel
Save