EqualTest.php 1.7 KB

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