Adding phpunit test on profil

pull/27/head
Dorian HODIN 10 months ago
parent 63aa794e98
commit 1c50a27746

662
composer.lock generated

File diff suppressed because it is too large Load Diff

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

@ -2,51 +2,59 @@
namespace App\Tests\Entity; namespace App\Tests\Entity;
use App\Entity\Commentary;
use App\Entity\Post;
use App\Entity\Profil; use App\Entity\Profil;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\Entity\Post;
use App\Entity\Commentary;
class ProfilTest extends TestCase class ProfilTest extends TestCase
{ {
public function testGetId() public function test_it_can_be_instantiated(): void
{ {
$profil = new Profil(); $profil = new Profil();
$this->assertNull($profil->getId()); $this->assertInstanceOf(Profil::class, $profil);
} }
public function testGetSetName() public function test_name()
{ {
$profil = new Profil(); $profil = new Profil();
$name = 'TestUser'; $profil->setName('John Doe');
$profil->setName($name); $this->assertEquals('John Doe', $profil->getName());
$this->assertSame($name, $profil->getName());
} }
public function testGetSetDescription() public function test_description()
{ {
$profil = new Profil(); $profil = new Profil();
$description = 'This is a test description.'; $profil->setDescription('Lorem ipsum');
$profil->setDescription($description); $this->assertEquals('Lorem ipsum', $profil->getDescription());
}
$this->assertSame($description, $profil->getDescription()); public function test_password()
{
$profil = new Profil();
$profil->setPassword('password123');
$this->assertEquals('password123', $profil->getPassword());
} }
public function testGetSetPassword() public function test_roles()
{ {
$profil = new Profil(); $profil = new Profil();
$password = 'password123'; $roles = ['ROLE_USER', 'ROLE_ADMIN'];
$profil->setPassword($password); $profil->setRoles($roles);
$this->assertEquals($roles, $profil->getRoles());
}
$this->assertSame($password, $profil->getPassword()); public function test_user_identifier()
{
$profil = new Profil();
$profil->setName('johndoe');
$this->assertEquals('johndoe', $profil->getUserIdentifier());
} }
public function testAddRemovePost() public function test_add_and_remove_post()
{ {
$profil = new Profil(); $profil = new Profil();
$post = new Post(); $post = new Post();
$profil->addPost($post); $profil->addPost($post);
$this->assertTrue($profil->getPosts()->contains($post)); $this->assertTrue($profil->getPosts()->contains($post));
@ -54,11 +62,10 @@ class ProfilTest extends TestCase
$this->assertFalse($profil->getPosts()->contains($post)); $this->assertFalse($profil->getPosts()->contains($post));
} }
public function testAddRemoveCommentary() public function test_add_and_remove_commentary()
{ {
$profil = new Profil(); $profil = new Profil();
$commentary = new Commentary(); $commentary = new Commentary();
$profil->addCommentary($commentary); $profil->addCommentary($commentary);
$this->assertTrue($profil->getCommentaries()->contains($commentary)); $this->assertTrue($profil->getCommentaries()->contains($commentary));
@ -66,31 +73,19 @@ class ProfilTest extends TestCase
$this->assertFalse($profil->getCommentaries()->contains($commentary)); $this->assertFalse($profil->getCommentaries()->contains($commentary));
} }
public function testGetSetRoles() public function test_add_and_remove_follower()
{
$profil = new Profil();
$roles = ['ROLE_ADMIN', 'ROLE_USER'];
$profil->setRoles($roles);
$this->assertSame($roles, $profil->getRoles());
}
public function testAddRemoveFollower()
{ {
$profil1 = new Profil(); $profil1 = new Profil();
$profil2 = new Profil(); $profil2 = new Profil();
$profil1->addFollower($profil2); $profil1->addFollower($profil2);
print_r($profil1->getFollowing());
$this->assertTrue($profil1->getFollowers()->contains($profil2)); $this->assertTrue($profil1->getFollowers()->contains($profil2));
$this->assertTrue($profil2->getFollowing()->contains($profil1));
$profil1->removeFollower($profil2); $profil1->removeFollower($profil2);
$this->assertFalse($profil1->getFollowers()->contains($profil2)); $this->assertFalse($profil1->getFollowers()->contains($profil2));
$this->assertFalse($profil2->getFollowing()->contains($profil1));
} }
public function testAddRemoveFollowing() public function test_add_and_remove_following()
{ {
$profil1 = new Profil(); $profil1 = new Profil();
$profil2 = new Profil(); $profil2 = new Profil();
@ -104,4 +99,3 @@ class ProfilTest extends TestCase
$this->assertFalse($profil2->getFollowers()->contains($profil1)); $this->assertFalse($profil2->getFollowers()->contains($profil1));
} }
} }

@ -1,34 +1,29 @@
<?php <?php
namespace App\Tests\Repository;
use App\Entity\Profil; use App\Entity\Profil;
use App\Repository\ProfilRepository; use App\Repository\ProfilRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ProfilRepositoryTest extends KernelTestCase class ProfilRepositoryTest extends WebTestCase
{ {
private ProfilRepository $profilRepository;
private mixed $em;
/**
* @var ProfilRepository
*/
private $profilRepository;
public function __construct(private EntityManagerInterface $entityManager) {
}
protected function setUp(): void protected function setUp(): void
{ {
self::bootKernel(); self::bootKernel();
// Récupérer l'EntityManager $this->client = static::createClient();
$this->em = static::getContainer()->get(EntityManagerInterface::class);
// Récupérer le repository Profil
$this->profilRepository = $this->entityManager->getRepository(Profil::class); $this->profilRepository = $this->em->getRepository(Profil::class);
} }
public function testFindById() public function testFindById()
@ -37,8 +32,8 @@ class ProfilRepositoryTest extends KernelTestCase
$profil = new Profil(); $profil = new Profil();
$profil->setName('John Doe'); $profil->setName('John Doe');
$profil->setDescription('Jean Dupont'); $profil->setDescription('Jean Dupont');
$this->entityManager->persist($profil); $this->em->persist($profil);
$this->entityManager->flush(); $this->em->flush();
// Récupérer l'ID du profil // Récupérer l'ID du profil
$profilId = $profil->getId(); $profilId = $profil->getId();
@ -57,14 +52,14 @@ class ProfilRepositoryTest extends KernelTestCase
$profil1 = new Profil(); $profil1 = new Profil();
$profil1->setName('Alice'); $profil1->setName('Alice');
$profil1->setDescription('Alice\'s profile'); $profil1->setDescription('Alice\'s profile');
$this->entityManager->persist($profil1); $this->em->persist($profil1);
$profil2 = new Profil(); $profil2 = new Profil();
$profil2->setName('Bob'); $profil2->setName('Bob');
$profil2->setDescription('Bob\'s profile'); $profil2->setDescription('Bob\'s profile');
$this->entityManager->persist($profil2); $this->em->persist($profil2);
$this->entityManager->flush(); $this->em->flush();
// Rechercher les profils par nom // Rechercher les profils par nom
$foundProfils = $this->profilRepository->findBy(['name' => 'Alice']); $foundProfils = $this->profilRepository->findBy(['name' => 'Alice']);
@ -81,8 +76,8 @@ class ProfilRepositoryTest extends KernelTestCase
$profil = new Profil(); $profil = new Profil();
$profil->setName('John Doe'); $profil->setName('John Doe');
$profil->setDescription('Jean Dupont'); $profil->setDescription('Jean Dupont');
$this->entityManager->persist($profil); $this->em->persist($profil);
$this->entityManager->flush(); $this->em->flush();
// Rechercher le profil par nom // Rechercher le profil par nom
$foundProfil = $this->profilRepository->findOneBy(['name' => 'John Doe']); $foundProfil = $this->profilRepository->findOneBy(['name' => 'John Doe']);
@ -91,13 +86,16 @@ class ProfilRepositoryTest extends KernelTestCase
$this->assertEquals('Jean Dupont', $foundProfil->getDescription()); $this->assertEquals('Jean Dupont', $foundProfil->getDescription());
} }
/**
* @throws Exception
*/
protected function tearDown(): void protected function tearDown(): void
{ {
parent::tearDown(); parent::tearDown();
$this->entityManager->getConnection()->executeStatement('DELETE FROM profil'); $this->em->getConnection()->executeStatement('DELETE FROM profil');
$this->entityManager->close(); $this->em->close();
} }
} }

@ -1,38 +0,0 @@
<?php
use App\Entity\Commentary;
use App\Entity\Post;
use App\Entity\Profil;
use PHPUnit\Framework\TestCase;
class CommentaryTest extends TestCase
{
public function test_it_can_be_instantiated()
{
$commentary = new Commentary();
$this->assertInstanceOf(Commentary::class, $commentary);
}
// public function test_text()
// {
// $commentary = new Commentary();
// $commentary->setText('Lorem ipsum');
// $this->assertEquals('Lorem ipsum', $commentary->getText());
// }
// public function test_post_association()
// {
// $commentary = new Commentary();
// $post = new Post(); // Assuming Post is properly defined
// $commentary->setPost($post);
// $this->assertInstanceOf(Post::class, $commentary->getPost());
// }
// public function test_profil_association()
// {
// $commentary = new Commentary();
// $profil = new Profil(); // Assuming Profil is properly defined
// $commentary->setProfil($profil);
// $this->assertInstanceOf(Profil::class, $commentary->getProfil());
// }
}

@ -1,99 +0,0 @@
<?php
use App\Entity\Profil;
use App\Entity\Post;
use App\Entity\Commentary;
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));
// }
}
Loading…
Cancel
Save