Browse Source

tests; lu dbo array; data access and sp modifications

develop
Robin Thoni 8 years ago
parent
commit
94b6e1417b

+ 1
- 0
.gitignore View File

@@ -1,3 +1,4 @@
1 1
 /.idea
2 2
 /vendor
3 3
 composer.lock
4
+/tests/config.json

+ 12
- 5
src/Utils/DataAccess/LuDataAccess.php View File

@@ -15,7 +15,7 @@ abstract class LuDataAccess {
15 15
     protected static $_table = "mydb_table";
16 16
     protected static $_table_as = "__t__";
17 17
     protected static $_dboClass = LuDbo::class;
18
-    protected static $_dboArrayClass = LuDbo::class;
18
+    protected static $_dboArrayClass = null;
19 19
     protected static $_dboIgnoreList = [];
20 20
 
21 21
     public static function transact(\Closure $function)
@@ -55,6 +55,14 @@ abstract class LuDataAccess {
55 55
         return static::getConnection()->table(static::$_table);
56 56
     }
57 57
 
58
+    public static function getDboClassArray()
59
+    {
60
+        if (is_null(static::$_dboArrayClass)) {
61
+            return static::$_dboClass . "[]";
62
+        }
63
+        return static::$_dboArrayClass;
64
+    }
65
+
58 66
     /**
59 67
      * @param $query Builder|callable
60 68
      * @return Builder
@@ -95,7 +103,7 @@ abstract class LuDataAccess {
95 103
         }
96 104
         $data = LuStringUtils::convertJsonString($string);
97 105
         
98
-        return call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $data)->getArray();
106
+        return LuDbo::deserializeValue($data, static::$_dboClass . "[]");
99 107
     }
100 108
 
101 109
     /**
@@ -109,13 +117,12 @@ abstract class LuDataAccess {
109 117
         if (is_null($string)) {
110 118
             return null;
111 119
         }
112
-        $json = json_decode($string, true);
113
-        $data = LuArrayUtils::snakeToCamelCase($json);
120
+        $data = LuStringUtils::convertJsonString($string);
114 121
         if (count($data) < 1) {
115 122
             return null;
116 123
         }
117 124
 
118
-        return call_user_func([static::$_dboClass, 'jsonDeserialize'], $data[0]);
125
+        return LuDbo::deserializeValue($data[0], static::$_dboClass);
119 126
     }
120 127
 
121 128
     /**

+ 24
- 8
src/Utils/DataAccess/LuStoredProcedure.php View File

@@ -12,13 +12,15 @@ use Illuminate\Database\Connection;
12 12
 use Luticate\Utils\Business\LuStringUtils;
13 13
 use Luticate\Utils\Dbo\LuDbo;
14 14
 use Illuminate\Database\Capsule\Manager as Capsule;
15
-use Luticate\Utils\Dbo\LuMultipleDbo;
15
+use Luticate\Utils\Dbo\LuDboArray;
16
+use Luticate\Utils\Dbo\LuPaginatedDbo;
16 17
 
17 18
 abstract class LuStoredProcedure
18 19
 {
19 20
     protected static $_connection = "mydb";
20 21
     protected static $_storedProcedure = "mydb_table";
21
-    protected static $_dboArrayClass = LuDbo::class;
22
+    protected static $_dboClass = LuDbo::class;
23
+    protected static $_dboArrayClass = null;
22 24
 
23 25
     /**
24 26
      * @return Connection
@@ -27,6 +29,14 @@ abstract class LuStoredProcedure
27 29
     {
28 30
         return Capsule::connection(static::$_connection);
29 31
     }
32
+    
33
+    public static function getDboClassArray()
34
+    {
35
+        if (is_null(static::$_dboArrayClass)) {
36
+            return static::$_dboClass . "[]";
37
+        }
38
+        return static::$_dboArrayClass;
39
+    }
30 40
 
31 41
     public static function getArgsArray($args)
32 42
     {
@@ -60,7 +70,10 @@ abstract class LuStoredProcedure
60 70
 
61 71
         $value = $values[0]->data;
62 72
         $json = LuStringUtils::convertJsonString($value);
63
-        $dboValues = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray();
73
+        $dboValues = LuDbo::deserializeValue($json, static::getDboClassArray());
74
+        if ($dboValues instanceof LuDboArray) {
75
+            return $dboValues->getArray();
76
+        }
64 77
         return $dboValues;
65 78
     }
66 79
 
@@ -79,15 +92,15 @@ abstract class LuStoredProcedure
79 92
         if (count($json) < 1 || is_null($json[0])) {
80 93
             return null;
81 94
         }
82
-        $dboValue = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray()[0];
83
-        return $dboValue;
95
+        $dboValue = LuDbo::deserializeValue($json, static::getDboClassArray());
96
+        return $dboValue[0];
84 97
     }
85 98
 
86 99
     /**
87 100
      * @param $args array
88 101
      * @param $page int The page number, 0 based
89 102
      * @param $perPage int The number of items per page
90
-     * @return LuMultipleDbo
103
+     * @return LuPaginatedDbo
91 104
      */
92 105
     public static function getMultiplePaginated($args, $page, $perPage)
93 106
     {
@@ -111,7 +124,10 @@ abstract class LuStoredProcedure
111 124
             $value->data = '[]';
112 125
         }
113 126
         $json = LuStringUtils::convertJsonString($value->data);
114
-        $dboValues = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray();
115
-        return new LuMultipleDbo($value->count, $dboValues);
127
+        $dboValues = LuDbo::deserializeValue($json, static::getDboClassArray());
128
+        if ($dboValues instanceof LuDboArray) {
129
+            $dboValues = $dboValues->getArray();
130
+        }
131
+        return new LuPaginatedDbo($value->count, $dboValues);
116 132
     }
117 133
 }

+ 1
- 1
src/Utils/Dbo/LuBoolDboArray.php View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-class LuBoolDboArray extends LuDbo
11
+class LuBoolDboArray extends LuDboArray
12 12
 {
13 13
     /**
14 14
      * @var int[]

+ 1
- 1
src/Utils/Dbo/LuDateTimeDboArray.php View File

@@ -10,7 +10,7 @@ namespace Luticate\Utils\Dbo;
10 10
 
11 11
 use Carbon\Carbon;
12 12
 
13
-class LuDateTimeDboArray extends LuDbo
13
+class LuDateTimeDboArray extends LuDboArray
14 14
 {
15 15
     /**
16 16
      * @var Carbon[]

+ 24
- 27
src/Utils/Dbo/LuDbo.php View File

@@ -27,33 +27,8 @@ abstract class LuDbo implements \JsonSerializable {
27 27
             $name = LuStringUtils::snakeToCamelCase($property->getName(), true);
28 28
             $property->setAccessible(true);
29 29
             $value = $property->getValue($this);
30
-            if (is_array($value)) {
31
-                $array = [];
32
-                foreach ($value as $key => $v) {
33
-                    if ($v instanceof LuDbo) {
34
-                        $array[$key] = $v->jsonSerialize();
35
-                    }
36
-                    else if ($v instanceof Carbon) {
37
-                        $array[$key] = $v->__toString();
38
-                    }
39
-                    else {
40
-                        $array[$key] = $v;
41
-                    }
42
-                }
43
-                $json[$name] = $array;
44
-            }
45
-            else {
46
-                $value = $property->getValue($this);
47
-                if ($value instanceof LuDbo) {
48
-                    $json[$name] = $value->jsonSerialize();
49
-                }
50
-                else if ($value instanceof Carbon) {
51
-                    $json[$name] = $value->__toString();
52
-                }
53
-                else {
54
-                    $json[$name] = $value;
55
-                }
56
-            }
30
+            $json[$name] = static::serializeValue($value);
31
+            
57 32
         }
58 33
         return $json;
59 34
     }
@@ -70,6 +45,28 @@ abstract class LuDbo implements \JsonSerializable {
70 45
         return $obj;
71 46
     }
72 47
 
48
+    public static function serializeValue($value)
49
+    {
50
+        if (is_array($value)) {
51
+            $array = [];
52
+            foreach ($value as $key => $v) {
53
+                $array[$key] = static::serializeValue($v);
54
+            }
55
+            return $array;
56
+        }
57
+        else {
58
+            if ($value instanceof LuDbo) {
59
+                return $value->jsonSerialize();
60
+            }
61
+            else if ($value instanceof Carbon) {
62
+                return $value->__toString();
63
+            }
64
+            else {
65
+                return $value;
66
+            }
67
+        }
68
+    }
69
+
73 70
     public static function deserializeValue($value, $type, $constraints = []) {
74 71
         if (is_null($value)) {
75 72
             return null;

+ 59
- 0
src/Utils/Dbo/LuDboArray.php View File

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

+ 1
- 1
src/Utils/Dbo/LuFloatDboArray.php View File

@@ -9,7 +9,7 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-class LuFloatDboArray extends LuDbo
12
+class LuFloatDboArray extends LuDboArray
13 13
 {
14 14
     /**
15 15
      * @var float[]

+ 1
- 1
src/Utils/Dbo/LuIntDboArray.php View File

@@ -9,7 +9,7 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-class LuIntDboArray extends LuDbo
12
+class LuIntDboArray extends LuDboArray
13 13
 {
14 14
     /**
15 15
      * @var int[]

+ 1
- 1
src/Utils/Dbo/LuMiddlewareDboArray.php View File

@@ -9,7 +9,7 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-class LuMiddlewareDboArray extends LuDbo
12
+class LuMiddlewareDboArray extends LuDboArray
13 13
 {
14 14
     /**
15 15
      * @var LuMiddlewareDbo[]

src/Utils/Dbo/LuMultipleDbo.php → src/Utils/Dbo/LuPaginatedDbo.php View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 namespace Luticate\Utils\Dbo;
4 4
 
5
-class LuMultipleDbo extends LuDbo {
5
+class LuPaginatedDbo extends LuDbo {
6 6
 
7 7
     /**
8 8
      * @var int
@@ -50,7 +50,7 @@ class LuMultipleDbo extends LuDbo {
50 50
         foreach ($this->_data as $data) {
51 51
             $values[] = $callback($data);
52 52
         }
53
-        return new LuMultipleDbo($this->_count, $values);
53
+        return new LuPaginatedDbo($this->_count, $values);
54 54
     }
55 55
     
56 56
     public static function jsonDeserialize($json)

+ 1
- 1
src/Utils/Dbo/LuRouteDboArray.php View File

@@ -9,7 +9,7 @@
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11 11
 
12
-class LuRouteDboArray extends LuDbo
12
+class LuRouteDboArray extends LuDboArray
13 13
 {
14 14
     /**
15 15
      * @var LuRouteDbo[]

+ 1
- 1
src/Utils/Dbo/LuStringDboArray.php View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 namespace Luticate\Utils\Dbo;
10 10
 
11
-class LuStringDboArray extends LuDbo
11
+class LuStringDboArray extends LuDboArray
12 12
 {
13 13
     /**
14 14
      * @var string[]

+ 45
- 28
tests/DatabaseTest.php View File

@@ -155,7 +155,6 @@ class SpTest extends LuStoredProcedure {
155 155
     protected static $_connection = "mydb";
156 156
     protected static $_storedProcedure = "sp_test";
157 157
     protected static $_dboClass = LuIntDbo::class;
158
-    protected static $_dboArrayClass = LuIntDboArray::class;
159 158
 
160 159
     public static function execute($an_int) {
161 160
         return self::getSingle([$an_int]);
@@ -185,42 +184,60 @@ class TestTableDataAccess extends LuDataAccess
185 184
     protected static $_connection = "mydb";
186 185
     protected static $_table = "test_table";
187 186
     protected static $_dboClass = TestTableDbo::class;
188
-    protected static $_dboArrayClass = TestTableDboArray::class;
189 187
 }
190 188
 
191 189
 class DatabaseTest extends \PHPUnit_Framework_TestCase
192 190
 {
193
-    public function testSetup()
194
-    {
195
-//        $config = ['databases' => [
196
-//            [
197
-//                'name'      => 'mydb',
198
-//                'driver'    => 'pgsql',
199
-//                'host'      => '172.17.0.1',
200
-//                'database'  => 'luticate2',
201
-//                'username'  => 'dev',
202
-//                'password'  => 'dev'
203
-//            ]
204
-//        ]];
205
-//        $app = new LuticateApplication($config);
206
-//        $app->setupDatabases();
207
-
208
-//        var_dump(SpTest::execute(42));
209
-//        var_dump(SpTest::execute(4));
210
-//        var_dump(SpTest2::execute(42));
211
-//        var_dump(SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
212
-//        var_dump(SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
213
-//
191
+    public function testSpGetSingle()
192
+    {
193
+        $this->assertSame(42, LuDbo::serializeValue(SpTest::execute(42)));
194
+    }
195
+
196
+    public function testSpGetSingleNull()
197
+    {
198
+        $this->assertNull(SpTest::execute(4));
199
+    }
200
+
201
+    public function testSpGetMultiple()
202
+    {
203
+        $this->assertSame(range(0, 15), SpTest2::execute(42));
204
+    }
205
+
206
+    public function testSpGetMultiplePaginated1()
207
+    {
208
+        $this->assertSame(["Count" => 16, "Data" => range(0, 1)], SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
209
+    }
210
+
211
+    public function testSpGetMultiplePaginated2()
212
+    {
213
+        $this->assertSame(["Count" => 16, "Data" => range(2, 3)], SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
214
+    }
215
+
216
+    public function testDaGetMultiple()
217
+    {
218
+        $this->assertSame([
219
+            ["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]
220
+        ],
221
+            LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
222
+            {
223
+                return $q->where("id", "=", 1);
224
+            })));
225
+    }
226
+
227
+    public function testDaGetSingleById()
228
+    {
229
+        $this->assertSame(["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"],
230
+            LuDbo::serializeValue(TestTableDataAccess::getSingleById(1)));
231
+    }
232
+    
233
+    public function testDatabase()
234
+    {
235
+
214 236
 //        $dbo = new TestTableDbo();
215 237
 //        $dbo->setId(11);
216 238
 //        $dbo->setSomeIntegerArray('{}');
217 239
 //        $dbo->setSomeText("lol2");
218 240
 //
219
-//        var_dump(TestTableDataAccess::getMultiple(function(Builder $q)
220
-//        {
221
-//            return $q->where("id", "=", 1);
222
-//        })[0]->jsonSerialize());
223
-//        var_dump(TestTableDataAccess::getSingleById(1));
224 241
 //        var_dump(TestTableDataAccess::deleteMultiple(function($q)
225 242
 //        {
226 243
 //            return $q->where("some_text", "=", "lol");

+ 5
- 11
tests/bootstrap.php View File

@@ -6,15 +6,9 @@
6 6
  * Time: 2:59 PM
7 7
  */
8 8
 
9
-class AbortException extends \Exception
10
-{
11
-    public function __construct($code, $reason)
12
-    {
13
-        parent::__construct($reason, $code, null);
14
-    }
15
-}
9
+use Luticate\Utils\Controller\LuticateApplication;
16 10
 
17
-function abort($code, $reason)
18
-{
19
-    throw new AbortException($code, $reason);
20
-}
11
+$json = file_get_contents(__DIR__ . '/config.json');
12
+$config = json_decode($json, true);
13
+$app = new LuticateApplication($config);
14
+$app->setupDatabases();

+ 14
- 0
tests/config.example.json View File

@@ -0,0 +1,14 @@
1
+{
2
+  "logs": "storage/logs/luticate.log",
3
+  "websocket": {
4
+    "address": "0.0.0.0",
5
+    "port": 8180
6
+  },
7
+  "databases": [
8
+    {
9
+      "name": "default", "driver": "pgsql",
10
+      "host": "127.0.0.1", "database": "project",
11
+      "username": "project", "password": "password"
12
+    }
13
+  ]
14
+}

Loading…
Cancel
Save