InArrayTest.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 在给定的数组中
  5. * Class ActiveUrlTest
  6. *
  7. * @internal
  8. */
  9. class InArrayTest extends BaseTestCase
  10. {
  11. // 合法断言
  12. public function testValidCase()
  13. {
  14. // 符合条件
  15. $this->freeValidate();
  16. $this->validate->addColumn('number')->inArray([1, 2, 3, 4, 5]);
  17. $validateResult = $this->validate->validate(['number' => 5]);
  18. $this->assertTrue($validateResult);
  19. }
  20. // 默认错误信息断言
  21. public function testDefaultErrorMsgCase()
  22. {
  23. // 条件不符
  24. $this->freeValidate();
  25. $this->validate->addColumn('number')->inArray([1, 2, 3, 4, 5]);
  26. $validateResult = $this->validate->validate(['number' => 6]);
  27. $this->assertFalse($validateResult);
  28. $this->assertEquals('number必须在 [1,2,3,4,5] 范围内', $this->validate->getError()->__toString());
  29. }
  30. // 自定义错误信息断言
  31. public function testCustomErrorMsgCase()
  32. {
  33. // 严格模式下类型不等也不通过
  34. $this->freeValidate();
  35. $this->validate->addColumn('number')->inArray([1, 2, 3, 4, 5], true, '您选择的选项不合法');
  36. $validateResult = $this->validate->validate(['number' => '1']);
  37. $this->assertFalse($validateResult);
  38. $this->assertEquals('您选择的选项不合法', $this->validate->getError()->__toString());
  39. }
  40. }