9
9
namespace Inhere \ValidateTest \Filter ;
10
10
11
11
use Inhere \Validate \Filter \Filtration ;
12
+ use InvalidArgumentException ;
12
13
use PHPUnit \Framework \TestCase ;
14
+ use function trim ;
13
15
14
16
/**
15
17
* Class FiltrationTest
@@ -48,21 +50,28 @@ public function testUserFilters(): void
48
50
$ fl ->addFilters ([
49
51
'name1 ' => function () {
50
52
},
51
- 'name2 ' => function () {
53
+ 'newTrim ' => function ($ val ) {
54
+ return trim ($ val );
52
55
},
53
56
'' => function () {
54
57
},
55
58
]);
56
59
57
60
$ this ->assertCount (2 , $ fl ->getFilters ());
58
61
59
- $ this ->assertNotEmpty ($ fl ->getFilter ('name2 ' ));
62
+ $ this ->assertNotEmpty ($ fl ->getFilter ('newTrim ' ));
60
63
$ this ->assertEmpty ($ fl ->getFilter ('name3 ' ));
61
64
62
65
$ fl ->addFilter ('new1 ' , function () {
63
66
});
64
67
$ this ->assertNotEmpty ($ fl ->getFilter ('new1 ' ));
65
68
69
+ // use user filter
70
+ $ filtered = $ fl ->filtering ([
71
+ ['name ' , 'newTrim ' ]
72
+ ]);
73
+ $ this ->assertSame ('tom ' , $ filtered ['name ' ]);
74
+
66
75
$ fl ->delFilter ('name1 ' );
67
76
$ this ->assertEmpty ($ fl ->getFilter ('name1 ' ));
68
77
@@ -72,7 +81,6 @@ public function testUserFilters(): void
72
81
73
82
public function testFiltering (): void
74
83
{
75
-
76
84
$ rules = [
77
85
['name ' , 'string|trim ' ],
78
86
['status ' , 'trim|int ' ],
@@ -104,4 +112,29 @@ public function testFiltering(): void
104
112
$ this ->assertEmpty ($ fl ->getData ());
105
113
$ this ->assertEmpty ($ fl ->getRules ());
106
114
}
115
+
116
+ public function testUseClosure (): void
117
+ {
118
+ $ fl = Filtration::make ($ this ->data );
119
+ $ fl ->setRules ([
120
+ ['name ' , function ($ val ) {
121
+ $ this ->assertSame (' tom ' , $ val );
122
+ return trim ($ val );
123
+ }]
124
+ ]);
125
+
126
+ $ cleaned = $ fl ->filtering ();
127
+ $ this ->assertSame ('tom ' , $ cleaned ['name ' ]);
128
+ }
129
+
130
+ public function testCallNotExist (): void
131
+ {
132
+ $ fl = Filtration::make ($ this ->data );
133
+ $ fl ->setRules ([
134
+ ['name ' , 'not-exist-filter ' ]
135
+ ]);
136
+
137
+ $ this ->expectException (InvalidArgumentException::class);
138
+ $ fl ->filtering ();
139
+ }
107
140
}
0 commit comments