Skip to content

Commit d58acf1

Browse files
committed
Add EmailRule with validation and tests, update DateRule
Introduce EmailRule for validating email addresses using regex, along with comprehensive unit tests. Update DateRule's validation messages for consistency and adjust corresponding test assertions.
1 parent 6a2d0ac commit d58acf1

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
lines changed

src/Validation/Rules/DateRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function make(?array $parameters = null): self
5555
public function validationMessage(): string
5656
{
5757
return empty($this->formats)
58-
? 'The :attribute must be a valid date.'
59-
: 'The :attribute must be a valid date in one of the following formats: ' . implode(', ', $this->formats);
58+
? 'The :attribute field must be a valid date.'
59+
: 'The :attribute field must be a valid date in one of the following formats: ' . implode(', ', $this->formats);
6060
}
6161
}

src/Validation/Rules/EmailRule.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Validation\Rules;
4+
5+
use Nuxtifyts\PhpDto\Validation\Rules\ValidationRule;
6+
use Override;
7+
8+
class EmailRule extends RegexRule
9+
{
10+
public string $name {
11+
get {
12+
return 'email';
13+
}
14+
}
15+
16+
/**
17+
* @param ?array<string, mixed> $parameters
18+
*/
19+
#[Override]
20+
public static function make(?array $parameters = null): self
21+
{
22+
$instance = new self();
23+
24+
$instance->pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';
25+
26+
return $instance;
27+
}
28+
29+
#[Override]
30+
public function validationMessage(): string
31+
{
32+
return 'The :attribute field must be a valid email address.';
33+
}
34+
}

tests/Unit/Validation/Rules/DateRuleTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ final class DateRuleTest extends ValidationRuleTestCase
1919
#[Test]
2020
public function validate_validation_message(): void
2121
{
22-
$rule = DateRule::make();
22+
self::assertEquals(
23+
'The :attribute field must be a valid date.',
24+
DateRule::make()->validationMessage()
25+
);
2326

2427
self::assertEquals(
25-
'The :attribute must be a valid date.',
26-
$rule->validationMessage()
28+
'The :attribute field must be a valid date in one of the following formats: Y/m-d H/m/s',
29+
DateRule::make(['formats' => ['Y/m-d H/m/s']])->validationMessage()
2730
);
2831
}
2932

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Validation\Rules;
4+
5+
use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException;
6+
use Nuxtifyts\PhpDto\Validation\Rules\EmailRule;
7+
use Nuxtifyts\PhpDto\Validation\Rules\ValidationRule;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use Throwable;
11+
12+
#[CoversClass(EmailRule::class)]
13+
final class EmailRuleTest extends ValidationRuleTestCase
14+
{
15+
/**
16+
* @throws Throwable
17+
*/
18+
#[Test]
19+
public function validate_validation_message(): void
20+
{
21+
self::assertEquals(
22+
'The :attribute field must be a valid email address.',
23+
EmailRule::make()->validationMessage()
24+
);
25+
}
26+
27+
/**
28+
* @return array<string, array{
29+
* validationRuleClassString: class-string<ValidationRule>,
30+
* makeParams: ?array<string, mixed>,
31+
* expectedMakeException: ?class-string<ValidationRuleException>,
32+
* valueToBeEvaluated: mixed,
33+
* expectedResult: bool
34+
* }>
35+
*/
36+
public static function data_provider(): array
37+
{
38+
return [
39+
'Will evaluate false when value is not a string' => [
40+
'validationRuleClassString' => EmailRule::class,
41+
'makeParams' => null,
42+
'expectedMakeException' => null,
43+
'valueToBeEvaluated' => 'test1234',
44+
'expectedResult' => false
45+
],
46+
'Will evaluate false when a string value is provided but it is not a valid email address' => [
47+
'validationRuleClassString' => EmailRule::class,
48+
'makeParams' => null,
49+
'expectedMakeException' => null,
50+
'valueToBeEvaluated' => 'test',
51+
'expectedResult' => false
52+
],
53+
'Will evaluate true when a valid email address is provided' => [
54+
'validationRuleClassString' => EmailRule::class,
55+
'makeParams' => null,
56+
'expectedMakeException' => null,
57+
'valueToBeEvaluated' => '[email protected]',
58+
'expectedResult' => true,
59+
],
60+
];
61+
}
62+
}

0 commit comments

Comments
 (0)