AlphaTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 给定的参数是否是字母 即[a-zA-Z]
  5. * Class AlphaTest
  6. *
  7. * @internal
  8. */
  9. class AlphaTest extends BaseTestCase
  10. {
  11. // 合法断言
  12. public function testValidCase()
  13. {
  14. $this->freeValidate();
  15. $this->validate->addColumn('alpha')->alpha();
  16. $validateResult = $this->validate->validate(['alpha' => 'alpha']);
  17. $this->assertTrue($validateResult);
  18. }
  19. // 默认错误信息断言
  20. public function testDefaultErrorMsgCase()
  21. {
  22. $this->freeValidate();
  23. $this->validate->addColumn('alpha')->alpha();
  24. $validateResult = $this->validate->validate(['alpha' => 'alpha1']);
  25. $this->assertFalse($validateResult);
  26. $this->assertEquals($this->validate->getError()->__toString(), 'alpha只能是字母');
  27. $this->freeValidate();
  28. $this->validate->addColumn('alpha', '用户名')->alpha();
  29. $validateResult = $this->validate->validate(['alpha' => 123]);
  30. $this->assertFalse($validateResult);
  31. $this->assertEquals($this->validate->getError()->__toString(), '用户名只能是字母');
  32. }
  33. // 自定义错误信息断言
  34. public function testCustomErrorMsgCase()
  35. {
  36. $this->freeValidate();
  37. $this->validate->addColumn('alpha')->alpha('您输入的用户名不合法');
  38. $validateResult = $this->validate->validate(['alpha' => true]);
  39. $this->assertFalse($validateResult);
  40. $this->assertEquals($this->validate->getError()->__toString(), '您输入的用户名不合法');
  41. }
  42. }