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

Commit bcbca3c

Browse files
committed
chore: refactored GreaterThan rule and tests
1 parent ff26b46 commit bcbca3c

3 files changed

Lines changed: 57 additions & 104 deletions

File tree

src/Rule/GreaterThan.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparisonTrait;
67

78
class GreaterThan extends AbstractRule implements RuleInterface
89
{
10+
use AssertComparisonTrait;
11+
912
private string $message;
1013

1114
public function __construct(
1215
private readonly mixed $constraint,
13-
string $message = null)
16+
string $message = null
17+
)
1418
{
1519
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
1620
}
@@ -20,15 +24,8 @@ public function __construct(
2024
*/
2125
public function assert(mixed $value, string $name): void
2226
{
23-
if (!$this->canBeCompared($this->constraint, $value)) {
24-
throw new \LogicException(
25-
\sprintf(
26-
'Cannot compare a constraint type "%s" with a value type "%s"',
27-
get_debug_type($this->constraint),
28-
get_debug_type($value)
29-
)
30-
);
31-
}
27+
// Assert if constraint and value can be compared
28+
$this->assertComparison($this->constraint, $value, GreaterThanException::class);
3229

3330
if (!($value > $this->constraint)) {
3431
throw new GreaterThanException(
@@ -41,21 +38,4 @@ public function assert(mixed $value, string $name): void
4138
);
4239
}
4340
}
44-
45-
protected function canBeCompared(mixed $value1, mixed $value2): bool
46-
{
47-
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
48-
return true;
49-
}
50-
51-
if (\is_numeric($value1) && \is_numeric($value2)) {
52-
return true;
53-
}
54-
55-
if (\is_string($value1) && \is_string($value2)) {
56-
return true;
57-
}
58-
59-
return false;
60-
}
6141
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule\Util;
4+
5+
trait AssertComparisonTrait
6+
{
7+
private function assertComparison(mixed $value1, mixed $value2, string $exception): bool
8+
{
9+
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
10+
return true;
11+
}
12+
13+
if (\is_numeric($value1) && \is_numeric($value2)) {
14+
return true;
15+
}
16+
17+
if (\is_string($value1) && \is_string($value2)) {
18+
return true;
19+
}
20+
21+
throw new $exception(
22+
\sprintf(
23+
'Cannot compare a type "%s" with a type "%s"',
24+
get_debug_type($value1),
25+
get_debug_type($value2)
26+
)
27+
);
28+
}
29+
}

tests/GreaterThanTest.php

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,40 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
44

5-
use PHPUnit\Framework\Attributes\DataProvider;
65
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
76
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
87
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
98
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10-
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
119

1210
class GreaterThanTest extends AbstractTest
1311
{
1412
use TestRuleFailureConditionTrait;
1513
use TestRuleSuccessConditionTrait;
1614

17-
#[DataProvider('provideInvalidConditionData')]
18-
public function testGreaterThanValidateInvalidCondition(mixed $constraint, mixed $value)
19-
{
20-
$this->expectException(\LogicException::class);
21-
$this->expectExceptionMessage(
22-
\sprintf(
23-
'Cannot compare a constraint type "%s" with a value type "%s"',
24-
get_debug_type($constraint),
25-
get_debug_type($value)
26-
)
27-
);
28-
29-
Validator::greaterThan($constraint)->validate($value);
30-
}
31-
32-
#[DataProvider('provideInvalidConditionData')]
33-
public function testGreaterThanAssertInvalidCondition(mixed $constraint, mixed $value)
34-
{
35-
$this->expectException(\LogicException::class);
36-
$this->expectExceptionMessage(
37-
\sprintf(
38-
'Cannot compare a constraint type "%s" with a value type "%s"',
39-
get_debug_type($constraint),
40-
get_debug_type($value)
41-
)
42-
);
43-
44-
Validator::greaterThan($constraint)->assert($value, 'test');
45-
}
46-
47-
public static function provideInvalidConditionData(): \Generator
48-
{
49-
yield 'datetime constraint with int value' => [new \DateTime(), 10];
50-
yield 'datetime constraint with float value' => [new \DateTime(), 1.0];
51-
yield 'datetime constraint with string value' => [new \DateTime(), 'a'];
52-
yield 'int constraint with string value' => [10, 'a'];
53-
yield 'float constraint with string value' => [1.0, 'a'];
54-
yield 'array constraint' => [[10], 10];
55-
yield 'null constraint' => [null, 10];
56-
}
57-
5815
public static function provideFailureConditionData(): \Generator
5916
{
6017
$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];
18+
$exceptionMessageInvalid = '/Cannot compare a type "(.*)" with a type "(.*)"/';
19+
$exceptionMessageFailure = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
20+
21+
yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $exception, $exceptionMessageInvalid];
22+
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $exception, $exceptionMessageInvalid];
23+
yield 'datetime constraint with string value' => [new GreaterThan(new \DateTime()), 'a', $exception, $exceptionMessageInvalid];
24+
yield 'int constraint with string value' => [new GreaterThan(10), 'a', $exception, $exceptionMessageInvalid];
25+
yield 'float constraint with string value' => [new GreaterThan(1.0), 'a', $exception, $exceptionMessageInvalid];
26+
yield 'array constraint' => [new GreaterThan([10]), 10, $exception, $exceptionMessageInvalid];
27+
yield 'null constraint' => [new GreaterThan(null), 10, $exception, $exceptionMessageInvalid];
28+
29+
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $exceptionMessageFailure];
30+
yield 'same datetime' => [new GreaterThan(new \DateTime('2000-01-01')), new \DateTime('2000-01-01'), $exception, $exceptionMessageFailure];
31+
yield 'int' => [new GreaterThan(10), 1, $exception, $exceptionMessageFailure];
32+
yield 'same int' => [new GreaterThan(10), 10, $exception, $exceptionMessageFailure];
33+
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $exceptionMessageFailure];
34+
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $exceptionMessageFailure];
35+
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $exceptionMessageFailure];
36+
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $exceptionMessageFailure];
37+
yield 'string' => [new GreaterThan('z'), 'a', $exception, $exceptionMessageFailure];
38+
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $exceptionMessageFailure];
8339
}
8440

8541
public static function provideSuccessConditionData(): \Generator
@@ -90,16 +46,4 @@ public static function provideSuccessConditionData(): \Generator
9046
yield 'int with float' => [new GreaterThan(10), 20.0];
9147
yield 'string' => [new GreaterThan('a'), 'z'];
9248
}
93-
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-
// }
10549
}

0 commit comments

Comments
 (0)