99namespace Inhere \ValidateTest \Filter ;
1010
1111use Inhere \Validate \Filter \Filtration ;
12+ use InvalidArgumentException ;
1213use PHPUnit \Framework \TestCase ;
14+ use function trim ;
1315
1416/**
1517 * Class FiltrationTest
@@ -48,21 +50,28 @@ public function testUserFilters(): void
4850 $ fl ->addFilters ([
4951 'name1 ' => function () {
5052 },
51- 'name2 ' => function () {
53+ 'newTrim ' => function ($ val ) {
54+ return trim ($ val );
5255 },
5356 '' => function () {
5457 },
5558 ]);
5659
5760 $ this ->assertCount (2 , $ fl ->getFilters ());
5861
59- $ this ->assertNotEmpty ($ fl ->getFilter ('name2 ' ));
62+ $ this ->assertNotEmpty ($ fl ->getFilter ('newTrim ' ));
6063 $ this ->assertEmpty ($ fl ->getFilter ('name3 ' ));
6164
6265 $ fl ->addFilter ('new1 ' , function () {
6366 });
6467 $ this ->assertNotEmpty ($ fl ->getFilter ('new1 ' ));
6568
69+ // use user filter
70+ $ filtered = $ fl ->filtering ([
71+ ['name ' , 'newTrim ' ]
72+ ]);
73+ $ this ->assertSame ('tom ' , $ filtered ['name ' ]);
74+
6675 $ fl ->delFilter ('name1 ' );
6776 $ this ->assertEmpty ($ fl ->getFilter ('name1 ' ));
6877
@@ -72,7 +81,6 @@ public function testUserFilters(): void
7281
7382 public function testFiltering (): void
7483 {
75-
7684 $ rules = [
7785 ['name ' , 'string|trim ' ],
7886 ['status ' , 'trim|int ' ],
@@ -104,4 +112,29 @@ public function testFiltering(): void
104112 $ this ->assertEmpty ($ fl ->getData ());
105113 $ this ->assertEmpty ($ fl ->getRules ());
106114 }
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+ }
107140}
0 commit comments