EqualWithColumnTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class EqualWithColumnTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 值相等,但类型不一样
  12. $this->freeValidate();
  13. $this->validate->addColumn('password')->equalWithColumn('rePassword');
  14. $validateResult = $this->validate->validate(['password' => '123', 'rePassword' => 123]);
  15. $this->assertTrue($validateResult);
  16. // 值相等,类型一样
  17. $this->freeValidate();
  18. $this->validate->addColumn('password')->equalWithColumn('rePassword', true);
  19. $validateResult = $this->validate->validate(['password' => '123', 'rePassword' => '123']);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 值相等,但类型不一样
  26. $this->freeValidate();
  27. $this->validate->addColumn('password')->equalWithColumn('rePassword', true);
  28. $validateResult = $this->validate->validate(['password' => '123', 'rePassword' => 123]);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals('password必须等于rePassword的值', $this->validate->getError()->__toString());
  31. // 值不相等
  32. $this->freeValidate();
  33. $this->validate->addColumn('password')->equalWithColumn('rePassword');
  34. $validateResult = $this->validate->validate(['password' => '123', 'rePassword' => 1234]);
  35. $this->assertFalse($validateResult);
  36. $this->assertEquals('password必须等于rePassword的值', $this->validate->getError()->__toString());
  37. $this->freeValidate();
  38. $this->validate->addColumn('bar', 'Bar')->equalWithColumn('foo');
  39. $this->validate->addColumn('foo', 'Foo');
  40. $validateResult = $this->validate->validate(['bar' => 10, '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('password')->equalWithColumn('rePassword', false, '密码必须和确认密码一样');
  50. $validateResult = $this->validate->validate(['password' => '123', 'rePassword' => 1234]);
  51. $this->assertFalse($validateResult);
  52. $this->assertEquals('密码必须和确认密码一样', $this->validate->getError()->__toString());
  53. }
  54. }