NotEmptyTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 非空数据测试用例
  5. * Class NotEmptyTest
  6. *
  7. * @internal
  8. */
  9. class NotEmptyTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * 不为空字符串
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('name')->notEmpty();
  21. $bool = $this->validate->validate(['name' => 'blank']);
  22. $this->assertTrue($bool);
  23. /*
  24. * 数值0
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('value')->notEmpty();
  28. $bool = $this->validate->validate(['value' => 0]);
  29. $this->assertTrue($bool);
  30. /*
  31. * 字符0
  32. */
  33. $this->freeValidate();
  34. $this->validate->addColumn('value')->notEmpty();
  35. $bool = $this->validate->validate(['value' => '0']);
  36. $this->assertTrue($bool);
  37. }
  38. /*
  39. * 默认错误信息
  40. */
  41. public function testDefaultErrorMsgCase()
  42. {
  43. /*
  44. * 空字符
  45. */
  46. $this->freeValidate();
  47. $this->validate->addColumn('name', '名字')->notEmpty();
  48. $bool = $this->validate->validate(['name' => '']);
  49. $this->assertFalse($bool);
  50. $this->assertEquals('名字不能为空', $this->validate->getError()->__toString());
  51. /*
  52. * null
  53. */
  54. $this->freeValidate();
  55. $this->validate->addColumn('name', '名字')->notEmpty();
  56. $bool = $this->validate->validate(['name' => null]);
  57. $this->assertFalse($bool);
  58. $this->assertEquals('名字不能为空', $this->validate->getError()->__toString());
  59. }
  60. /*
  61. * 自定义错误信息
  62. */
  63. public function testCustomErrorMsgCase()
  64. {
  65. /*
  66. * 空字符
  67. */
  68. $this->freeValidate();
  69. $this->validate->addColumn('name')->notEmpty('名字必填');
  70. $bool = $this->validate->validate(['name' => '']);
  71. $this->assertFalse($bool);
  72. $this->assertEquals('名字必填', $this->validate->getError()->__toString());
  73. }
  74. }