NotInArrayTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 不在枚举范围测试用例
  5. * Class NotInArrayTest
  6. *
  7. * @internal
  8. */
  9. class NotInArrayTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * strict true
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('fruit')->notInArray(['apple', 'grape', 'orange'], true);
  21. $bool = $this->validate->validate(['fruit' => 'Apple']);
  22. $this->assertTrue($bool);
  23. /*
  24. * strict false
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('fruit')->notInArray(['apple', 'grape', 'orange'], true);
  28. $bool = $this->validate->validate(['fruit' => 'banana']);
  29. $this->assertTrue($bool);
  30. }
  31. /*
  32. * 默认错误信息
  33. */
  34. public function testDefaultErrorMsgCase()
  35. {
  36. $this->freeValidate();
  37. $this->validate->addColumn('fruit')->notInArray(['apple', 'grape', 'orange']);
  38. $bool = $this->validate->validate(['fruit' => 'apple']);
  39. $this->assertFalse($bool);
  40. $this->assertEquals('fruit不能在 [apple,grape,orange] 范围内', $this->validate->getError()->__toString());
  41. }
  42. /*
  43. * 自定义错误信息
  44. */
  45. public function testCustomErrorMsgCase()
  46. {
  47. $this->freeValidate();
  48. $this->validate->addColumn('fruit')->notInArray(['apple', 'grape', 'orange'], false, '水果不能是苹果、葡萄以及橘子');
  49. $bool = $this->validate->validate(['fruit' => 'apple']);
  50. $this->assertFalse($bool);
  51. $this->assertEquals('水果不能是苹果、葡萄以及橘子', $this->validate->getError()->__toString());
  52. }
  53. }