Add last phpunit test

pull/27/head
Dorian HODIN 10 months ago
parent 1c50a27746
commit 2b42aa4f66

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

@ -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',
])
;
}

@ -18,14 +18,6 @@ class PostControllerTest extends WebTestCase
$this->em = static::getContainer()->get(EntityManagerInterface::class);
}
public function testGetAllPost()
{
$crawler = $this->client->request('GET', '/');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'FukaFukashita');
}
public function testGetPost()
{

@ -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'));
}
}

@ -19,7 +19,6 @@ class ProfilRepositoryTest extends WebTestCase
{
self::bootKernel();
$this->client = static::createClient();
$this->em = static::getContainer()->get(EntityManagerInterface::class);
@ -35,13 +34,10 @@ class ProfilRepositoryTest extends WebTestCase
$this->em->persist($profil);
$this->em->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());
}

Loading…
Cancel
Save