LengthMinTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 最小长度测试用例
  5. * Class LengthMinTest
  6. *
  7. * @internal
  8. */
  9. class LengthMinTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * int
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('name')->lengthMin(2);
  21. $bool = $this->validate->validate(['name' => 12]);
  22. $this->assertTrue($bool);
  23. /*
  24. * 字符串整数
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('name')->lengthMin(2);
  28. $bool = $this->validate->validate(['name' => '12']);
  29. $this->assertTrue($bool);
  30. /*
  31. * 数组
  32. */
  33. $this->freeValidate();
  34. $this->validate->addColumn('fruit')->lengthMin(2);
  35. $bool = $this->validate->validate(['fruit' => ['apple', 'grape', 'orange']]);
  36. $this->assertTrue($bool);
  37. /*
  38. * file
  39. */
  40. $this->freeValidate();
  41. $this->validate->addColumn('file')->lengthMin(1);
  42. $bool = $this->validate->validate(['file' => (new UploadFile(__DIR__ . '/../res/easyswoole.png', 2, 200))]);
  43. $this->assertTrue($bool);
  44. }
  45. /*
  46. * 默认错误信息
  47. */
  48. public function testDefaultErrorMsgCase()
  49. {
  50. /*
  51. * int
  52. */
  53. $this->freeValidate();
  54. $this->validate->addColumn('name')->lengthMin(6);
  55. $bool = $this->validate->validate(['name' => 123]);
  56. $this->assertFalse($bool);
  57. $this->assertEquals('name长度不能小于6', $this->validate->getError()->__toString());
  58. /*
  59. * 字符串整数
  60. */
  61. $this->freeValidate();
  62. $this->validate->addColumn('name')->lengthMin(6);
  63. $bool = $this->validate->validate(['name' => '123']);
  64. $this->assertFalse($bool);
  65. $this->assertEquals('name长度不能小于6', $this->validate->getError()->__toString());
  66. /*
  67. * 数组
  68. */
  69. $this->freeValidate();
  70. $this->validate->addColumn('fruit')->lengthMin(6);
  71. $bool = $this->validate->validate(['fruit' => ['apple', 'grape', 'orange', 'banana']]);
  72. $this->assertFalse($bool);
  73. $this->assertEquals('fruit长度不能小于6', $this->validate->getError()->__toString());
  74. /*
  75. * 对象
  76. */
  77. $this->freeValidate();
  78. $this->validate->addColumn('fruit')->lengthMin(6);
  79. $bool = $this->validate->validate(['fruit' => (object)['apple', 'grape', 'orange', 'banana']]);
  80. $this->assertFalse($bool);
  81. $this->assertEquals('fruit长度不能小于6', $this->validate->getError()->__toString());
  82. }
  83. /*
  84. * 自定义错误信息
  85. */
  86. public function testCustomErrorMsgCase()
  87. {
  88. $this->freeValidate();
  89. $this->validate->addColumn('name')->lengthMin(6, '名字长度最少6位');
  90. $bool = $this->validate->validate(['name' => 'blank']);
  91. $this->assertFalse($bool);
  92. $this->assertEquals('名字长度最少6位', $this->validate->getError()->__toString());
  93. }
  94. }