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 51869b1 commit 6be80b1
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
182 changes: 182 additions & 0 deletions tests/Validator/OptionIfParamMissTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?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\Required;
use PHPUnit\Framework\TestCase;

class OptionIfParamMissTest extends TestCase
{
function testNormal()
{
$request = new Request();
$request->withQueryParams([

]);

$num = new Param(
name:"num",
validate: [
new OptionalIfParamMiss('account'),
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 OptionalIfParamMiss('account'),
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"=>"1221",
"num"=>'asd'
]);

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

$num = new Param(
name:"num",
validate: [
new OptionalIfParamMiss('account'),
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);
}
}
36 changes: 36 additions & 0 deletions tests/Validator/OptionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ function testNormal()
$this->assertEquals(true,$ret);
}


function testEmpty()
{
$request = new Request();
$request->withQueryParams([
'num' => ""
]);

$num = new Param(
name:"num",
validate: [
new Optional(),
new Integer()
]
);
$num->parsedValue($request);


$allDefineParams = [
'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();
Expand Down

0 comments on commit 6be80b1

Please sign in to comment.