TypeTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace Test\PhpDevCommunity\RequestKit;
  3. use PhpDevCommunity\RequestKit\Type\BoolType;
  4. use PhpDevCommunity\RequestKit\Type\DateTimeType;
  5. use PhpDevCommunity\RequestKit\Type\DateType;
  6. use PhpDevCommunity\RequestKit\Type\FloatType;
  7. use PhpDevCommunity\RequestKit\Type\IntType;
  8. use PhpDevCommunity\RequestKit\Type\NumericType;
  9. use PhpDevCommunity\RequestKit\Type\StringType;
  10. class TypeTest extends \PhpDevCommunity\UniTester\TestCase
  11. {
  12. protected function setUp(): void
  13. {
  14. // TODO: Implement setUp() method.
  15. }
  16. protected function tearDown(): void
  17. {
  18. // TODO: Implement tearDown() method.
  19. }
  20. protected function execute(): void
  21. {
  22. $this->testStringType();
  23. $this->testIntType();
  24. $this->testBoolType();
  25. $this->testDateTimeType();
  26. $this->testDateType();
  27. $this->testNumericType();
  28. }
  29. private function testStringType()
  30. {
  31. $type = (new StringType())
  32. ->required()
  33. ->length(4, 20);
  34. $result = $type->validate(" test ");
  35. $this->assertTrue($result->isValid());
  36. $this->assertEquals(' test ', $result->getValue());
  37. $type->trim();
  38. $result = $type->validate(" test ");
  39. $this->assertTrue($result->isValid());
  40. $this->assertEquals('test', $result->getValue());
  41. $type->length(10, 20);
  42. $result = $type->validate(" test ");
  43. $this->assertFalse($result->isValid());
  44. $this->assertEquals('test must be at least 10 characters long', $result->getError());
  45. $type->length(1, 3);
  46. $result = $type->validate(" test ");
  47. $this->assertFalse($result->isValid());
  48. $this->assertEquals('test cannot be longer than 3 characters', $result->getError());
  49. $type->length(10, 20)->optional();
  50. $result = $type->validate(null);
  51. $this->assertTrue($result->isValid());
  52. $this->assertEquals(null, $result->getValue());
  53. $type->required();
  54. $result = $type->validate(null);
  55. $this->assertFalse($result->isValid());
  56. $this->assertEquals('Value is required, but got null or empty string', $result->getError());
  57. $type->length(1);
  58. $result = $type->validate(123);
  59. $this->assertTrue($result->isValid());
  60. $this->assertEquals('123', $result->getValue());
  61. $this->assertEquals(123, $result->getRawValue());
  62. $type->strict();
  63. $result = $type->validate(123);
  64. $this->assertFalse($result->isValid());
  65. $this->assertEquals('Value must be a string, got: integer', $result->getError());
  66. $type->uppercase();
  67. $result = $type->validate("is test");
  68. $this->assertTrue($result->isValid());
  69. $this->assertStrictEquals('IS TEST', $result->getValue());
  70. $type->lowercase();
  71. $type->removeSpaces();
  72. $result = $type->validate("is test for me");
  73. $this->assertTrue($result->isValid());
  74. $this->assertStrictEquals('istestforme', $result->getValue());
  75. $type->length(6);
  76. $type->padLeft(6, "0");
  77. $result = $type->validate("1");
  78. $this->assertTrue($result->isValid());
  79. $this->assertStrictEquals('000001', $result->getValue());
  80. $type->length(6);
  81. $type->removeChars('+', '-', '.');
  82. $result = $type->validate("123-45+6.");
  83. $this->assertTrue($result->isValid());
  84. $this->assertStrictEquals('123456', $result->getValue());
  85. $type->allowed('123456', '654321');
  86. $result = $type->validate("654321");
  87. $this->assertTrue($result->isValid());
  88. $this->assertStrictEquals('654321', $result->getValue());
  89. $type->allowed('123456', '654321');
  90. $result = $type->validate("254321");
  91. $this->assertFalse($result->isValid());
  92. $this->assertNotNull($result->getError());
  93. }
  94. private function testIntType()
  95. {
  96. $type = (new IntType())
  97. ->required()
  98. ->min(5)
  99. ->max(12);
  100. $result = $type->validate(12);
  101. $this->assertTrue($result->isValid());
  102. $this->assertEquals(12, $result->getValue());
  103. $result = $type->validate('12');
  104. $this->assertTrue($result->isValid());
  105. $this->assertEquals(12, $result->getValue());
  106. $result = $type->validate(1);
  107. $this->assertFalse($result->isValid());
  108. $this->assertNotNull($result->getError());
  109. $type->strict();
  110. $result = $type->validate("10");
  111. $this->assertFalse($result->isValid());
  112. $this->assertNotNull($result->getError());
  113. $result = $type->validate(null);
  114. $this->assertFalse($result->isValid());
  115. $this->assertNotNull($result->getError());
  116. $type->optional();
  117. $result = $type->validate(null);
  118. $this->assertTrue($result->isValid());
  119. $this->assertNull($result->getError());
  120. $intWithTransform = (new IntType())
  121. ->required()
  122. ->transform(function ($value) {
  123. {
  124. if (!is_string($value)) {
  125. return $value;
  126. }
  127. if (preg_match('/-?\d+(\.\d+)?/', $value, $match)) {
  128. return $match[0];
  129. }
  130. return $value;
  131. }
  132. })
  133. ->min(1)
  134. ->max(12);
  135. $result = $intWithTransform->validate("5 UNION ALL");
  136. $this->assertTrue($result->isValid());
  137. $this->assertEquals(5,$result->getValue());
  138. $floatWithTransform = (new FloatType())
  139. ->required()
  140. ->transform(function ($value) {
  141. {
  142. if (!is_string($value)) {
  143. return $value;
  144. }
  145. if (preg_match('/-?\d+(\.\d+)?/', $value, $match)) {
  146. return $match[0];
  147. }
  148. return $value;
  149. }
  150. })
  151. ->min(1)
  152. ->max(12);
  153. $result = $floatWithTransform->validate("3.04 OR 1=1");
  154. $this->assertTrue($result->isValid());
  155. $this->assertEquals(3.04,$result->getValue());
  156. }
  157. private function testBoolType()
  158. {
  159. $type = (new BoolType())
  160. ->required();
  161. $result = $type->validate(true);
  162. $this->assertTrue($result->isValid());
  163. $this->assertEquals(true, $result->getValue());
  164. $result = $type->validate('true');
  165. $this->assertTrue($result->isValid());
  166. $this->assertEquals(true, $result->getValue());
  167. $result = $type->validate('1');
  168. $this->assertTrue($result->isValid());
  169. $this->assertEquals(true, $result->getValue());
  170. $result = $type->validate(1);
  171. $this->assertTrue($result->isValid());
  172. $this->assertEquals(true, $result->getValue());
  173. $result = $type->validate(false);
  174. $this->assertTrue($result->isValid());
  175. $this->assertEquals(false, $result->getValue());
  176. $result = $type->validate('false');
  177. $this->assertTrue($result->isValid());
  178. $this->assertEquals(false, $result->getValue());
  179. $result = $type->validate('0');
  180. $this->assertTrue($result->isValid());
  181. $this->assertEquals(false, $result->getValue());
  182. $result = $type->validate(0);
  183. $this->assertTrue($result->isValid());
  184. $this->assertEquals(false, $result->getValue());
  185. }
  186. private function testDateTimeType()
  187. {
  188. $type = (new DateTimeType())
  189. ->format('Y-m-d H:i:s')
  190. ->required();
  191. $result = $type->validate('2020-01-01 10:00:00');
  192. $this->assertTrue($result->isValid());
  193. $this->assertInstanceOf(\DateTime::class, $result->getValue());
  194. $type->optional();
  195. $result = $type->validate(null);
  196. $this->assertTrue($result->isValid());
  197. $this->assertEquals(null, $result->getValue());
  198. $type->required();
  199. $result = $type->validate('2020-01-01 10:00');
  200. $this->assertFalse($result->isValid());
  201. $this->assertNotNull($result->getError());
  202. ;
  203. $result = $type->validate(strtotime('2020-01-01 10:00'));
  204. $this->assertTrue($result->isValid());
  205. $this->assertInstanceOf(\DateTime::class, $result->getValue());
  206. $datetime = $result->getValue();
  207. $this->assertEquals('2020-01-01 10:00:00', $datetime->format('Y-m-d H:i:s'));
  208. }
  209. private function testDateType()
  210. {
  211. $type = (new DateType())
  212. ->format('Y-m-d')
  213. ->required();
  214. $result = $type->validate('2020-01-01');
  215. $this->assertTrue($result->isValid());
  216. $this->assertInstanceOf(\DateTime::class, $result->getValue());
  217. $type->optional();
  218. $result = $type->validate(null);
  219. $this->assertTrue($result->isValid());
  220. $this->assertEquals(null, $result->getValue());
  221. $type->required();
  222. $result = $type->validate('2020-01-01 10:00');
  223. $this->assertFalse($result->isValid());
  224. $this->assertNotNull($result->getError());
  225. $result = $type->validate(strtotime('2020-01-01'));
  226. $this->assertTrue($result->isValid());
  227. $this->assertInstanceOf(\DateTime::class, $result->getValue());
  228. $datetime = $result->getValue();
  229. $this->assertEquals('2020-01-01', $datetime->format('Y-m-d'));
  230. }
  231. private function testNumericType()
  232. {
  233. $testCases = [
  234. [1, '1'],
  235. ['1', '1'],
  236. ['1.0', '1.0'],
  237. [1.0, '1'],
  238. [0, '0'],
  239. [0.0, '0'],
  240. ['0.0', '0.0'],
  241. ['136585.589', '136585.589'],
  242. [136585.589, '136585.589'],
  243. [-1, "-1"],
  244. [-1.5,'-1.5'],
  245. [PHP_INT_MAX, (string)PHP_INT_MAX],
  246. ];
  247. foreach ($testCases as [$input, $expectedOutput]) {
  248. $type = (new NumericType())->required();
  249. $result = $type->validate($input);
  250. $this->assertTrue($result->isValid());
  251. $this->assertStrictEquals($expectedOutput, $result->getValue());
  252. }
  253. }
  254. }