DifferentWithColumnTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class DifferentWithColumnTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 值不相等
  12. $this->freeValidate();
  13. $this->validate->addColumn('name')->differentWithColumn('realName', true);
  14. $validateResult = $this->validate->validate(['name' => 'test', 'realName' => 'blank']);
  15. $this->assertTrue($validateResult);
  16. // 值相等,但类型不一样
  17. $this->freeValidate();
  18. $this->validate->addColumn('name')->differentWithColumn('realName', true);
  19. $validateResult = $this->validate->validate(['name' => '12', 'realName' => 12]);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 值相等
  26. $this->freeValidate();
  27. $this->validate->addColumn('name')->differentWithColumn('realName', true);
  28. $validateResult = $this->validate->validate(['name' => 'blank', 'realName' => 'blank']);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals('name必须不等于realName的值', $this->validate->getError()->__toString());
  31. // 值相等,但类型不一样
  32. $this->freeValidate();
  33. $this->validate->addColumn('name')->differentWithColumn('realName');
  34. $validateResult = $this->validate->validate(['name' => '123', 'realName' => 123]);
  35. $this->assertFalse($validateResult);
  36. $this->assertEquals('name必须不等于realName的值', $this->validate->getError()->__toString());
  37. $this->freeValidate();
  38. $this->validate->addColumn('bar', 'Bar')->differentWithColumn('foo');
  39. $this->validate->addColumn('foo', 'Foo');
  40. $validateResult = $this->validate->validate(['bar' => 11, 'foo' => 11]);
  41. $this->assertFalse($validateResult);
  42. $this->assertEquals('Bar必须不等于Foo的值', $this->validate->getError()->__toString());
  43. }
  44. // 自定义错误信息断言
  45. public function testCustomErrorMsgCase()
  46. {
  47. // 值相等但类型不符
  48. $this->freeValidate();
  49. $this->validate->addColumn('name')->differentWithColumn('realName', true, '昵称和真实姓名不能一致');
  50. $validateResult = $this->validate->validate(['name' => 'blank', 'realName' => 'blank']);
  51. $this->assertFalse($validateResult);
  52. $this->assertEquals('昵称和真实姓名不能一致', $this->validate->getError()->__toString());
  53. }
  54. }