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

Commit 729ecff

Browse files
committed
chore(tests): added test for unexpected values
1 parent 2dc872e commit 729ecff

6 files changed

Lines changed: 93 additions & 51 deletions

File tree

src/Rule/GreaterThan.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
77
use Symfony\Component\OptionsResolver\OptionsResolver;
88

99
class GreaterThan extends AbstractRule implements RuleInterface
1010
{
11-
use AssertComparableTrait;
11+
use AssertIsComparableTrait;
1212

1313
private array $options;
1414

@@ -29,7 +29,7 @@ public function __construct(private readonly mixed $constraint, array $options =
2929
public function assert(mixed $value, string $name): void
3030
{
3131
// Assert if constraint and value can be compared
32-
$this->assertComparable($this->constraint, $value, GreaterThanException::class);
32+
$this->assertIsComparable($this->constraint, $value);
3333

3434
if (!($value > $this->constraint)) {
3535
throw new GreaterThanException(

src/Rule/LessThan.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
77
use Symfony\Component\OptionsResolver\OptionsResolver;
88

99
class LessThan extends AbstractRule implements RuleInterface
1010
{
11-
use AssertComparableTrait;
11+
use AssertIsComparableTrait;
1212

1313
private array $options;
1414

@@ -29,7 +29,7 @@ public function __construct(private readonly mixed $constraint, array $options =
2929
public function assert(mixed $value, string $name): void
3030
{
3131
// Assert if constraint and value can be compared
32-
$this->assertComparable($this->constraint, $value, LessThanException::class);
32+
$this->assertIsComparable($this->constraint, $value);
3333

3434
if (!($value < $this->constraint)) {
3535
throw new LessThanException(
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule\Util;
44

5-
trait AssertComparableTrait
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
7+
trait AssertIsComparableTrait
68
{
7-
private function assertComparable(mixed $value1, mixed $value2, string $exception): bool
9+
private function assertIsComparable(mixed $value1, mixed $value2): bool
810
{
911
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
1012
return true;
@@ -18,7 +20,7 @@ private function assertComparable(mixed $value1, mixed $value2, string $exceptio
1820
return true;
1921
}
2022

21-
throw new $exception(
23+
throw new UnexpectedValueException(
2224
\sprintf(
2325
'Cannot compare a type "%s" with a type "%s"',
2426
get_debug_type($value1),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test\Util;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
8+
9+
trait TestRuleUnexpectedValueTrait
10+
{
11+
public static abstract function provideRuleUnexpectedValueData(): \Generator;
12+
13+
#[DataProvider('provideRuleUnexpectedValueData')]
14+
public function testRuleAssertUnexpectedValue(RuleInterface $rule, mixed $value, string $expectedExceptionMessage): void
15+
{
16+
$this->expectException(UnexpectedValueException::class);
17+
$this->expectExceptionMessageMatches($expectedExceptionMessage);
18+
$rule->assert($value, 'test');
19+
}
20+
21+
#[DataProvider('provideRuleUnexpectedValueData')]
22+
public function testRuleValidateUnexpectedValue(RuleInterface $rule, mixed $value, string $expectedExceptionMessage): void
23+
{
24+
$this->expectException(UnexpectedValueException::class);
25+
$this->expectExceptionMessageMatches($expectedExceptionMessage);
26+
$rule->validate($value);
27+
}
28+
}

tests/GreaterThanTest.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,43 @@
77
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
88
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
1011

1112
class GreaterThanTest extends AbstractTest
1213
{
14+
use TestRuleUnexpectedValueTrait;
1315
use TestRuleFailureConditionTrait;
1416
use TestRuleSuccessConditionTrait;
1517
use TestRuleMessageOptionTrait;
1618

19+
public static function provideRuleUnexpectedValueData(): \Generator
20+
{
21+
$exceptionMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
22+
23+
yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $exceptionMessage];
24+
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $exceptionMessage];
25+
yield 'datetime constraint with string value' => [new GreaterThan(new \DateTime()), 'a', $exceptionMessage];
26+
yield 'int constraint with string value' => [new GreaterThan(10), 'a', $exceptionMessage];
27+
yield 'float constraint with string value' => [new GreaterThan(1.0), 'a', $exceptionMessage];
28+
yield 'array constraint' => [new GreaterThan([10]), 10, $exceptionMessage];
29+
yield 'null constraint' => [new GreaterThan(null), 10, $exceptionMessage];
30+
}
31+
1732
public static function provideRuleFailureConditionData(): \Generator
1833
{
1934
$exception = GreaterThanException::class;
20-
$exceptionMessageInvalid = '/Cannot compare a type "(.*)" with a type "(.*)"/';
21-
$exceptionMessageFailure = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
22-
23-
yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $exception, $exceptionMessageInvalid];
24-
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $exception, $exceptionMessageInvalid];
25-
yield 'datetime constraint with string value' => [new GreaterThan(new \DateTime()), 'a', $exception, $exceptionMessageInvalid];
26-
yield 'int constraint with string value' => [new GreaterThan(10), 'a', $exception, $exceptionMessageInvalid];
27-
yield 'float constraint with string value' => [new GreaterThan(1.0), 'a', $exception, $exceptionMessageInvalid];
28-
yield 'array constraint' => [new GreaterThan([10]), 10, $exception, $exceptionMessageInvalid];
29-
yield 'null constraint' => [new GreaterThan(null), 10, $exception, $exceptionMessageInvalid];
30-
31-
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $exceptionMessageFailure];
32-
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessageFailure];
33-
yield 'int' => [new GreaterThan(10), 1, $exception, $exceptionMessageFailure];
34-
yield 'same int' => [new GreaterThan(10), 10, $exception, $exceptionMessageFailure];
35-
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $exceptionMessageFailure];
36-
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $exceptionMessageFailure];
37-
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $exceptionMessageFailure];
38-
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $exceptionMessageFailure];
39-
yield 'string' => [new GreaterThan('z'), 'a', $exception, $exceptionMessageFailure];
40-
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $exceptionMessageFailure];
35+
$exceptionMessage = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
36+
37+
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $exceptionMessage];
38+
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessage];
39+
yield 'int' => [new GreaterThan(10), 1, $exception, $exceptionMessage];
40+
yield 'same int' => [new GreaterThan(10), 10, $exception, $exceptionMessage];
41+
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $exceptionMessage];
42+
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $exceptionMessage];
43+
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $exceptionMessage];
44+
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $exceptionMessage];
45+
yield 'string' => [new GreaterThan('z'), 'a', $exception, $exceptionMessage];
46+
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $exceptionMessage];
4147
}
4248

4349
public static function provideRuleSuccessConditionData(): \Generator

tests/LessThanTest.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,43 @@
77
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
88
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
1011

1112
class LessThanTest extends AbstractTest
1213
{
14+
use TestRuleUnexpectedValueTrait;
1315
use TestRuleFailureConditionTrait;
1416
use TestRuleSuccessConditionTrait;
1517
use TestRuleMessageOptionTrait;
1618

19+
public static function provideRuleUnexpectedValueData(): \Generator
20+
{
21+
$exceptionMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
22+
23+
yield 'datetime constraint with int value' => [new LessThan(new \DateTime()), 10, $exceptionMessage];
24+
yield 'datetime constraint with float value' => [new LessThan(new \DateTime()), 1.0, $exceptionMessage];
25+
yield 'datetime constraint with string value' => [new LessThan(new \DateTime()), 'a', $exceptionMessage];
26+
yield 'int constraint with string value' => [new LessThan(10), 'a', $exceptionMessage];
27+
yield 'float constraint with string value' => [new LessThan(1.0), 'a', $exceptionMessage];
28+
yield 'array constraint' => [new LessThan([10]), 10, $exceptionMessage];
29+
yield 'null constraint' => [new LessThan(null), 10, $exceptionMessage];
30+
}
31+
1732
public static function provideRuleFailureConditionData(): \Generator
1833
{
1934
$exception = LessThanException::class;
20-
$exceptionMessageInvalid = '/Cannot compare a type "(.*)" with a type "(.*)"/';
21-
$exceptionMessageFailure = '/The "(.*)" value should be less than "(.*)", "(.*)" given./';
22-
23-
yield 'datetime constraint with int value' => [new LessThan(new \DateTime()), 10, $exception, $exceptionMessageInvalid];
24-
yield 'datetime constraint with float value' => [new LessThan(new \DateTime()), 1.0, $exception, $exceptionMessageInvalid];
25-
yield 'datetime constraint with string value' => [new LessThan(new \DateTime()), 'a', $exception, $exceptionMessageInvalid];
26-
yield 'int constraint with string value' => [new LessThan(10), 'a', $exception, $exceptionMessageInvalid];
27-
yield 'float constraint with string value' => [new LessThan(1.0), 'a', $exception, $exceptionMessageInvalid];
28-
yield 'array constraint' => [new LessThan([10]), 10, $exception, $exceptionMessageInvalid];
29-
yield 'null constraint' => [new LessThan(null), 10, $exception, $exceptionMessageInvalid];
30-
31-
yield 'datetime' => [new LessThan(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $exceptionMessageFailure];
32-
yield 'same datetime' => [new LessThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessageFailure];
33-
yield 'int' => [new LessThan(10), 20, $exception, $exceptionMessageFailure];
34-
yield 'same int' => [new LessThan(10), 10, $exception, $exceptionMessageFailure];
35-
yield 'float' => [new LessThan(10.0), 20.0, $exception, $exceptionMessageFailure];
36-
yield 'same float' => [new LessThan(10.0), 10.0, $exception, $exceptionMessageFailure];
37-
yield 'int with float' => [new LessThan(10), 20.0, $exception, $exceptionMessageFailure];
38-
yield 'same int with float' => [new LessThan(10), 10.0, $exception, $exceptionMessageFailure];
39-
yield 'string' => [new LessThan('a'), 'z', $exception, $exceptionMessageFailure];
40-
yield 'same string' => [new LessThan('a'), 'a', $exception, $exceptionMessageFailure];
35+
$exceptionMessage = '/The "(.*)" value should be less than "(.*)", "(.*)" given./';
36+
37+
yield 'datetime' => [new LessThan(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $exceptionMessage];
38+
yield 'same datetime' => [new LessThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessage];
39+
yield 'int' => [new LessThan(10), 20, $exception, $exceptionMessage];
40+
yield 'same int' => [new LessThan(10), 10, $exception, $exceptionMessage];
41+
yield 'float' => [new LessThan(10.0), 20.0, $exception, $exceptionMessage];
42+
yield 'same float' => [new LessThan(10.0), 10.0, $exception, $exceptionMessage];
43+
yield 'int with float' => [new LessThan(10), 20.0, $exception, $exceptionMessage];
44+
yield 'same int with float' => [new LessThan(10), 10.0, $exception, $exceptionMessage];
45+
yield 'string' => [new LessThan('a'), 'z', $exception, $exceptionMessage];
46+
yield 'same string' => [new LessThan('a'), 'a', $exception, $exceptionMessage];
4147
}
4248

4349
public static function provideRuleSuccessConditionData(): \Generator

0 commit comments

Comments
 (0)