Skip to content

Commit

Permalink
Added ValidatorCest
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez committed May 1, 2024
1 parent 6886a99 commit aeaf521
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 479 deletions.
898 changes: 455 additions & 443 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions resources/views/security/register.html.twig
Expand Up @@ -11,9 +11,7 @@

{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword, {
label: 'Password'
}) }}
{{ form_row(registrationForm.password) }}
{{ form_row(registrationForm.agreeTerms) }}

<button type="submit">Register</button>
Expand Down
3 changes: 0 additions & 3 deletions src/Controller/RegistrationController.php
Expand Up @@ -30,9 +30,6 @@ public function __invoke(Request $request): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$plainPassword = $form->get('plainPassword')->getData();
$user->setPassword($plainPassword);

$this->userRepository->save($user);

$this->mailer->sendConfirmationEmail($user);
Expand Down
3 changes: 2 additions & 1 deletion src/Doctrine/UserHashPasswordListener.php
Expand Up @@ -21,7 +21,8 @@ public function prePersist(User $user): void

$user->setPassword(
$this->hasher->hashPassword(
$user, $user->getPassword()
$user,
$user->getPassword()
)
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/User.php
Expand Up @@ -9,6 +9,7 @@
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
Expand All @@ -20,12 +21,17 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private ?int $id = null;

#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(min: 3)]
#[Assert\Email(message: 'The email {{ value }} is not a valid email.')]
private string $email = '';

#[ORM\Column(type: 'json')]
private array $roles = [];

#[ORM\Column(type: 'string')]
#[Assert\NotBlank(message: 'Please enter a password')]
#[Assert\Length(min: 6, minMessage: 'Your password should be at least {{ limit }} characters')]
private string $password = '';

public static function create(string $email, string $password, array $roles = []): self
Expand Down
16 changes: 1 addition & 15 deletions src/Form/RegistrationFormType.php
Expand Up @@ -11,8 +11,6 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

final class RegistrationFormType extends AbstractType
{
Expand All @@ -28,19 +26,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
]),
],
])
->add('plainPassword', PasswordType::class, [
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
'max' => 4096,
]),
],
])
->add('password', PasswordType::class)
;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/BrowserCest.php
Expand Up @@ -29,7 +29,7 @@ public function submitSymfonyForm(FunctionalTester $I)
$I->amOnPage('/register');
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$I->seeInRepository(User::class, [
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/EventsCest.php
Expand Up @@ -84,7 +84,7 @@ public function seeOrphanEvent(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true,
]);
$I->seeOrphanEvent(UserRegisteredEvent::class);
Expand All @@ -96,7 +96,7 @@ public function seeEvent(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true,
]);
$I->seeEvent(UserRegisteredEvent::class);
Expand Down
14 changes: 7 additions & 7 deletions tests/Functional/FormCest.php
Expand Up @@ -13,7 +13,7 @@ public function dontSeeFormErrors(FunctionalTester $I)
$I->amOnPage('/register');
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$I->dontSeeFormErrors();
Expand All @@ -24,7 +24,7 @@ public function seeFormErrorMessage(FunctionalTester $I)
$I->amOnPage('/register');
$I->submitSymfonyForm('registration_form', [
'[email]' => 'john_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$I->seeFormErrorMessage('email');
Expand All @@ -36,19 +36,19 @@ public function seeFormErrorMessages(FunctionalTester $I)
$I->amOnPage('/register');
$I->submitSymfonyForm('registration_form', [
'[email]' => 'john_doe@gmail.com',
'[plainPassword]' => '123',
'[password]' => '123',
'[agreeTerms]' => true
]);

// Only with the names of the fields
$I->seeFormErrorMessages(['email', 'plainPassword']);
$I->seeFormErrorMessages(['email', 'password']);

// With field names and error messages
$I->seeFormErrorMessages([
// Full Message
'email' => 'There is already an account with this email',
// Part of a message
'plainPassword' => 'at least 6 characters'
'password' => 'at least 6 characters'
]);
}

Expand All @@ -57,10 +57,10 @@ public function seeFormHasErrors(FunctionalTester $I)
$I->amOnPage('/register');
$I->submitSymfonyForm('registration_form', [
'[email]' => 'john_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
//There is already an account with this email
// There is already an account with this email
$I->seeFormHasErrors();
}
}
8 changes: 4 additions & 4 deletions tests/Functional/MailerCest.php
Expand Up @@ -14,7 +14,7 @@ public function dontSeeEmailIsSent(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'john_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
//There is already an account with this email
Expand All @@ -27,7 +27,7 @@ public function grabLastSentEmail(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$email = $I->grabLastSentEmail();
Expand All @@ -41,7 +41,7 @@ public function grabSentEmails(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$emails = $I->grabSentEmails();
Expand All @@ -55,7 +55,7 @@ public function seeEmailIsSent(FunctionalTester $I)
$I->stopFollowingRedirects();
$I->submitSymfonyForm('registration_form', [
'[email]' => 'jane_doe@gmail.com',
'[plainPassword]' => '123456',
'[password]' => '123456',
'[agreeTerms]' => true
]);
$I->seeEmailIsSent();
Expand Down
62 changes: 62 additions & 0 deletions tests/Functional/ValidatorCest.php
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional;

use App\Entity\User;
use App\Tests\Support\FunctionalTester;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

final class ValidatorCest
{
public function dontSeeViolatedConstraint(FunctionalTester $I)
{
$user = User::create('test@example.com', 'password', ['ROLE_ADMIN']);
$I->dontSeeViolatedConstraint($user);
$I->dontSeeViolatedConstraint($user, 'email');
$I->dontSeeViolatedConstraint($user, 'email', Email::class);

$user->setEmail('invalid_email');
$I->dontSeeViolatedConstraint($user, 'password');

$user->setEmail('test@example.com');
$user->setPassword('weak');
$I->dontSeeViolatedConstraint($user, 'email');
$I->dontSeeViolatedConstraint($user, 'password', NotBlank::class);
}

public function seeViolatedConstraint(FunctionalTester $I)
{
$user = User::create('invalid_email', 'password', ['ROLE_ADMIN']);
$I->seeViolatedConstraint($user);
$I->seeViolatedConstraint($user, 'email');

$user->setEmail('test@example.com');
$user->setPassword('weak');
$I->seeViolatedConstraint($user);
$I->seeViolatedConstraint($user, 'password');
$I->seeViolatedConstraint($user, 'password', Length::class);
}

public function seeViolatedConstraintCount(FunctionalTester $I)
{
$user = User::create('invalid_email', 'weak', ['ROLE_ADMIN']);
$I->seeViolatedConstraintsCount(2, $user);
$I->seeViolatedConstraintsCount(1, $user, 'email');
$user->setEmail('test@example.com');
$I->seeViolatedConstraintsCount(1, $user);
$I->seeViolatedConstraintsCount(0, $user, 'email');
}

public function seeViolatedConstraintMessageContains(FunctionalTester $I)
{
$user = User::create('invalid_email', 'weak', ['ROLE_ADMIN']);
$I->seeViolatedConstraintMessage('is not a valid email', $user, 'email');
$user->setEmail('');
$I->seeViolatedConstraintMessage('should not be blank', $user, 'email');
$I->seeViolatedConstraintMessage('This value is too short', $user, 'email');
}
}

0 comments on commit aeaf521

Please sign in to comment.