123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace EasySwoole\Validate\tests;
- /**
- * @internal
- */
- class BoolTest extends BaseTestCase
- {
- // 合法断言
- public function testValidCase()
- {
- // 值为true
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool();
- $validateResult = $this->validate->validate(['boolean' => true]);
- $this->assertTrue($validateResult);
- // 值为 1 等同于 true
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool();
- $validateResult = $this->validate->validate(['boolean' => 1]);
- $this->assertTrue($validateResult);
- // 值为false
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool();
- $validateResult = $this->validate->validate(['boolean' => false]);
- $this->assertTrue($validateResult);
- // 值为 0 等同于 false
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool();
- $validateResult = $this->validate->validate(['boolean' => 0]);
- $this->assertTrue($validateResult);
- }
- // 默认错误信息断言 TODO 需要确认修正
- public function testDefaultErrorMsgCase()
- {
- // 值为文本值无法通过
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool();
- $validateResult = $this->validate->validate(['boolean' => 'true']);
- $this->assertFalse($validateResult);
- $this->assertEquals('boolean只能是布尔值', $this->validate->getError()->__toString());
- // 值为文本数字时无法通过
- $this->freeValidate();
- $this->validate->addColumn('boolean', '状态')->bool();
- $validateResult = $this->validate->validate(['boolean' => '1']);
- $this->assertFalse($validateResult);
- $this->assertEquals('状态只能是布尔值', $this->validate->getError()->__toString());
- }
- // 自定义错误信息断言
- public function testCustomErrorMsgCase()
- {
- // 异常值测试
- $this->freeValidate();
- $this->validate->addColumn('boolean')->bool('状态只能是开启或关闭');
- $validateResult = $this->validate->validate(['boolean' => null]);
- $this->assertFalse($validateResult);
- $this->assertEquals('状态只能是开启或关闭', $this->validate->getError()->__toString());
- }
- }
|