Adding test php, removing useless test

pull/27/head
Dorian HODIN 5 months ago
parent a6bc54b187
commit ed52e15aa8

@ -36,16 +36,13 @@ class PostController extends AbstractController
#[Route(
'/post/{id}',
name: 'display_post',
methods: ['GET', 'POST'],
requirements: ['id' => '\d+']
requirements: ['id' => '\d+'],
methods: ['GET', 'POST']
)]
public function getPost(int $id, Request $request): Response
{
$post = $this->em->getRepository(Post::class)->find($id);
if (!$post) {
}
$comment = new Commentary();
$commentForm = $this->createForm(CommentType::class, $comment);

@ -1,6 +1,6 @@
<?php
namespace App\Form\Type;
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

@ -1,84 +0,0 @@
<?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;
}
}

@ -1,97 +0,0 @@
<?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