Skip to content

Commit e0bfd77

Browse files
committed
Added truthy rule
1 parent c88db7f commit e0bfd77

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Validation\Logic;
4+
5+
use Nuxtifyts\PhpDto\Exceptions\LogicalRuleException;
6+
use Nuxtifyts\PhpDto\Validation\Contracts\RuleEvaluator;
7+
use Override;
8+
9+
class TruthyRule extends LogicalRule
10+
{
11+
/**
12+
* @throws LogicalRuleException
13+
*/
14+
#[Override]
15+
public function addRule(RuleEvaluator $rule): never
16+
{
17+
throw LogicalRuleException::unableToCreateRule('TruthyRule cannot have nested rules');
18+
}
19+
20+
public function evaluate(mixed $value): bool
21+
{
22+
return true;
23+
}
24+
25+
public function validationMessageTree(): array
26+
{
27+
return [];
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Validation\Logical;
4+
5+
use Nuxtifyts\PhpDto\Exceptions\LogicalRuleException;
6+
use Nuxtifyts\PhpDto\Validation\Contracts\RuleEvaluator;
7+
use Nuxtifyts\PhpDto\Validation\Logic\LogicalRule;
8+
use Nuxtifyts\PhpDto\Validation\Logic\TruthyRule;
9+
use Nuxtifyts\PhpDto\Validation\Rules\StringRule;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\UsesClass;
12+
use Throwable;
13+
14+
#[CoversClass(TruthyRule::class)]
15+
#[CoversClass(LogicalRuleException::class)]
16+
#[UsesClass(StringRule::class)]
17+
final class TruthyRuleTest extends LogicalRuleTestCase
18+
{
19+
/**
20+
* @return array<string, array{
21+
* logicalRuleClassString: class-string<LogicalRule>,
22+
* ruleEvaluators: list<RuleEvaluator>,
23+
* expectedCreateException: ?class-string<LogicalRuleException>,
24+
* valueToBeEvaluated: mixed,
25+
* expectedResult: bool,
26+
* expectedValidationMessageTree: array<string, mixed>
27+
* }>
28+
*
29+
* @throws Throwable
30+
*/
31+
public static function data_provider(): array
32+
{
33+
return [
34+
'Will always return true when validating' => [
35+
'logicalRuleClassString' => TruthyRule::class,
36+
'ruleEvaluators' => [],
37+
'expectedCreateException' => null,
38+
'valueToBeEvaluated' => 'string',
39+
'expectedResult' => true,
40+
'expectedValidationMessageTree' => []
41+
],
42+
'Will throw an exception if trying to add a nested rule' => [
43+
'logicalRuleClassString' => TruthyRule::class,
44+
'ruleEvaluators' => [
45+
StringRule::make(),
46+
],
47+
'expectedCreateException' => LogicalRuleException::class,
48+
'valueToBeEvaluated' => 1234,
49+
'expectedResult' => true,
50+
'expectedValidationMessageTree' => []
51+
]
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)