DifferentTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class DifferentTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 值不相等
  12. $this->freeValidate();
  13. $this->validate->addColumn('equal')->different('true');
  14. $validateResult = $this->validate->validate(['equal' => 'false']);
  15. $this->assertTrue($validateResult);
  16. // 值相等,但类型不一样
  17. $this->freeValidate();
  18. $this->validate->addColumn('age')->different(12, true);
  19. $validateResult = $this->validate->validate(['age' => '12']);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 值相等
  26. $this->freeValidate();
  27. $this->validate->addColumn('equal')->different('true', true);
  28. $validateResult = $this->validate->validate(['equal' => 'true']);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals('equal必须不等于true', $this->validate->getError()->__toString());
  31. // 值相等,但类型不一样
  32. $this->freeValidate();
  33. $this->validate->addColumn('age')->different('12');
  34. $validateResult = $this->validate->validate(['age' => 12]);
  35. $this->assertFalse($validateResult);
  36. $this->assertEquals('age必须不等于12', $this->validate->getError()->__toString());
  37. }
  38. // 自定义错误信息断言
  39. public function testCustomErrorMsgCase()
  40. {
  41. // 值相等但类型不符
  42. $this->freeValidate();
  43. $this->validate->addColumn('equal', '参数')->different('0');
  44. $validateResult = $this->validate->validate(['equal' => 0]);
  45. $this->assertFalse($validateResult);
  46. $this->assertEquals('参数必须不等于0', $this->validate->getError()->__toString());
  47. }
  48. }