Hi All,
If anyone has any pointers on my below issue it would be greatly appreciated.
I have been playing around with pest and attempted to write a test for this IsValidEmailAddress rule i have been playing with.
Custom validation rule https://laravel.com/docs/10.x/validation#custom-validation-rules
but regardless of what avenue i try and go down to validate the test i keep hitting a roadblock, reading the facade documentation https://laravel.com/api/8.x/Illuminate/Contracts/Validation/Validator.html validated() should return the attributes that were validated but its always an empty array.
Validation Rule
class IsValidEmailAddress implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(! is_string($value)) {
$fail('The value must be a string!');
}
if(!preg_match_all('/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/', $value))
{
$fail('The value must be a valid email address!');
}
}
}
Tests
it('can validate an email', function () {
$validator = Validator::make(['email' => '[email protected]'], [new IsValidEmailAddress()]);
expect($validator->validated())->toBe(['email' => '[email protected]']);
});
Output
FAILED Tests\Feature\emailTest > it can validate an email
Failed asserting that two arrays are identical.
-Array &0 (
- 'email' => '[email protected]'
-)
+Array &0 ()