1+ <?php
2+
3+ namespace ProgrammatorDev \Validator \Test ;
4+
5+ use ProgrammatorDev \Validator \Exception \CountException ;
6+ use ProgrammatorDev \Validator \Rule \Count ;
7+ use ProgrammatorDev \Validator \Test \Util \TestRuleFailureConditionTrait ;
8+ use ProgrammatorDev \Validator \Test \Util \TestRuleMessageOptionTrait ;
9+ use ProgrammatorDev \Validator \Test \Util \TestRuleSuccessConditionTrait ;
10+ use ProgrammatorDev \Validator \Test \Util \TestRuleUnexpectedValueTrait ;
11+
12+ class CountTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ missingOptionsMessage = '/At least one of the options "min" or "max" must be given./ ' ;
22+ $ invalidTypeMessage = '/Expected value of type "array|\Countable", "(.*)" given./ ' ;
23+
24+ yield 'missing options ' => [new Count (), [1 , 2 , 3 ], $ missingOptionsMessage ];
25+ yield 'invalid type value ' => [new Count (min: 5 , max: 10 ), 1 , $ invalidTypeMessage ];
26+ }
27+
28+ public static function provideRuleFailureConditionData (): \Generator
29+ {
30+ $ value = [1 , 2 , 3 , 4 , 5 ];
31+ $ exception = CountException::class;
32+ $ minMessage = '/The (.*) value should contain (.*) elements or more, (.*) elements given./ ' ;
33+ $ maxMessage = '/The (.*) value should contain (.*) elements or less, (.*) elements given./ ' ;
34+ $ exactMessage = '/The (.*) value should contain exactly (.*) elements, (.*) elements given./ ' ;
35+
36+ yield 'min constraint ' => [new Count (min: 10 ), $ value , $ exception , $ minMessage ];
37+ yield 'max constraint ' => [new Count (max: 2 ), $ value , $ exception , $ maxMessage ];
38+ yield 'min and max constraint ' => [new Count (min: 10 , max: 20 ), $ value , $ exception , $ minMessage ];
39+ yield 'exact constraint ' => [new Count (min: 2 , max: 2 ), $ value , $ exception , $ exactMessage ];
40+ }
41+
42+ public static function provideRuleSuccessConditionData (): \Generator
43+ {
44+ $ value = [1 , 2 , 3 , 4 , 5 ];
45+
46+ yield 'min constraint ' => [new Count (min: 5 ), $ value ];
47+ yield 'max constraint ' => [new Count (max: 5 ), $ value ];
48+ yield 'min and max constraint ' => [new Count (min: 4 , max: 6 ), $ value ];
49+ yield 'exact constraint ' => [new Count (min: 5 , max: 5 ), $ value ];
50+ }
51+
52+ public static function provideRuleMessageOptionData (): \Generator
53+ {
54+ $ value = [1 , 2 , 3 , 4 , 5 ];
55+
56+ yield 'min message ' => [
57+ new Count (
58+ min: 10 ,
59+ minMessage: 'The {{ name }} value should have at least {{ min }} elements. '
60+ ),
61+ $ value ,
62+ 'The test value should have at least 10 elements. '
63+ ];
64+ yield 'max message ' => [
65+ new Count (
66+ max: 2 ,
67+ maxMessage: 'The {{ name }} value should have at most {{ max }} elements. '
68+ ),
69+ $ value ,
70+ 'The test value should have at most 2 elements. '
71+ ];
72+ yield 'exact message ' => [
73+ new Count (
74+ min: 2 ,
75+ max: 2 ,
76+ exactMessage: 'The {{ name }} value should have exactly {{ min }} elements. '
77+ ),
78+ $ value ,
79+ 'The test value should have exactly 2 elements. '
80+ ];
81+ }
82+ }
0 commit comments