FieldValidationTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php declare(strict_types=1);
  2. namespace Inhere\ValidateTest;
  3. use Inhere\Validate\FieldValidation;
  4. use Inhere\Validate\FV;
  5. use Inhere\ValidateTest\Example\FieldExample;
  6. use PHPUnit\Framework\TestCase;
  7. use Throwable;
  8. /**
  9. * Class FieldValidationTest
  10. *
  11. * @package Inhere\ValidateTest
  12. */
  13. class FieldValidationTest extends TestCase
  14. {
  15. public array $data = [
  16. // 'userId' => 234,
  17. 'userId' => 'is not an integer',
  18. 'tagId' => '234535',
  19. // 'freeTime' => '1456767657', // filed not exists
  20. 'note' => '',
  21. 'name' => 'Ajohn',
  22. 'status' => 2,
  23. 'existsField' => 'test',
  24. 'passwd' => 'password',
  25. 'repasswd' => 'repassword',
  26. 'insertTime' => '1456767657',
  27. 'goods' => [
  28. 'apple' => 34,
  29. 'pear' => 50,
  30. ],
  31. ];
  32. public function testRuleCollectError(): void
  33. {
  34. $rv = FieldValidation::make(['name' => 'inhere'], [
  35. []
  36. ]);
  37. try {
  38. $rv->validate();
  39. } catch (Throwable $e) {
  40. $this->assertSame('Please setting the field(string) to wait validate! position: rule[0]', $e->getMessage());
  41. }
  42. $rv = FieldValidation::make(['name' => 'inhere'], [
  43. ['name']
  44. ]);
  45. try {
  46. $rv->validate();
  47. } catch (Throwable $e) {
  48. $this->assertSame(
  49. 'Please setting the validator(s) for validate field! position: rule[1]',
  50. $e->getMessage()
  51. );
  52. }
  53. }
  54. public function testValidateField(): void
  55. {
  56. $rules = [
  57. ['freeTime', 'required'],
  58. ['userId', 'required|int'],
  59. ['tagId', 'size:0,50'],
  60. ['status', 'enum:1,2'],
  61. ['goods.pear', 'max:30'],
  62. ];
  63. $v = FieldValidation::make($this->data, $rules);
  64. $v->setMessages([
  65. 'freeTime.required' => 'freeTime is required!!!!'
  66. ])->validate([], false);
  67. $this->assertFalse($v->isOk());
  68. $this->assertTrue($v->failed());
  69. $errors = $v->getErrors();
  70. $this->assertNotEmpty($errors);
  71. $this->assertCount(4, $errors);
  72. $this->assertSame('freeTime is required!!!!', $v->getErrors('freeTime')[0]);
  73. $v = FieldValidation::check($this->data, [
  74. ['goods.pear', 'required|int|min:30|max:60']
  75. ]);
  76. $this->assertTrue($v->isOk());
  77. $v = FieldValidation::check($this->data, [
  78. ['userId', 'required|int'],
  79. ['userId', 'min:1'],
  80. ]);
  81. $this->assertFalse($v->isOk());
  82. $errors = $v->getErrors();
  83. $this->assertNotEmpty($errors);
  84. $this->assertCount(1, $errors);
  85. $v = FieldValidation::check([
  86. 'title' => 'hello',
  87. ], [
  88. ['title', 'required|string:2,8']
  89. ]);
  90. $this->assertTrue($v->isOk());
  91. $v = FieldValidation::check([
  92. 'title' => 'hello',
  93. ], [
  94. ['title', 'required|string:1,3']
  95. ]);
  96. $this->assertTrue($v->isFail());
  97. $this->assertSame('title must be a string and length range must be 1 ~ 3', $v->firstError());
  98. }
  99. public function testOnScene(): void
  100. {
  101. $data = [
  102. 'user' => 'inhere',
  103. 'pwd' => '123456',
  104. 'code' => '1234',
  105. ];
  106. $v = FieldValidation::make($data, [
  107. ['user', 'required|string', 'on' => 's1'],
  108. ['code', 'required|int', 'filter' => 'int', 'on' => 's2'],
  109. ]);
  110. $v->atScene('s1')->validate();
  111. $this->assertCount(1, $v->getUsedRules());
  112. }
  113. public function testScenarios(): void
  114. {
  115. $data = [
  116. 'user' => 'inhere',
  117. 'pwd' => '123456',
  118. 'code' => '1234',
  119. ];
  120. $v = FieldExample::quick($data, 'create')->validate();
  121. $this->assertTrue($v->isOk());
  122. $this->assertEmpty($v->getErrors());
  123. $data = [
  124. 'user' => 'inhere',
  125. 'pwd' => '123456',
  126. 'code' => '12345',
  127. ];
  128. $v = FieldExample::quick($data, 'create')->validate();
  129. $this->assertFalse($v->isOk());
  130. $this->assertEquals('code length must is 4', $v->firstError());
  131. $v = FieldExample::quick($data, 'update')->validate();
  132. $this->assertTrue($v->isOk());
  133. $this->assertEmpty($v->getErrors());
  134. }
  135. /**
  136. * @link https://github.com/inhere/php-validate/issues/22
  137. */
  138. public function testIssues22(): void
  139. {
  140. $rs = [
  141. ['id', 'required'],
  142. ['name', 'required|string:5,10', 'msg' => '5~10位的字符串'],
  143. ['sex', 'required|enum:0,1'],
  144. ['age', 'requiredIf:sex,0|int']
  145. ];
  146. $v = FV::check([
  147. 'id' => 1,
  148. 'name' => '12345',
  149. 'sex' => 0,
  150. 'age' => 25,
  151. ], $rs);
  152. $this->assertTrue($v->isOk());
  153. $v = FV::check([
  154. 'id' => 1,
  155. 'name' => '12345',
  156. 'sex' => 1,
  157. // 'age' => 25,
  158. ], $rs);
  159. $this->assertTrue($v->isOk());
  160. $v = FV::check([
  161. 'id' => 1,
  162. 'name' => '12345',
  163. 'sex' => 0,
  164. // 'age' => 25,
  165. // 'age' => 'string',
  166. ], $rs);
  167. $this->assertFalse($v->isOk());
  168. $this->assertSame('parameter age is required!', $v->firstError());
  169. $v = FV::check([
  170. 'id' => 1,
  171. 'name' => '12345',
  172. 'sex' => 0,
  173. 'age' => 'string',
  174. ], $rs);
  175. $this->assertFalse($v->isOk());
  176. $this->assertSame('age must be an integer!', $v->firstError());
  177. }
  178. /**
  179. * @link https://github.com/inhere/php-validate/issues/36
  180. */
  181. public function testIssues36(): void
  182. {
  183. $params = [];
  184. $v = FieldValidation::check($params, [
  185. ['owner', 'required', 'msg' => ['owner' => 'owner 缺失']],
  186. ]);
  187. $this->assertTrue($v->isFail());
  188. //$this->assertSame('parameter owner is required!', $v->firstError());
  189. $v = FieldValidation::check($params, [
  190. ['owner', 'required', 'msg' => ['required' => 'owner 缺失']],
  191. ]);
  192. $this->assertTrue($v->isFail());
  193. $this->assertSame('owner 缺失', $v->firstError());
  194. }
  195. /**
  196. * @link https://github.com/inhere/php-validate/issues/55
  197. */
  198. public function testIssues55(): void
  199. {
  200. $data = ['title' => '', 'name' => ''];
  201. $msg = ['title' => '标题不能为空。', 'name' => '姓名不能为空。'];
  202. $validator = \Inhere\Validate\Validation::make($data, [
  203. ['title,name', 'required', 'msg' => $msg],
  204. ])->validate([], false);
  205. $this->assertTrue($validator->isFail());
  206. $this->assertSame(
  207. [[
  208. 'name' => 'title',
  209. 'msg' => $msg['title'],
  210. ], [
  211. 'name' => 'name',
  212. 'msg' => $msg['name'],
  213. ]],
  214. $validator->getErrors()
  215. );
  216. }
  217. }