LengthMaxTest.php 3.1 KB

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