-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from XueSiLf/3.x
🚀feat: add support for DateAfterColumn and DateBeforeColumn
- Loading branch information
Showing
4 changed files
with
489 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<?php | ||
|
||
namespace EasySwoole\HttpAnnotation\Tests\Validator; | ||
|
||
use EasySwoole\Http\Request; | ||
use EasySwoole\HttpAnnotation\Attributes\Param; | ||
use EasySwoole\HttpAnnotation\Validator\DateAfterColumn; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DateAfterColumnTest extends TestCase | ||
{ | ||
/* | ||
* 合法 | ||
*/ | ||
public function testValidCase() | ||
{ | ||
// 日期格式 | ||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "20220501", | ||
"date1" => "20220430" | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(true, $rule->execute($param, $request)); | ||
|
||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "2022-05-06", | ||
"date1" => "20220430" | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(true, $rule->execute($param, $request)); | ||
|
||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "2022-05-06 00:00:00", | ||
"date1" => "20200630", | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(true, $rule->execute($param, $request)); | ||
} | ||
|
||
/* | ||
* 默认错误信息 | ||
*/ | ||
public function testDefaultErrorMsgCase() | ||
{ | ||
// 日期不符 | ||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "2022-05-08", | ||
"date1" => "20220530", | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(false, $rule->execute($param, $request)); | ||
$this->assertEquals("date must be date after date1 column", $rule->errorMsg()); | ||
|
||
// 非法参数 | ||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "xuesi", | ||
"date1" => "20220530", | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(false, $rule->execute($param, $request)); | ||
$this->assertEquals("date must be date after date1 column", $rule->errorMsg()); | ||
|
||
//字段必须是日期格式。因此传时间戳,失败 | ||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "1654765394", | ||
"date1" => "20220530", | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1"); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(false, $rule->execute($param, $request)); | ||
$this->assertEquals("date must be date after date1 column", $rule->errorMsg()); | ||
} | ||
|
||
/* | ||
* 自定义错误信息 | ||
*/ | ||
public function testCustomErrorMsgCase() | ||
{ | ||
$request = new Request(); | ||
$request->withQueryParams([ | ||
"date" => "xuesi", | ||
"date1" => "20220530", | ||
]); | ||
|
||
$param = new Param(name: "date"); | ||
$param->parsedValue($request); | ||
|
||
$date1 = new Param(name: "date1"); | ||
$date1->parsedValue($request); | ||
|
||
$rule = new DateAfterColumn(compare: "date1", errorMsg: '日期不合法'); | ||
|
||
$rule->allCheckParams([ | ||
"date" => $param, | ||
"date1" => $date1 | ||
]); | ||
|
||
$this->assertEquals(false, $rule->execute($param, $request)); | ||
$this->assertEquals("日期不合法", $rule->errorMsg()); | ||
} | ||
} |
Oops, something went wrong.