pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.48k stars 343 forks source link

[2.x] Adds toBeEmail() Expectation #1088

Closed LucasLaurens closed 7 months ago

LucasLaurens commented 7 months ago

What:

Description:

This PR adds a toBeEmail() expectation, to validate whether a value is a valid EMAIL.

Related:

expect('test@example.com')->toBeEmail();
expect('test@localhost')->not->toBeEmail();
LucasLaurens commented 7 months ago

If the PR is accepted, I could create one for documentation.

LucasLaurens commented 7 months ago

I also have a question. Do I need to update the snapshots? This would seem normal if this feature is accepted.

LucasLaurens commented 7 months ago

I hadn't seen it before. But my PR matches this one exactly. So it doesn't make sense anymore.

LucasLaurens commented 7 months ago

Perhaps in the long term, if the intention is to add an expectation on the email, this could be improved with a DNS check.

[!NOTE] I was able to extend an expectation on a personal project where I check for a specific use case the validity of the email according to an RFC and DNS validation.

/** DNS Validation */
$dns = new DNSCheckValidation();

if (
    !$dns->isValid(
        email: $value,
        emailLexer: new \Egulias\EmailValidator\EmailLexer()
    )
) {
    return false;
}