ErrorMessageTraitTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php declare(strict_types=1);
  2. namespace Inhere\ValidateTest\Traits;
  3. use Inhere\Validate\FieldValidation;
  4. use Inhere\Validate\Validation;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * Class ErrorMessageTraitTest
  8. */
  9. class ErrorMessageTraitTest extends TestCase
  10. {
  11. private array $sampleDate = [
  12. // 'userId' => 234,
  13. 'userId' => 'is not an integer',
  14. 'tagId' => '234535',
  15. // 'freeTime' => '1456767657', // filed not exists
  16. 'note' => '',
  17. 'name' => 'Ajohn',
  18. 'status' => 2,
  19. 'existsField' => 'test',
  20. 'passwd' => 'password',
  21. 'repasswd' => 'repassword',
  22. 'insertTime' => '1456767657',
  23. 'goods' => [
  24. 'apple' => 34,
  25. 'pear' => 50,
  26. ],
  27. ];
  28. public function testErrorMessage(): void
  29. {
  30. // empty test
  31. $v = FieldValidation::make($this->sampleDate);
  32. $this->assertCount(0, $v->getErrors());
  33. $this->assertCount(0, $v->getMessages());
  34. $this->assertSame('', $v->firstError());
  35. $this->assertSame([], $v->firstError(false));
  36. $v = FieldValidation::check($this->sampleDate, [
  37. ['userId', 'required|int'],
  38. ]);
  39. $this->assertTrue($v->isPrettifyName());
  40. $this->assertNotEmpty($v->getErrors());
  41. $this->assertNotEmpty($v->getErrors('userId'));
  42. // firstError
  43. $this->assertSame('user id must be an integer!', $v->firstError());
  44. $this->assertNotEmpty($error = $v->firstError(false));
  45. $this->assertSame('userId', $error['name']);
  46. $this->assertSame('user id must be an integer!', $error['msg']);
  47. // lastError
  48. $this->assertSame('user id must be an integer!', $v->lastError());
  49. $this->assertNotEmpty($error = $v->lastError(false));
  50. $this->assertSame('userId', $error['name']);
  51. $this->assertSame('user id must be an integer!', $error['msg']);
  52. // reset validation
  53. $v->resetValidation();
  54. // prettifyName
  55. $v->setPrettifyName(false);
  56. $this->assertFalse($v->isPrettifyName());
  57. // re-validate
  58. $v->validate();
  59. // firstError
  60. $this->assertSame('userId must be an integer!', $v->firstError());
  61. $this->assertNotEmpty($error = $v->firstError(false));
  62. $this->assertSame('userId', $error['name']);
  63. $this->assertSame('userId must be an integer!', $error['msg']);
  64. // lastError
  65. $this->assertSame('userId must be an integer!', $v->lastError());
  66. $this->assertNotEmpty($error = $v->lastError(false));
  67. $this->assertSame('userId', $error['name']);
  68. $this->assertSame('userId must be an integer!', $error['msg']);
  69. }
  70. public function testFieldTranslate(): void
  71. {
  72. $v = FieldValidation::make([]);
  73. // getTranslates
  74. $this->assertEmpty($v->getTranslates());
  75. $v->setTranslates([
  76. 'userId' => 'User ID',
  77. ]);
  78. $this->assertNotEmpty($v->getTranslates());
  79. // getTranslate
  80. $this->assertSame('User ID', $v->getTranslate('userId'));
  81. $this->assertSame('not exist', $v->getTranslate('notExist'));
  82. // clearTranslates
  83. $v->clearTranslates();
  84. $this->assertEmpty($v->getTranslates());
  85. }
  86. /**
  87. * for https://github.com/inhere/php-validate/issues/10
  88. */
  89. public function testForIssues10(): void
  90. {
  91. $v = Validation::check([
  92. 'page' => 0
  93. ], [
  94. ['page', 'integer', 'min' => 1]
  95. ]);
  96. $this->assertTrue($v->isFail());
  97. $this->assertSame('page must be an integer and minimum value is 1', $v->firstError());
  98. }
  99. }