Skip to content

Commit

Permalink
OptionalTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Mar 22, 2023
1 parent 6be80b1 commit 58cdf2e
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions tests/Validator/OptionIfParamInArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace EasySwoole\HttpAnnotation\Tests\Validator;

use EasySwoole\Http\Request;
use EasySwoole\HttpAnnotation\Attributes\Param;
use EasySwoole\HttpAnnotation\Exception\ValidateFail;
use EasySwoole\HttpAnnotation\Validator\AbstractInterface\AbstractValidator;
use EasySwoole\HttpAnnotation\Validator\DifferentWithColumn;
use EasySwoole\HttpAnnotation\Validator\Integer;
use EasySwoole\HttpAnnotation\Validator\Optional;
use EasySwoole\HttpAnnotation\Validator\OptionalIfParamMiss;
use EasySwoole\HttpAnnotation\Validator\OptionalIfParamSet;
use EasySwoole\HttpAnnotation\Validator\OptionalIfParamValInArray;
use EasySwoole\HttpAnnotation\Validator\Required;
use PHPUnit\Framework\TestCase;

class OptionIfParamInArrayTest extends TestCase
{
function testNormal()
{
$request = new Request();
$request->withQueryParams([
"account"=>"admin",
]);

$num = new Param(
name:"num",
validate: [
new OptionalIfParamValInArray("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 OptionalIfParamValInArray("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 testFail()
{
$request = new Request();
$request->withQueryParams([
"account"=>"normalUser",
]);

$num = new Param(
name:"num",
validate: [
new OptionalIfParamValInArray("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"=>"normal",
"num"=>111
]);

$num = new Param(
name:"num",
validate: [
new OptionalIfParamValInArray("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);
}


}

0 comments on commit 58cdf2e

Please sign in to comment.