Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 39cab83

Browse files
committed
chore: refactored tests for failure/success conditions
1 parent 2d16271 commit 39cab83

4 files changed

Lines changed: 110 additions & 104 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test\Util;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
7+
8+
trait TestRuleFailureConditionTrait
9+
{
10+
public static abstract function provideFailureConditionData(): \Generator;
11+
12+
#[DataProvider('provideFailureConditionData')]
13+
public function testRuleFailureCondition(
14+
RuleInterface $rule,
15+
mixed $value,
16+
string $expectedException,
17+
string $expectedExceptionMessage
18+
)
19+
{
20+
$this->assertFalse($rule->validate($value));
21+
22+
$this->expectException($expectedException);
23+
$this->expectExceptionMessageMatches($expectedExceptionMessage);
24+
$rule->assert($value, 'test');
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test\Util;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
7+
8+
trait TestRuleSuccessConditionTrait
9+
{
10+
public static abstract function provideSuccessConditionData(): \Generator;
11+
12+
#[DataProvider('provideSuccessConditionData')]
13+
public function testRuleSuccessCondition(RuleInterface $rule, mixed $value)
14+
{
15+
$rule->assert($value, 'test');
16+
17+
$this->assertTrue($rule->validate($value));
18+
}
19+
}

tests/GreaterThanTest.php

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\Util\FormatValueTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
810
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
911

1012
class GreaterThanTest extends AbstractTest
1113
{
12-
use FormatValueTrait;
14+
use TestRuleFailureConditionTrait;
15+
use TestRuleSuccessConditionTrait;
1316

1417
#[DataProvider('provideInvalidConditionData')]
1518
public function testGreaterThanValidateInvalidCondition(mixed $constraint, mixed $value)
@@ -52,66 +55,51 @@ public static function provideInvalidConditionData(): \Generator
5255
yield 'null constraint' => [null, 10];
5356
}
5457

55-
#[DataProvider('provideFailureConditionData')]
56-
public function testGreaterThanFailureCondition(mixed $constraint, mixed $value)
57-
{
58-
$validator = Validator::greaterThan($constraint);
59-
60-
$this->assertFalse($validator->validate($value));
61-
62-
$this->expectException(GreaterThanException::class);
63-
$this->expectExceptionMessage(
64-
\sprintf(
65-
'The "test" value should be greater than "%s", "%s" given.',
66-
$this->formatValue($constraint),
67-
$this->formatValue($value)
68-
)
69-
);
70-
$validator->assert($value, 'test');
71-
}
72-
7358
public static function provideFailureConditionData(): \Generator
7459
{
75-
yield 'datetime' => [new \DateTime('today'), new \DateTime('yesterday')];
76-
yield 'same datetime' => [new \DateTime('2000-01-01 00:00:00'), new \DateTime('2000-01-01 00:00:00')];
77-
yield 'int' => [10, 1];
78-
yield 'same int' => [10, 10];
79-
yield 'float' => [10.0, 1.0];
80-
yield 'same float' => [10.0, 10.0];
81-
yield 'int with float' => [10, 1.0];
82-
yield 'same int with float' => [10, 10.0];
83-
yield 'string' => ['z', 'a'];
84-
yield 'same string' => ['a', 'a'];
85-
}
86-
87-
#[DataProvider('provideSuccessConditionData')]
88-
public function testGreaterThanSuccessCondition(mixed $constraint, mixed $value)
89-
{
90-
$validator = Validator::greaterThan($constraint);
91-
92-
$this->assertTrue($validator->validate($value));
93-
94-
$validator->assert($value, 'test');
60+
$exception = GreaterThanException::class;
61+
$exceptionMessage = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
62+
63+
yield 'datetime' => [
64+
new GreaterThan(new \DateTime('today')),
65+
new \DateTime('yesterday'),
66+
$exception,
67+
$exceptionMessage
68+
];
69+
yield 'same datetime' => [
70+
new GreaterThan(new \DateTime('2000-01-01')),
71+
new \DateTime('2000-01-01'),
72+
$exception,
73+
$exceptionMessage
74+
];
75+
yield 'int' => [new GreaterThan(10), 1, $exception, $exceptionMessage];
76+
yield 'same int' => [new GreaterThan(10), 10, $exception, $exceptionMessage];
77+
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $exceptionMessage];
78+
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $exceptionMessage];
79+
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $exceptionMessage];
80+
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $exceptionMessage];
81+
yield 'string' => [new GreaterThan('z'), 'a', $exception, $exceptionMessage];
82+
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $exceptionMessage];
9583
}
9684

9785
public static function provideSuccessConditionData(): \Generator
9886
{
99-
yield 'datetime' => [new \DateTime('today'), new \DateTime('tomorrow')];
100-
yield 'int' => [10, 20];
101-
yield 'float' => [10.0, 20.0];
102-
yield 'int with float' => [10, 20.0];
103-
yield 'string' => ['a', 'z'];
87+
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('tomorrow')];
88+
yield 'int' => [new GreaterThan(10), 20];
89+
yield 'float' => [new GreaterThan(10.0), 20.0];
90+
yield 'int with float' => [new GreaterThan(10), 20.0];
91+
yield 'string' => [new GreaterThan('a'), 'z'];
10492
}
10593

106-
public function testGreaterThanMessageArgument()
107-
{
108-
$this->expectExceptionMessage('The "test" value "1" is invalid. Must not be greater than "10".');
109-
110-
Validator
111-
::greaterThan(
112-
constraint: 10,
113-
message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be greater than "{{ constraint }}".'
114-
)
115-
->assert(1, 'test');
116-
}
94+
// public function testGreaterThanMessageArgument()
95+
// {
96+
// $this->expectExceptionMessage('The "test" value "1" is invalid. Must not be greater than "10".');
97+
//
98+
// Validator
99+
// ::greaterThan(
100+
// constraint: 10,
101+
// message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be greater than "{{ constraint }}".'
102+
// )
103+
// ->assert(1, 'test');
104+
// }
117105
}

tests/NotBlankTest.php

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,41 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
44

5-
use PHPUnit\Framework\Attributes\DataProvider;
65
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\Util\FormatValueTrait;
8-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
99

1010
class NotBlankTest extends AbstractTest
1111
{
12-
use FormatValueTrait;
13-
14-
#[DataProvider('provideFailureConditionData')]
15-
public function testNotBlankFailureCondition(mixed $value)
16-
{
17-
$validator = Validator::notBlank();
18-
19-
$this->assertFalse($validator->validate($value));
20-
21-
$this->expectException(NotBlankException::class);
22-
$this->expectExceptionMessage(\sprintf('The "test" value should not be blank, "%s" given.', $this->formatValue($value)));
23-
$validator->assert($value, 'test');
24-
}
12+
use TestRuleFailureConditionTrait;
13+
use TestRuleSuccessConditionTrait;
2514

2615
public static function provideFailureConditionData(): \Generator
2716
{
28-
yield 'null' => [null];
29-
yield 'false' => [false];
30-
yield 'blank string' => [''];
31-
yield 'blank array' => [[]];
32-
}
17+
$exception = NotBlankException::class;
18+
$exceptionMessage = '/The "(.*)" value should not be blank, "(.*)" given./';
3319

34-
#[DataProvider('provideSuccessConditionData')]
35-
public function testNotBlankSuccessCondition(mixed $value)
36-
{
37-
$validator = Validator::notBlank();
38-
39-
$this->assertTrue($validator->validate($value));
40-
41-
$validator->assert($value, 'test');
20+
yield 'null' => [new NotBlank(), null, $exception, $exceptionMessage];
21+
yield 'false' => [new NotBlank(), false, $exception, $exceptionMessage];
22+
yield 'blank string' => [new NotBlank(), '', $exception, $exceptionMessage];
23+
yield 'blank array' => [new NotBlank(), [], $exception, $exceptionMessage];
4224
}
4325

4426
public static function provideSuccessConditionData(): \Generator
4527
{
46-
yield 'true' => [true];
47-
48-
yield 'string' => ['string'];
49-
yield 'whitespace string' => [' '];
50-
yield 'zero string' => ['0'];
28+
yield 'true' => [new NotBlank(), true];
5129

52-
yield 'array' => [['string']];
53-
yield 'blank string array' => [['']];
54-
yield 'whitespace array' => [[' ']];
55-
yield 'zero array' => [[0]];
30+
yield 'string' => [new NotBlank(), 'string'];
31+
yield 'whitespace string' => [new NotBlank(), ' '];
32+
yield 'zero string' => [new NotBlank(), '0'];
5633

57-
yield 'number' => [10];
58-
yield 'zero number' => [0];
59-
}
60-
61-
public function testNotBlankMessageArgument()
62-
{
63-
$this->expectExceptionMessage('The "test" value "" is invalid. Must not be blank.');
34+
yield 'array' => [new NotBlank(), ['string']];
35+
yield 'blank string array' => [new NotBlank(), ['']];
36+
yield 'whitespace array' => [new NotBlank(), [' ']];
37+
yield 'zero array' => [new NotBlank(), [0]];
6438

65-
Validator
66-
::notBlank(message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be blank.')
67-
->assert('', 'test');
39+
yield 'number' => [new NotBlank(), 10];
40+
yield 'zero number' => [new NotBlank(), 0];
6841
}
6942
}

0 commit comments

Comments
 (0)