浏览代码

Add unit test for numeric extraction transformer

phpdevcommunity 3 周之前
父节点
当前提交
9e95c70770
共有 2 个文件被更改,包括 45 次插入0 次删除
  1. 1 0
      src/Type/FloatType.php
  2. 44 0
      tests/TypeTest.php

+ 1 - 0
src/Type/FloatType.php

@@ -12,6 +12,7 @@ final class FloatType extends AbstractType
     private ?float $min = null;
     private ?float $max = null;
 
+
     public function min(float $min): self
     {
         $this->min = $min;

+ 44 - 0
tests/TypeTest.php

@@ -3,6 +3,7 @@ namespace Test\PhpDevCommunity\RequestKit;
 use PhpDevCommunity\RequestKit\Type\BoolType;
 use PhpDevCommunity\RequestKit\Type\DateTimeType;
 use PhpDevCommunity\RequestKit\Type\DateType;
+use PhpDevCommunity\RequestKit\Type\FloatType;
 use PhpDevCommunity\RequestKit\Type\IntType;
 use PhpDevCommunity\RequestKit\Type\NumericType;
 use PhpDevCommunity\RequestKit\Type\StringType;
@@ -133,6 +134,7 @@ class TypeTest extends \PhpDevCommunity\UniTester\TestCase
         $this->assertFalse($result->isValid());
         $this->assertNotNull($result->getError());
 
+
         $type->strict();
         $result = $type->validate("10");
         $this->assertFalse($result->isValid());
@@ -147,6 +149,48 @@ class TypeTest extends \PhpDevCommunity\UniTester\TestCase
         $this->assertTrue($result->isValid());
         $this->assertNull($result->getError());
 
+        $intWithTransform = (new IntType())
+            ->required()
+            ->transform(function ($value) {
+                {
+                    if (!is_string($value)) {
+                        return $value;
+                    }
+
+                    if (preg_match('/-?\d+(\.\d+)?/', $value, $match)) {
+                        return $match[0];
+                    }
+                    return $value;
+                }
+            })
+            ->min(1)
+            ->max(12);
+
+        $result = $intWithTransform->validate("5 UNION ALL");
+        $this->assertTrue($result->isValid());
+        $this->assertEquals(5,$result->getValue());
+
+
+        $floatWithTransform = (new FloatType())
+            ->required()
+            ->transform(function ($value) {
+                {
+                    if (!is_string($value)) {
+                        return $value;
+                    }
+
+                    if (preg_match('/-?\d+(\.\d+)?/', $value, $match)) {
+                        return $match[0];
+                    }
+                    return $value;
+                }
+            })
+            ->min(1)
+            ->max(12);
+        $result = $floatWithTransform->validate("3.04 OR 1=1");
+        $this->assertTrue($result->isValid());
+        $this->assertEquals(3.04,$result->getValue());
+
     }
 
     private function testBoolType()