DateAfterTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class DateAfterTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. $this->freeValidate();
  12. $this->validate->addColumn('data1')->dateAfter('2018-08-08');
  13. $this->validate->addColumn('data2')->dateAfter('20180808');
  14. $this->validate->addColumn('data3')->dateAfter('2018-08-07 23:59:59');
  15. $validateResult = $this->validate->validate([
  16. 'data1' => '2018-08-09',
  17. 'data2' => '20180809',
  18. 'data3' => '2018-08-08 00:00:00',
  19. ]);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 日期不符
  26. $this->freeValidate();
  27. $this->validate->addColumn('data')->dateAfter('2018-08-08');
  28. $validateResult = $this->validate->validate(['data' => '2018-08-06']);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals("data必须在日期 '2018-08-08' 之后", $this->validate->getError()->__toString());
  31. // 非法参数
  32. $this->freeValidate();
  33. $this->validate->addColumn('data')->dateAfter('aaa');
  34. $validateResult = $this->validate->validate(['data' => '2018-08-06']);
  35. $this->assertFalse($validateResult);
  36. $this->assertEquals("data必须在日期 'aaa' 之后", $this->validate->getError()->__toString());
  37. }
  38. // 自定义错误信息断言
  39. public function testCustomErrorMsgCase()
  40. {
  41. // 日期相等
  42. $this->freeValidate();
  43. $this->validate->addColumn('data')->dateAfter('2018-08-08', '日期不合法');
  44. $validateResult = $this->validate->validate(['data' => '2018-08-08']);
  45. $this->assertFalse($validateResult);
  46. $this->assertEquals('日期不合法', $this->validate->getError()->__toString());
  47. }
  48. }