Bladeren bron

lubooldbo and tests

tags/0.1.10
Robin Thoni 8 jaren geleden
bovenliggende
commit
24e37cf285
4 gewijzigde bestanden met toevoegingen van 356 en 4 verwijderingen
  1. 5
    4
      src/Utils/Dbo/LuBoolDbo.php
  2. 57
    0
      src/Utils/Dbo/LuBoolDboArray.php
  3. 164
    0
      tests/LuBoolDboArrayTest.php
  4. 130
    0
      tests/LuBoolDboTest.php

+ 5
- 4
src/Utils/Dbo/LuBoolDbo.php Bestand weergeven

@@ -46,9 +46,10 @@ class LuBoolDbo extends LuDbo
46 46
         if (is_bool($json)) {
47 47
             $bool = $json;
48 48
         }
49
-        if (is_string($json)) {
50
-            if (strtolower($json) === "true" || strtolower($json) === "false") {
51
-                $bool = (strtolower($json) === "true");
49
+        else if (is_string($json)) {
50
+            $json = strtolower($json);
51
+            if ($json == "true" || $json == "false") {
52
+                $bool = ($json == "true");
52 53
             }
53 54
         }
54 55
         if (is_null($bool)) {
@@ -61,6 +62,6 @@ class LuBoolDbo extends LuDbo
61 62
 
62 63
     public static function generateSample()
63 64
     {
64
-        return "sample string";
65
+        return true;
65 66
     }
66 67
 }

+ 57
- 0
src/Utils/Dbo/LuBoolDboArray.php Bestand weergeven

@@ -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 LuBoolDboArray extends LuDbo
16
+{
17
+    /**
18
+     * @var int[]
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[] = LuBoolDbo::jsonDeserialize($data)->getBool();
44
+        }
45
+        $dbo->setArray($array);
46
+        return $dbo;
47
+    }
48
+
49
+    public static function generateSample()
50
+    {
51
+        return [
52
+            LuBoolDbo::generateSample(),
53
+            LuBoolDbo::generateSample()
54
+        ];
55
+    }
56
+
57
+}

+ 164
- 0
tests/LuBoolDboArrayTest.php Bestand weergeven

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

+ 130
- 0
tests/LuBoolDboTest.php Bestand weergeven

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

Laden…
Annuleren
Opslaan