MakeTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. use EasySwoole\Validate\Validate;
  4. class MakeTest extends BaseTestCase
  5. {
  6. protected $rules = [
  7. 'name' => 'required|notEmpty',
  8. 'age' => 'required|integer|between:20,30',
  9. 'weight' => 'required|max:50'
  10. ];
  11. protected $message = [
  12. 'name.required' => '名字不能为空呀!',
  13. 'age' => '年龄输入有误呀!',
  14. 'weight.max' => '体重最大不能超过50呀!'
  15. ];
  16. protected $alias = [
  17. 'name' => '名字',
  18. 'age' => '年龄',
  19. 'weight' => '体重'
  20. ];
  21. public function testValidCase()
  22. {
  23. $validate = Validate::make($this->rules);
  24. $ret = $validate->validate([
  25. 'name' => '史迪仔',
  26. 'age' => 20,
  27. 'weight' => 20
  28. ]);
  29. $this->assertTrue($ret);
  30. }
  31. public function testDefaultErrMsg()
  32. {
  33. $validate = Validate::make($this->rules);
  34. $ret = $validate->validate([
  35. 'name' => '',
  36. 'age' => 20,
  37. 'weight' => 20
  38. ]);
  39. $this->assertFalse($ret);
  40. $this->assertEquals('name不能为空', $validate->getError()->getErrorRuleMsg());
  41. $validate = Validate::make($this->rules);
  42. $ret = $validate->validate([
  43. 'name' => '史迪仔',
  44. 'age' => 10,
  45. 'weight' => 20
  46. ]);
  47. $this->assertFalse($ret);
  48. $this->assertEquals('age只能在 20 - 30 之间', $validate->getError()->getErrorRuleMsg());
  49. $validate = Validate::make($this->rules);
  50. $ret = $validate->validate([
  51. 'name' => '史迪仔',
  52. 'age' => 20,
  53. 'weight' => 70
  54. ]);
  55. $this->assertFalse($ret);
  56. $this->assertEquals("weight的值不能大于'50'", $validate->getError()->getErrorRuleMsg());
  57. }
  58. public function testAliasErrMsg()
  59. {
  60. $validate = Validate::make($this->rules, [], $this->alias);
  61. $ret = $validate->validate([
  62. 'name' => '',
  63. 'age' => 20,
  64. 'weight' => 20
  65. ]);
  66. $this->assertFalse($ret);
  67. $this->assertEquals('名字不能为空', $validate->getError()->getErrorRuleMsg());
  68. $validate = Validate::make($this->rules, [], $this->alias);
  69. $ret = $validate->validate([
  70. 'name' => '史迪仔',
  71. 'age' => 10,
  72. 'weight' => 20
  73. ]);
  74. $this->assertFalse($ret);
  75. $this->assertEquals('年龄只能在 20 - 30 之间', $validate->getError()->getErrorRuleMsg());
  76. $validate = Validate::make($this->rules, [], $this->alias);
  77. $ret = $validate->validate([
  78. 'name' => '史迪仔',
  79. 'age' => 20,
  80. 'weight' => 70
  81. ]);
  82. $this->assertFalse($ret);
  83. $this->assertEquals("体重的值不能大于'50'", $validate->getError()->getErrorRuleMsg());
  84. }
  85. public function testCustomErrMsg()
  86. {
  87. $validate = Validate::make($this->rules, $this->message);
  88. $ret = $validate->validate([
  89. 'name' => null,
  90. 'age' => 20,
  91. 'weight' => 20
  92. ]);
  93. $this->assertFalse($ret);
  94. $this->assertEquals('名字不能为空呀!', $validate->getError()->getErrorRuleMsg());
  95. $validate = Validate::make($this->rules, $this->message);
  96. $ret = $validate->validate([
  97. 'name' => '史迪仔',
  98. 'age' => 10,
  99. 'weight' => 20
  100. ]);
  101. $this->assertFalse($ret);
  102. $this->assertEquals('年龄输入有误呀!', $validate->getError()->getErrorRuleMsg());
  103. $validate = Validate::make($this->rules, $this->message);
  104. $ret = $validate->validate([
  105. 'name' => '史迪仔',
  106. 'age' => 20,
  107. 'weight' => 70
  108. ]);
  109. $this->assertFalse($ret);
  110. $this->assertEquals("体重最大不能超过50呀!", $validate->getError()->getErrorRuleMsg());
  111. }
  112. }