diff --git a/tests/Validator/OptionIfParamNotInArrayTest.php b/tests/Validator/OptionIfParamNotInArrayTest.php new file mode 100644 index 0000000..bdd1b84 --- /dev/null +++ b/tests/Validator/OptionIfParamNotInArrayTest.php @@ -0,0 +1,193 @@ +withQueryParams([ + "account"=>"normal", + ]); + + $num = new Param( + name:"num", + validate: [ + new OptionalIfParamValNoInArray("account",[ + 'admin' + ]), + new Integer() + ] + ); + $num->parsedValue($request); + + $account = new Param( + name: "account" + ); + $account->parsedValue($request); + + $allDefineParams = [ + 'account'=>$account, + 'num'=>$num, + ]; + + $ret = true; + $rules = $num->validate; + /** @var AbstractValidator $rule */ + foreach ($rules as $rule){ + $rule->allCheckParams($allDefineParams); + $ret = $rule->execute($num,$request); + if(!$ret){ + break; + } + } + + $this->assertEquals(true,$ret); + } + + function testEmpty() + { + $request = new Request(); + $request->withQueryParams([ + "account"=>"", + ]); + + $num = new Param( + name:"num", + validate: [ + new OptionalIfParamValNoInArray("account",[ + 'admin' + ]), + new Integer() + ] + ); + $num->parsedValue($request); + + $account = new Param( + name: "account" + ); + $account->parsedValue($request); + + $allDefineParams = [ + 'account'=>$account, + 'num'=>$num, + ]; + + $ret = true; + $rules = $num->validate; + /** @var AbstractValidator $rule */ + foreach ($rules as $rule){ + $rule->allCheckParams($allDefineParams); + $ret = $rule->execute($num,$request); + if(!$ret){ + break; + } + } + + $this->assertEquals(true,$ret); + } + + + function testFail() + { + $request = new Request(); + $request->withQueryParams([ + "account"=>"admin", + ]); + + $num = new Param( + name:"num", + validate: [ + new OptionalIfParamValNoInArray("account",[ + 'admin' + ]), + new Integer() + ] + ); + $num->parsedValue($request); + + $account = new Param( + name: "account" + ); + $account->parsedValue($request); + + $allDefineParams = [ + 'account'=>$account, + 'num'=>$num, + ]; + + $ret = true; + $rules = $num->validate; + /** @var AbstractValidator $rule */ + foreach ($rules as $rule){ + $rule->allCheckParams($allDefineParams); + $ret = $rule->execute($num,$request); + if(!$ret){ + break; + } + } + + $this->assertEquals(false,$ret); + } + + + function testSuccess() + { + $request = new Request(); + $request->withQueryParams([ + "account"=>"admin", + "num"=>1 + ]); + + $num = new Param( + name:"num", + validate: [ + new OptionalIfParamValNoInArray("account",[ + 'admin' + ]), + new Integer() + ] + ); + $num->parsedValue($request); + + $account = new Param( + name: "account" + ); + $account->parsedValue($request); + + $allDefineParams = [ + 'account'=>$account, + 'num'=>$num, + ]; + + $ret = true; + $rules = $num->validate; + /** @var AbstractValidator $rule */ + foreach ($rules as $rule){ + $rule->allCheckParams($allDefineParams); + $ret = $rule->execute($num,$request); + if(!$ret){ + break; + } + } + + $this->assertEquals(true,$ret); + } + + +} \ No newline at end of file