浏览代码

LuIntDBo fixes and LuStringDbo; tests

tags/0.1.10
Robin Thoni 8 年前
父节点
当前提交
79ab3e045e

+ 3
- 0
composer.json 查看文件

@@ -10,6 +10,9 @@
10 10
     "require": {
11 11
     "guzzlehttp/guzzle": "^6.1"
12 12
     },
13
+    "require-dev": {
14
+        "phpunit/phpunit": "5.3.*"
15
+    },
13 16
     "autoload": {
14 17
       "psr-4": {
15 18
         "Luticate\\": "src/"

+ 1
- 1
src/Utils/Dbo/LuIntDbo.php 查看文件

@@ -45,7 +45,7 @@ class LuIntDbo extends LuDbo
45 45
         if (!is_numeric($json) || intval($json) != $json) {
46 46
             LuBusiness::badInput("Invalid integer value");
47 47
         }
48
-        $val = new LuIntDBo();
48
+        $val = new self();
49 49
         $val->setInt(intval($json));
50 50
         return $val;
51 51
     }

+ 5
- 1
src/Utils/Dbo/LuIntDboArray.php 查看文件

@@ -9,6 +9,7 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
+use Luticate\Utils\LuBusiness;
12 13
 use Luticate\Utils\LuDbo;
13 14
 
14 15
 class LuIntDboArray extends LuDbo
@@ -33,7 +34,10 @@ class LuIntDboArray extends LuDbo
33 34
 
34 35
     public static function jsonDeserialize($json)
35 36
     {
36
-        $dbo = new LuIntDboArray();
37
+        if (!is_array($json)) {
38
+            LuBusiness::badInput("Invalid array value");
39
+        }
40
+        $dbo = new self();
37 41
         $array = [];
38 42
         foreach ($json as $data) {
39 43
             $array[] = LuIntDbo::jsonDeserialize($data)->getInt();

+ 57
- 0
src/Utils/Dbo/LuStringDbo.php 查看文件

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 2:41 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+use Luticate\Utils\LuBusiness;
13
+use Luticate\Utils\LuDbo;
14
+
15
+class LuStringDbo extends LuDbo
16
+{
17
+    /**
18
+     * @var $_value string
19
+     */
20
+    private $_value;
21
+
22
+    /**
23
+     * @return string
24
+     */
25
+    public function getString()
26
+    {
27
+        return $this->_value;
28
+    }
29
+
30
+    /**
31
+     * @param string $value
32
+     */
33
+    public function setString($value)
34
+    {
35
+        $this->_value = $value;
36
+    }
37
+
38
+    function jsonSerialize()
39
+    {
40
+        return $this->_value;
41
+    }
42
+
43
+    public static function jsonDeserialize($json)
44
+    {
45
+        if (is_object($json) || is_array($json) || is_bool($json) || is_null($json)) {
46
+            LuBusiness::badInput("Invalid string value");
47
+        }
48
+        $val = new self();
49
+        $val->setString($json . "");
50
+        return $val;
51
+    }
52
+
53
+    public static function generateSample()
54
+    {
55
+        return "sample string";
56
+    }
57
+}

+ 57
- 0
src/Utils/Dbo/LuStringDboArray.php 查看文件

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 2/22/16
6
+ * Time: 9:40 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+use Luticate\Utils\LuBusiness;
13
+use Luticate\Utils\LuDbo;
14
+
15
+class LuStringDboArray extends LuDbo
16
+{
17
+    /**
18
+     * @var string[]
19
+     */
20
+    protected $_array;
21
+    public function getArray()
22
+    {
23
+        return $this->_array;
24
+    }
25
+    public function setArray($value)
26
+    {
27
+        $this->_array = $value;
28
+    }
29
+
30
+    public function jsonSerialize()
31
+    {
32
+        return $this->_array;
33
+    }
34
+
35
+    public static function jsonDeserialize($json)
36
+    {
37
+        if (!is_array($json)) {
38
+            LuBusiness::badInput("Invalid array value");
39
+        }
40
+        $dbo = new self();
41
+        $array = [];
42
+        foreach ($json as $data) {
43
+            $array[] = LuStringDbo::jsonDeserialize($data)->getString();
44
+        }
45
+        $dbo->setArray($array);
46
+        return $dbo;
47
+    }
48
+
49
+    public static function generateSample()
50
+    {
51
+        return [
52
+            LuStringDbo::generateSample(),
53
+            LuStringDbo::generateSample()
54
+        ];
55
+    }
56
+
57
+}

+ 1
- 1
src/Utils/LuDbo.php 查看文件

@@ -25,7 +25,7 @@ abstract class LuDbo implements \JsonSerializable {
25 25
 
26 26
     /**
27 27
      * Deserialize from a JSON object
28
-     * @param $json array The JSON data to deserialize
28
+     * @param $json mixed The JSON data to deserialize
29 29
      * @return LuDbo
30 30
      */
31 31
     public static function jsonDeserialize($json)

+ 127
- 0
test/LuIntDboArrayTest.php 查看文件

@@ -0,0 +1,127 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuIntDboArray;
3
+use Luticate\Utils\Dbo\LuIntDbo;
4
+
5
+/**
6
+ * Created by PhpStorm.
7
+ * User: robin
8
+ * Date: 5/29/16
9
+ * Time: 2:57 PM
10
+ */
11
+
12
+class LuIntDboArrayTest extends \PHPUnit_Framework_TestCase
13
+{
14
+
15
+    public function testArrayEmpty()
16
+    {
17
+        $dbo = LuIntDboArray::jsonDeserialize([]);
18
+        $this->assertSame([], $dbo->getArray());
19
+    }
20
+    
21
+    public function testArrayString()
22
+    {
23
+        $dbo = LuIntDboArray::jsonDeserialize(["42"]);
24
+        $this->assertSame([42], $dbo->getArray());
25
+    }
26
+
27
+    public function testArrayMultipleString()
28
+    {
29
+        $dbo = LuIntDboArray::jsonDeserialize(["42", "24", "48"]);
30
+        $this->assertSame([42, 24, 48], $dbo->getArray());
31
+    }
32
+
33
+    public function testArrayInt()
34
+    {
35
+        $dbo = LuIntDboArray::jsonDeserialize([42]);
36
+        $this->assertSame([42], $dbo->getArray());
37
+    }
38
+
39
+    public function testArrayMultipleInt()
40
+    {
41
+        $dbo = LuIntDboArray::jsonDeserialize([42, 24, 48]);
42
+        $this->assertSame([42, 24, 48], $dbo->getArray());
43
+    }
44
+
45
+    public function testArrayStringIntPlusData()
46
+    {
47
+        $this->expectException(AbortException::class);
48
+        LuIntDboArray::jsonDeserialize(["42test"]);
49
+    }
50
+
51
+    public function testArrayMultipleStringIntPlusData()
52
+    {
53
+        $this->expectException(AbortException::class);
54
+        LuIntDboArray::jsonDeserialize(["42", 42, "42test"]);
55
+    }
56
+    
57
+    public function testIntString()
58
+    {
59
+        $this->expectException(AbortException::class);
60
+        LuIntDboArray::jsonDeserialize("42");
61
+    }
62
+    
63
+    public function testInt()
64
+    {
65
+        $this->expectException(AbortException::class);
66
+        LuIntDboArray::jsonDeserialize(42);
67
+    }
68
+    public function testIntNegativeString()
69
+    {
70
+        $this->expectException(AbortException::class);
71
+        LuIntDboArray::jsonDeserialize("-42");
72
+    }
73
+
74
+    public function testIntNegative()
75
+    {
76
+        $this->expectException(AbortException::class);
77
+        LuIntDboArray::jsonDeserialize(-42);
78
+    }
79
+
80
+    public function testFloat()
81
+    {
82
+        $this->expectException(AbortException::class);
83
+        LuIntDboArray::jsonDeserialize(42.42);
84
+    }
85
+
86
+    public function testFloatString()
87
+    {
88
+        $this->expectException(AbortException::class);
89
+        LuIntDboArray::jsonDeserialize("42.42");
90
+    }
91
+
92
+    public function testExponentialString()
93
+    {
94
+        $this->expectException(AbortException::class);
95
+        LuIntDboArray::jsonDeserialize("42e+01");
96
+    }
97
+
98
+    public function testBool()
99
+    {
100
+        $this->expectException(AbortException::class);
101
+        LuIntDboArray::jsonDeserialize(true);
102
+    }
103
+
104
+    public function testBoolString()
105
+    {
106
+        $this->expectException(AbortException::class);
107
+        LuIntDboArray::jsonDeserialize("true");
108
+    }
109
+
110
+    public function testArrayFloat()
111
+    {
112
+        $this->expectException(AbortException::class);
113
+        LuIntDboArray::jsonDeserialize([42.42]);
114
+    }
115
+
116
+    public function testNull()
117
+    {
118
+        $this->expectException(AbortException::class);
119
+        LuIntDboArray::jsonDeserialize(null);
120
+    }
121
+
122
+    public function testObject()
123
+    {
124
+        $this->expectException(AbortException::class);
125
+        LuIntDboArray::jsonDeserialize(new self());
126
+    }
127
+}

+ 107
- 0
test/LuIntDboTest.php 查看文件

@@ -0,0 +1,107 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuIntDbo;
3
+
4
+/**
5
+ * Created by PhpStorm.
6
+ * User: robin
7
+ * Date: 5/29/16
8
+ * Time: 2:57 PM
9
+ */
10
+
11
+class LuIntDboTest extends \PHPUnit_Framework_TestCase
12
+{
13
+    public function testIntString()
14
+    {
15
+        $dbo = LuIntDbo::jsonDeserialize("42");
16
+        $this->assertSame(42, $dbo->getInt());
17
+    }
18
+    
19
+    public function testInt()
20
+    {
21
+        $dbo = LuIntDbo::jsonDeserialize(42);
22
+        $this->assertSame(42, $dbo->getInt());
23
+    }
24
+    public function testIntNegativeString()
25
+    {
26
+        $dbo = LuIntDbo::jsonDeserialize("-42");
27
+        $this->assertSame(-42, $dbo->getInt());
28
+    }
29
+
30
+    public function testIntNegative()
31
+    {
32
+        $dbo = LuIntDbo::jsonDeserialize(-42);
33
+        $this->assertSame(-42, $dbo->getInt());
34
+    }
35
+
36
+    public function testStringIntPlusData()
37
+    {
38
+        $this->expectException(AbortException::class);
39
+        LuIntDbo::jsonDeserialize("42test");
40
+    }
41
+
42
+    public function testFloat()
43
+    {
44
+        $this->expectException(AbortException::class);
45
+        LuIntDbo::jsonDeserialize(42.42);
46
+    }
47
+
48
+    public function testFloatString()
49
+    {
50
+        $this->expectException(AbortException::class);
51
+        LuIntDbo::jsonDeserialize("42.42");
52
+    }
53
+
54
+    public function testExponentialString()
55
+    {
56
+        $this->expectException(AbortException::class);
57
+        LuIntDbo::jsonDeserialize("42e+01");
58
+    }
59
+
60
+    public function testBool()
61
+    {
62
+        $this->expectException(AbortException::class);
63
+        LuIntDbo::jsonDeserialize(true);
64
+    }
65
+
66
+    public function testBoolString()
67
+    {
68
+        $this->expectException(AbortException::class);
69
+        LuIntDbo::jsonDeserialize("true");
70
+    }
71
+
72
+    public function testArrayEmpty()
73
+    {
74
+        $this->expectException(AbortException::class);
75
+        LuIntDbo::jsonDeserialize([]);
76
+    }
77
+
78
+    public function testArrayString()
79
+    {
80
+        $this->expectException(AbortException::class);
81
+        LuIntDbo::jsonDeserialize(["42"]);
82
+    }
83
+
84
+    public function testArrayInt()
85
+    {
86
+        $this->expectException(AbortException::class);
87
+        LuIntDbo::jsonDeserialize([42]);
88
+    }
89
+
90
+    public function testArrayFloat()
91
+    {
92
+        $this->expectException(AbortException::class);
93
+        LuIntDbo::jsonDeserialize([42.42]);
94
+    }
95
+
96
+    public function testNull()
97
+    {
98
+        $this->expectException(AbortException::class);
99
+        LuIntDbo::jsonDeserialize(null);
100
+    }
101
+
102
+    public function testObject()
103
+    {
104
+        $this->expectException(AbortException::class);
105
+        LuIntDbo::jsonDeserialize(new self());
106
+    }
107
+}

+ 133
- 0
test/LuStringDboArrayTest.php 查看文件

@@ -0,0 +1,133 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuStringDboArray;
3
+use Luticate\Utils\Dbo\LuStringDbo;
4
+
5
+/**
6
+ * Created by PhpStorm.
7
+ * User: robin
8
+ * Date: 5/29/16
9
+ * Time: 2:57 PM
10
+ */
11
+
12
+class LuStringDboArrayTest extends \PHPUnit_Framework_TestCase
13
+{
14
+
15
+    public function testArrayEmpty()
16
+    {
17
+        $dbo = LuStringDboArray::jsonDeserialize([]);
18
+        $this->assertSame([], $dbo->getArray());
19
+    }
20
+    
21
+    public function testArrayString()
22
+    {
23
+        $dbo = LuStringDboArray::jsonDeserialize(["42"]);
24
+        $this->assertSame(["42"], $dbo->getArray());
25
+    }
26
+
27
+    public function testArrayMultipleString()
28
+    {
29
+        $dbo = LuStringDboArray::jsonDeserialize(["42", "24", "48"]);
30
+        $this->assertSame(["42", "24", "48"], $dbo->getArray());
31
+    }
32
+
33
+    public function testArrayInt()
34
+    {
35
+        $dbo = LuStringDboArray::jsonDeserialize([42]);
36
+        $this->assertSame(["42"], $dbo->getArray());
37
+    }
38
+
39
+    public function testArrayMultipleInt()
40
+    {
41
+        $dbo = LuStringDboArray::jsonDeserialize([42, 24, 48]);
42
+        $this->assertSame(["42", "24", "48"], $dbo->getArray());
43
+    }
44
+
45
+    public function testArrayStringIntPlusData()
46
+    {
47
+        $dbo = LuStringDboArray::jsonDeserialize(["42test"]);
48
+        $this->assertSame(["42test"], $dbo->getArray());
49
+    }
50
+
51
+    public function testArrayMultipleStringIntPlusData()
52
+    {
53
+        $dbo = LuStringDboArray::jsonDeserialize(["42", 42, "42test"]);
54
+        $this->assertSame(["42", "42", "42test"], $dbo->getArray());
55
+    }
56
+
57
+    public function testArrayFloat()
58
+    {
59
+        $dbo = LuStringDboArray::jsonDeserialize([42.42]);
60
+        $this->assertSame(["42.42"], $dbo->getArray());
61
+    }
62
+
63
+    public function testArrayMultipleTypes()
64
+    {
65
+        $this->expectException(AbortException::class);
66
+        LuStringDboArray::jsonDeserialize(["42", 42, "42test", null]);
67
+    }
68
+    
69
+    public function testIntString()
70
+    {
71
+        $this->expectException(AbortException::class);
72
+        LuStringDboArray::jsonDeserialize("42");
73
+    }
74
+    
75
+    public function testInt()
76
+    {
77
+        $this->expectException(AbortException::class);
78
+        LuStringDboArray::jsonDeserialize(42);
79
+    }
80
+    public function testIntNegativeString()
81
+    {
82
+        $this->expectException(AbortException::class);
83
+        LuStringDboArray::jsonDeserialize("-42");
84
+    }
85
+
86
+    public function testIntNegative()
87
+    {
88
+        $this->expectException(AbortException::class);
89
+        LuStringDboArray::jsonDeserialize(-42);
90
+    }
91
+
92
+    public function testFloat()
93
+    {
94
+        $this->expectException(AbortException::class);
95
+        LuStringDboArray::jsonDeserialize(42.42);
96
+    }
97
+
98
+    public function testFloatString()
99
+    {
100
+        $this->expectException(AbortException::class);
101
+        LuStringDboArray::jsonDeserialize("42.42");
102
+    }
103
+
104
+    public function testExponentialString()
105
+    {
106
+        $this->expectException(AbortException::class);
107
+        LuStringDboArray::jsonDeserialize("42e+01");
108
+    }
109
+
110
+    public function testBool()
111
+    {
112
+        $this->expectException(AbortException::class);
113
+        LuStringDboArray::jsonDeserialize(true);
114
+    }
115
+
116
+    public function testBoolString()
117
+    {
118
+        $this->expectException(AbortException::class);
119
+        LuStringDboArray::jsonDeserialize("true");
120
+    }
121
+
122
+    public function testNull()
123
+    {
124
+        $this->expectException(AbortException::class);
125
+        LuStringDboArray::jsonDeserialize(null);
126
+    }
127
+
128
+    public function testObject()
129
+    {
130
+        $this->expectException(AbortException::class);
131
+        LuStringDboArray::jsonDeserialize(new self());
132
+    }
133
+}

+ 107
- 0
test/LuStringDboTest.php 查看文件

@@ -0,0 +1,107 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuStringDbo;
3
+
4
+/**
5
+ * Created by PhpStorm.
6
+ * User: robin
7
+ * Date: 5/29/16
8
+ * Time: 2:57 PM
9
+ */
10
+
11
+class LuStringDboTest extends \PHPUnit_Framework_TestCase
12
+{
13
+    public function testIntString()
14
+    {
15
+        $dbo = LuStringDbo::jsonDeserialize("42");
16
+        $this->assertSame("42", $dbo->getString());
17
+    }
18
+    
19
+    public function testInt()
20
+    {
21
+        $dbo = LuStringDbo::jsonDeserialize(42);
22
+        $this->assertSame("42", $dbo->getString());
23
+    }
24
+    public function testIntNegativeString()
25
+    {
26
+        $dbo = LuStringDbo::jsonDeserialize("-42");
27
+        $this->assertSame("-42", $dbo->getString());
28
+    }
29
+
30
+    public function testIntNegative()
31
+    {
32
+        $dbo = LuStringDbo::jsonDeserialize(-42);
33
+        $this->assertSame("-42", $dbo->getString());
34
+    }
35
+
36
+    public function testStringIntPlusData()
37
+    {
38
+        $dbo = LuStringDbo::jsonDeserialize("42test");
39
+        $this->assertSame("42test", $dbo->getString());
40
+    }
41
+
42
+    public function testFloat()
43
+    {
44
+        $dbo = LuStringDbo::jsonDeserialize(42.42);
45
+        $this->assertSame("42.42", $dbo->getString());
46
+    }
47
+
48
+    public function testFloatString()
49
+    {
50
+        $dbo = LuStringDbo::jsonDeserialize("42.42");
51
+        $this->assertSame("42.42", $dbo->getString());
52
+    }
53
+
54
+    public function testExponentialString()
55
+    {
56
+        $dbo = LuStringDbo::jsonDeserialize("42e+01");
57
+        $this->assertSame("42e+01", $dbo->getString());
58
+    }
59
+
60
+    public function testBool()
61
+    {
62
+        $this->expectException(AbortException::class);
63
+        LuStringDbo::jsonDeserialize(true);
64
+    }
65
+
66
+    public function testBoolString()
67
+    {
68
+        $dbo = LuStringDbo::jsonDeserialize("true");
69
+        $this->assertSame("true", $dbo->getString());
70
+    }
71
+
72
+    public function testArrayEmpty()
73
+    {
74
+        $this->expectException(AbortException::class);
75
+        LuStringDbo::jsonDeserialize([]);
76
+    }
77
+
78
+    public function testArrayString()
79
+    {
80
+        $this->expectException(AbortException::class);
81
+        LuStringDbo::jsonDeserialize(["42"]);
82
+    }
83
+
84
+    public function testArrayInt()
85
+    {
86
+        $this->expectException(AbortException::class);
87
+        LuStringDbo::jsonDeserialize([42]);
88
+    }
89
+
90
+    public function testArrayFloat()
91
+    {
92
+        $this->expectException(AbortException::class);
93
+        LuStringDbo::jsonDeserialize([42.42]);
94
+    }
95
+
96
+    public function testNull()
97
+    {
98
+        $this->expectException(AbortException::class);
99
+        LuStringDbo::jsonDeserialize(null);
100
+    }
101
+
102
+    public function testObject()
103
+    {
104
+        $this->expectException(AbortException::class);
105
+        LuStringDbo::jsonDeserialize(new self());
106
+    }
107
+}

+ 20
- 0
test/bootstrap.php 查看文件

@@ -0,0 +1,20 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 2:59 PM
7
+ */
8
+
9
+class AbortException extends \Exception
10
+{
11
+    public function __construct($code, $reason)
12
+    {
13
+        parent::__construct($reason, $code, null);
14
+    }
15
+}
16
+
17
+function abort($code, $reason)
18
+{
19
+    throw new AbortException($code, $reason);
20
+}

正在加载...
取消
保存