Adding test

pull/27/head
Corentin RICHARD 5 months ago
parent b19c9f2246
commit af309b3cdc

@ -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}}

@ -0,0 +1,103 @@
<?php
use App\Entity\Profil;
use App\Repository\ProfilRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ProfilRepositoryTest extends KernelTestCase
{
/**
* @var ProfilRepository
*/
private $profilRepository;
public function __construct(private EntityManagerInterface $entityManager) {
}
protected function setUp(): void
{
self::bootKernel();
// Récupérer l'EntityManager
// Récupérer le repository Profil
$this->profilRepository = $this->entityManager->getRepository(Profil::class);
}
public function testFindById()
{
// Créer un profil pour tester
$profil = new Profil();
$profil->setName('John Doe');
$profil->setDescription('Jean Dupont');
$this->entityManager->persist($profil);
$this->entityManager->flush();
// Récupérer l'ID du profil
$profilId = $profil->getId();
// Rechercher le profil par ID
$foundProfil = $this->profilRepository->find($profilId);
// Vérifier si le profil retrouvé correspond au profil créé
$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->entityManager->persist($profil1);
$profil2 = new Profil();
$profil2->setName('Bob');
$profil2->setDescription('Bob\'s profile');
$this->entityManager->persist($profil2);
$this->entityManager->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->entityManager->persist($profil);
$this->entityManager->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());
}
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->getConnection()->executeStatement('DELETE FROM profil');
$this->entityManager->close();
}
}

@ -1,6 +1,99 @@
<?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()
{
$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