ValidationTraitTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php declare(strict_types=1);
  2. namespace Inhere\ValidateTest;
  3. use Inhere\Validate\Validation;
  4. use Inhere\Validate\ValidationTrait;
  5. use InvalidArgumentException;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * Class ValidationTraitTest
  9. *
  10. * @package Inhere\ValidateTest
  11. */
  12. class ValidationTraitTest extends TestCase
  13. {
  14. public function testNoDataProperty(): void
  15. {
  16. $v = new class {
  17. use ValidationTrait;
  18. };
  19. // want data property
  20. $this->expectException(InvalidArgumentException::class);
  21. $v->validate();
  22. }
  23. public function testGetByPath(): void
  24. {
  25. $v = Validation::make([
  26. 'prod' => [
  27. 'key0' => 'val0',
  28. [
  29. 'attr' => [
  30. 'wid' => 1
  31. ]
  32. ],
  33. [
  34. 'attr' => [
  35. 'wid' => 2,
  36. ]
  37. ],
  38. [
  39. 'attr' => [
  40. 'wid' => 3,
  41. ]
  42. ],
  43. ],
  44. 'users' => [
  45. ['id' => 1,],
  46. ['id' => 2,]
  47. ]
  48. ]);
  49. $val = $v->getByPath('users.*.id');
  50. $this->assertSame([1, 2], $val);
  51. $val = $v->getByPath('prod.key0');
  52. $this->assertSame('val0', $val);
  53. $val = $v->getByPath('prod.0.attr');
  54. $this->assertSame(['wid' => 1], $val);
  55. $val = $v->getByPath('prod.0.attr.wid');
  56. $this->assertSame(1, $val);
  57. $val = $v->getByPath('prod.*.attr');
  58. $this->assertSame([['wid' => 1], ['wid' => 2], ['wid' => 3],], $val);
  59. $val = $v->getByPath('prod.*.attr.wid');
  60. $this->assertSame([1, 2, 3], $val);
  61. }
  62. // TODO key is must exists on data.
  63. // public function testIndexedArrayGetByPath(): void
  64. // {
  65. // $v = Validation::make([
  66. // ['attr' => ['wid' => 1]],
  67. // ['attr' => ['wid' => 2]],
  68. // ['attr' => ['wid' => 3]],
  69. // ]);
  70. //
  71. // $val = $v->GetByPath('0.attr');
  72. // $this->assertSame(['wid' => 1], $val);
  73. //
  74. // $val = $v->getByPath('0.attr.wid');
  75. // $this->assertSame(1, $val);
  76. // }
  77. /**
  78. * @var \array[][] see PR https://github.com/inhere/php-validate/pull/19
  79. */
  80. public array $deepData = [
  81. 'companies' => [
  82. [
  83. 'name' => 'ms',
  84. 'departments' => [
  85. [
  86. 'name' => '111',
  87. 'employees' => [
  88. [
  89. 'name' => 'aaa',
  90. 'manage' => 1,
  91. ],
  92. [
  93. 'name' => 'bbb',
  94. 'manage' => 2,
  95. ],
  96. ],
  97. ],
  98. [
  99. 'name' => '222',
  100. 'employees' => [
  101. [
  102. 'name' => 'ccc',
  103. 'manage' => 3,
  104. ],
  105. [
  106. 'name' => 'ddd',
  107. 'manage' => 4,
  108. ],
  109. ],
  110. ],
  111. [
  112. 'name' => '333',
  113. 'employees' => [
  114. [
  115. 'name' => 'eee',
  116. 'manage' => 5,
  117. ],
  118. [
  119. 'name' => 'fff',
  120. 'manage' => 6,
  121. ],
  122. ],
  123. ],
  124. ]
  125. ],
  126. [
  127. 'name' => 'google',
  128. 'departments' => [
  129. [
  130. 'name' => '444',
  131. 'employees' => [
  132. [
  133. 'name' => 'xxx',
  134. 'manage' => 7,
  135. ],
  136. [
  137. 'name' => 'yyy',
  138. 'manage' => 8,
  139. ],
  140. ],
  141. ],
  142. ]
  143. ],
  144. ],
  145. ];
  146. public function testMultidimensionalArray(): void
  147. {
  148. $v = Validation::make($this->deepData);
  149. $val = $v->getByPath('companies.*.name');
  150. $this->assertSame(['ms', 'google'], $val);
  151. }
  152. public function testMultidimensionalArray1(): void
  153. {
  154. $v = Validation::make($this->deepData);
  155. $val = $v->getByPath('companies.0.departments.*.employees.0.manage');
  156. $this->assertSame([1, 3, 5], $val);
  157. }
  158. public function testMultidimensionalArray2(): void
  159. {
  160. $v = Validation::make($this->deepData);
  161. $val = $v->getByPath('companies.0.departments.*.employees.*.manage');
  162. $this->assertSame([1, 2, 3, 4, 5, 6], $val);
  163. }
  164. public function testMultidimensionalArray3(): void
  165. {
  166. $v = Validation::make($this->deepData);
  167. $val = $v->getByPath('companies.*.departments.*.employees.*.name');
  168. $this->assertSame(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'xxx', 'yyy'], $val);
  169. }
  170. public function testBeforeAndAfter(): void
  171. {
  172. $v = Validation::make(['name' => 'inhere'], [
  173. ['name', 'string', 'min' => 3, 'filter' => 'trim|upper']
  174. ]);
  175. $v->onBeforeValidate(function (Validation $v) {
  176. $this->assertSame('inhere', $v->getRaw('name'));
  177. $this->assertNull($v->getSafe('name'));
  178. return true;
  179. });
  180. $v->onAfterValidate(function (Validation $v) {
  181. $this->assertSame('INHERE', $v->getRaw('name'));
  182. $this->assertSame('INHERE', $v->getSafe('name'));
  183. });
  184. $v->validate();
  185. $this->assertTrue($v->isOk());
  186. $this->assertTrue($v->isValidated());
  187. $v->validate();
  188. $this->assertTrue($v->isValidated());
  189. }
  190. public function testRuleBeforeAndAfter(): void
  191. {
  192. $v = Validation::make(['name' => 'inhere'], [
  193. [
  194. 'name',
  195. 'string',
  196. 'min' => 3,
  197. 'before' => function ($value) {
  198. return $value === 'inhere';
  199. },
  200. 'after' => function ($value) {
  201. $this->assertSame('inhere', $value);
  202. return true;
  203. }
  204. ]
  205. ]);
  206. $v->validate();
  207. $this->assertTrue($v->isOk());
  208. }
  209. }