Skip to content

Commit 1526b70

Browse files
committed
Added ValidatorCest
1 parent c76bc07 commit 1526b70

File tree

11 files changed

+625
-512
lines changed

11 files changed

+625
-512
lines changed

composer.lock

+539-476
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/security/register.html.twig

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
{{ form_start(registrationForm) }}
1313
{{ form_row(registrationForm.email) }}
14-
{{ form_row(registrationForm.plainPassword, {
15-
label: 'Password'
16-
}) }}
14+
{{ form_row(registrationForm.password) }}
1715
{{ form_row(registrationForm.agreeTerms) }}
1816

1917
<button type="submit">Register</button>

src/Controller/RegistrationController.php

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ public function __invoke(Request $request): Response
3030
$form->handleRequest($request);
3131

3232
if ($form->isSubmitted() && $form->isValid()) {
33-
$plainPassword = $form->get('plainPassword')->getData();
34-
$user->setPassword($plainPassword);
35-
3633
$this->userRepository->save($user);
3734

3835
$this->mailer->sendConfirmationEmail($user);

src/Doctrine/UserHashPasswordListener.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function prePersist(User $user): void
2121

2222
$user->setPassword(
2323
$this->hasher->hashPassword(
24-
$user, $user->getPassword()
24+
$user,
25+
$user->getPassword()
2526
)
2627
);
2728
}

src/Entity/User.php

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1010
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1111
use Symfony\Component\Security\Core\User\UserInterface;
12+
use Symfony\Component\Validator\Constraints as Assert;
1213

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

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

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

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

3137
public static function create(string $email, string $password, array $roles = []): self

src/Form/RegistrationFormType.php

+1-15
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use Symfony\Component\Form\FormBuilderInterface;
1212
use Symfony\Component\OptionsResolver\OptionsResolver;
1313
use Symfony\Component\Validator\Constraints\IsTrue;
14-
use Symfony\Component\Validator\Constraints\Length;
15-
use Symfony\Component\Validator\Constraints\NotBlank;
1614

1715
final class RegistrationFormType extends AbstractType
1816
{
@@ -28,19 +26,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2826
]),
2927
],
3028
])
31-
->add('plainPassword', PasswordType::class, [
32-
'mapped' => false,
33-
'constraints' => [
34-
new NotBlank([
35-
'message' => 'Please enter a password',
36-
]),
37-
new Length([
38-
'min' => 6,
39-
'minMessage' => 'Your password should be at least {{ limit }} characters',
40-
'max' => 4096,
41-
]),
42-
],
43-
])
29+
->add('password', PasswordType::class)
4430
;
4531
}
4632

tests/Functional/BrowserCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function submitSymfonyForm(FunctionalTester $I)
2929
$I->amOnPage('/register');
3030
$I->submitSymfonyForm('registration_form', [
3131
'[email]' => '[email protected]',
32-
'[plainPassword]' => '123456',
32+
'[password]' => '123456',
3333
'[agreeTerms]' => true
3434
]);
3535
$I->seeInRepository(User::class, [

tests/Functional/EventsCest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function seeOrphanEvent(FunctionalTester $I)
8484
$I->stopFollowingRedirects();
8585
$I->submitSymfonyForm('registration_form', [
8686
'[email]' => '[email protected]',
87-
'[plainPassword]' => '123456',
87+
'[password]' => '123456',
8888
'[agreeTerms]' => true,
8989
]);
9090
$I->seeOrphanEvent(UserRegisteredEvent::class);
@@ -96,7 +96,7 @@ public function seeEvent(FunctionalTester $I)
9696
$I->stopFollowingRedirects();
9797
$I->submitSymfonyForm('registration_form', [
9898
'[email]' => '[email protected]',
99-
'[plainPassword]' => '123456',
99+
'[password]' => '123456',
100100
'[agreeTerms]' => true,
101101
]);
102102
$I->seeEvent(UserRegisteredEvent::class);

tests/Functional/FormCest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function dontSeeFormErrors(FunctionalTester $I)
1313
$I->amOnPage('/register');
1414
$I->submitSymfonyForm('registration_form', [
1515
'[email]' => '[email protected]',
16-
'[plainPassword]' => '123456',
16+
'[password]' => '123456',
1717
'[agreeTerms]' => true
1818
]);
1919
$I->dontSeeFormErrors();
@@ -24,7 +24,7 @@ public function seeFormErrorMessage(FunctionalTester $I)
2424
$I->amOnPage('/register');
2525
$I->submitSymfonyForm('registration_form', [
2626
'[email]' => '[email protected]',
27-
'[plainPassword]' => '123456',
27+
'[password]' => '123456',
2828
'[agreeTerms]' => true
2929
]);
3030
$I->seeFormErrorMessage('email');
@@ -36,19 +36,19 @@ public function seeFormErrorMessages(FunctionalTester $I)
3636
$I->amOnPage('/register');
3737
$I->submitSymfonyForm('registration_form', [
3838
'[email]' => '[email protected]',
39-
'[plainPassword]' => '123',
39+
'[password]' => '123',
4040
'[agreeTerms]' => true
4141
]);
4242

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

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

@@ -57,10 +57,10 @@ public function seeFormHasErrors(FunctionalTester $I)
5757
$I->amOnPage('/register');
5858
$I->submitSymfonyForm('registration_form', [
5959
'[email]' => '[email protected]',
60-
'[plainPassword]' => '123456',
60+
'[password]' => '123456',
6161
'[agreeTerms]' => true
6262
]);
63-
//There is already an account with this email
63+
// There is already an account with this email
6464
$I->seeFormHasErrors();
6565
}
6666
}

tests/Functional/MailerCest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function dontSeeEmailIsSent(FunctionalTester $I)
1414
$I->stopFollowingRedirects();
1515
$I->submitSymfonyForm('registration_form', [
1616
'[email]' => '[email protected]',
17-
'[plainPassword]' => '123456',
17+
'[password]' => '123456',
1818
'[agreeTerms]' => true
1919
]);
2020
//There is already an account with this email
@@ -27,7 +27,7 @@ public function grabLastSentEmail(FunctionalTester $I)
2727
$I->stopFollowingRedirects();
2828
$I->submitSymfonyForm('registration_form', [
2929
'[email]' => '[email protected]',
30-
'[plainPassword]' => '123456',
30+
'[password]' => '123456',
3131
'[agreeTerms]' => true
3232
]);
3333
$email = $I->grabLastSentEmail();
@@ -41,7 +41,7 @@ public function grabSentEmails(FunctionalTester $I)
4141
$I->stopFollowingRedirects();
4242
$I->submitSymfonyForm('registration_form', [
4343
'[email]' => '[email protected]',
44-
'[plainPassword]' => '123456',
44+
'[password]' => '123456',
4545
'[agreeTerms]' => true
4646
]);
4747
$emails = $I->grabSentEmails();
@@ -55,7 +55,7 @@ public function seeEmailIsSent(FunctionalTester $I)
5555
$I->stopFollowingRedirects();
5656
$I->submitSymfonyForm('registration_form', [
5757
'[email]' => '[email protected]',
58-
'[plainPassword]' => '123456',
58+
'[password]' => '123456',
5959
'[agreeTerms]' => true
6060
]);
6161
$I->seeEmailIsSent();

tests/Functional/ValidatorCest.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional;
6+
7+
use App\Entity\User;
8+
use App\Tests\Support\FunctionalTester;
9+
use Symfony\Component\Validator\Constraints\Email;
10+
use Symfony\Component\Validator\Constraints\Length;
11+
use Symfony\Component\Validator\Constraints\NotBlank;
12+
13+
final class ValidatorCest
14+
{
15+
public function dontSeeViolatedConstraint(FunctionalTester $I)
16+
{
17+
$user = User::create('[email protected]', 'password', ['ROLE_ADMIN']);
18+
$I->dontSeeViolatedConstraint($user);
19+
$I->dontSeeViolatedConstraint($user, 'email');
20+
$I->dontSeeViolatedConstraint($user, 'email', Email::class);
21+
22+
$user->setEmail('invalid_email');
23+
$I->dontSeeViolatedConstraint($user, 'password');
24+
25+
$user->setEmail('[email protected]');
26+
$user->setPassword('weak');
27+
$I->dontSeeViolatedConstraint($user, 'email');
28+
$I->dontSeeViolatedConstraint($user, 'password', NotBlank::class);
29+
}
30+
31+
public function seeViolatedConstraint(FunctionalTester $I)
32+
{
33+
$user = User::create('invalid_email', 'password', ['ROLE_ADMIN']);
34+
$I->seeViolatedConstraint($user);
35+
$I->seeViolatedConstraint($user, 'email');
36+
37+
$user->setEmail('[email protected]');
38+
$user->setPassword('weak');
39+
$I->seeViolatedConstraint($user);
40+
$I->seeViolatedConstraint($user, 'password');
41+
$I->seeViolatedConstraint($user, 'password', Length::class);
42+
}
43+
44+
public function seeViolatedConstraintCount(FunctionalTester $I)
45+
{
46+
$user = User::create('invalid_email', 'weak', ['ROLE_ADMIN']);
47+
$I->seeViolatedConstraintsCount(2, $user);
48+
$I->seeViolatedConstraintsCount(1, $user, 'email');
49+
$user->setEmail('[email protected]');
50+
$I->seeViolatedConstraintsCount(1, $user);
51+
$I->seeViolatedConstraintsCount(0, $user, 'email');
52+
}
53+
54+
public function seeViolatedConstraintMessageContains(FunctionalTester $I)
55+
{
56+
$user = User::create('invalid_email', 'weak', ['ROLE_ADMIN']);
57+
$I->seeViolatedConstraintMessage('is not a valid email', $user, 'email');
58+
$user->setEmail('');
59+
$I->seeViolatedConstraintMessage('should not be blank', $user, 'email');
60+
$I->seeViolatedConstraintMessage('This value is too short', $user, 'email');
61+
}
62+
}

0 commit comments

Comments
 (0)