Browse Source

lufloatdbo and tests

tags/0.1.10^0
Robin Thoni 8 years ago
parent
commit
b84f281f66

+ 57
- 0
src/Utils/Dbo/LuFloatDbo.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 2/22/16
6
+ * Time: 9:25 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+use Luticate\Utils\LuBusiness;
13
+use Luticate\Utils\LuDbo;
14
+
15
+class LuFloatDbo extends LuDbo
16
+{
17
+    /**
18
+     * @var float
19
+     */
20
+    private $_value;
21
+
22
+    /**
23
+     * @return float
24
+     */
25
+    public function getFloat()
26
+    {
27
+        return $this->_value;
28
+    }
29
+
30
+    /**
31
+     * @param float $value
32
+     */
33
+    public function setFloat($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_numeric($json)) {
46
+            throw new LuDboDeserializeException("Invalid float value");
47
+        }
48
+        $val = new self();
49
+        $val->setFloat(floatval($json));
50
+        return $val;
51
+    }
52
+
53
+    public static function generateSample()
54
+    {
55
+        return 42.42;
56
+    }
57
+}

+ 57
- 0
src/Utils/Dbo/LuFloatDboArray.php View File

@@ -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 LuFloatDboArray extends LuDbo
16
+{
17
+    /**
18
+     * @var float[]
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
+            throw new LuDboDeserializeException("Invalid array value");
39
+        }
40
+        $dbo = new self();
41
+        $array = [];
42
+        foreach ($json as $data) {
43
+            $array[] = LuFloatDbo::jsonDeserialize($data)->getFloat();
44
+        }
45
+        $dbo->setArray($array);
46
+        return $dbo;
47
+    }
48
+
49
+    public static function generateSample()
50
+    {
51
+        return [
52
+            LuFloatDbo::generateSample(),
53
+            LuFloatDbo::generateSample()
54
+        ];
55
+    }
56
+
57
+}

+ 140
- 0
tests/LuFloatDboArrayTest.php View File

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

+ 108
- 0
tests/LuFloatDboTest.php View File

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

Loading…
Cancel
Save