WildcardTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. use EasySwoole\Validate\Validate;
  4. class WildcardTest extends BaseTestCase
  5. {
  6. public function testLeft()
  7. {
  8. $this->freeValidate();
  9. $this->validate->addColumn('*.a')->required()->notEmpty()->between(1, 10);
  10. $this->assertFalse($this->validate->validate([
  11. 'a' => ['a' => 2],
  12. 'b' => ['a' => 11]
  13. ]));
  14. $this->assertEquals('*.a只能在 1 - 10 之间', $this->validate->getError()->getErrorRuleMsg());
  15. $this->freeValidate();
  16. $this->validate->addColumn('*.a')->required()->notEmpty()->between(1, 10);
  17. $this->assertTrue($this->validate->validate([
  18. 'a' => ['a' => 2],
  19. 'b' => ['a' => 9]
  20. ]));
  21. $validate = Validate::make([
  22. '*.a' => 'required|notEmpty|between:1,10'
  23. ]);
  24. $this->assertFalse($validate->validate([
  25. 'a' => ['a' => 2],
  26. 'b' => ['a' => 11]
  27. ]));
  28. $this->assertEquals('*.a只能在 1 - 10 之间', $validate->getError()->getErrorRuleMsg());
  29. }
  30. public function testMiddle()
  31. {
  32. $this->freeValidate();
  33. $this->validate->addColumn('a.*.a')->required()->notEmpty()->between(1, 10);
  34. $this->assertFalse($this->validate->validate([
  35. 'a' => ['a' => ['a' => 0]],
  36. 'b' => ['a' => 11]
  37. ]));
  38. $this->assertEquals('a.*.a只能在 1 - 10 之间', $this->validate->getError()->getErrorRuleMsg());
  39. $this->freeValidate();
  40. $this->validate->addColumn('a.*.a')->required()->notEmpty()->between(1, 10);
  41. $this->assertTrue($this->validate->validate([
  42. 'a' => ['a' => ['a' => 1]],
  43. 'b' => ['a' => 11]
  44. ]));
  45. $validate = Validate::make([
  46. 'a.*.a' => 'required|notEmpty|between:1,10'
  47. ], [
  48. 'a.*.a.between' => '不在1-10之间'
  49. ]);
  50. $this->assertFalse($validate->validate([
  51. 'a' => ['a' => ['a' => 11]],
  52. 'b' => ['a' => 11]
  53. ]));
  54. $this->assertEquals('不在1-10之间', $validate->getError()->getErrorRuleMsg());
  55. }
  56. }