[8.x] Cannot test with fake emails when using Email Validation rules. #39042
Replies: 5 comments
-
This is a feature request so I'm moving it to discussions. Also please note that you can't perform two HTTP calls in a single test as that's not a real-life scenario. |
Beta Was this translation helpful? Give feedback.
-
Assuming you are creating this test in a standard test file you should have the following at the top of the file. use Illuminate\Foundation\Testing\WithFaker; If you do then you can then use the use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Testing\WithFaker;
public function test_bad()
{
Route::post('test', function (Request $request): string {
$request->validate(['email' => 'email:dns,rfc']);
return 'ok';
});
$this->post('test', ['email' => $this->faker->freeEmail()])->assertOk();
} This will create a fake email with a genuine email domain allowing for the test to pass. |
Beta Was this translation helpful? Give feedback.
-
It would be good to have a way to mock the DNS check to avoid making requests in the tests. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I would also like the opportunity to mock the DNS check in testing. The only way I know how to mock it is by creating a custom rule that implements basically the same logic as the one below but in tests, just mock the calls. framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php Lines 851 to 882 in 73f5e1b I thought I would be able to mock the DNS by doing this, but the mock wasn't used by the $this->mock(DNSGetRecordWrapper::class, function (MockInterface $mock) {
$mock->shouldIgnoreMissing();
}); |
Beta Was this translation helpful? Give feedback.
-
I was able to solve this as follows: So, in my request, I have the following: public function rules() {
return [
'email' => 'required|email:strict,Egulias\EmailValidator\Validation\DNSCheckValidation',
];
} In my unit tests (only the ones that trigger the validation), I completely mock the DNS validation by doing the following: public function setUp(): void {
parent::setUp();
$this->app->bind(DNSCheckValidation::class, TestDNSCheckValidation::class);
} class TestDNSCheckValidation extends DNSCheckValidation {
/**
* In this test, we want the DNS validation to fail for fail.com domains, and pass for all others.
*/
public function isValid(string $email, EmailLexer $emailLexer): bool {
return !str_ends_with($email, 'fail.com');
}
} This way, my tests are happy even when I have no internet connection. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
There is no way to fake an email on testing when dealing with a route validating email with RFC, DNS, or a custom one. Since the rule cannot be bypassed instead of mocked or faked (just for the email), any test fails, unless the whole validation system is faked in an additional test case.
Steps To Reproduce:
The is no way to allow to bypass email validation, leaving these alternatives:
app()->environment('testing')
conditionally.Possible solutions.
EmailValidator
instance so it can be faked/mocked.fakeRules()
helper for the Validator to allow bypassing certain keys, or certain rule keys.Beta Was this translation helpful? Give feedback.
All reactions