Robin Thoni 8 роки тому
джерело
коміт
3e409511d3
2 змінених файлів з 228 додано та 0 видалено
  1. 9
    0
      tests/LuDboDeserializeTest.php
  2. 219
    0
      tests/LuRouteTest.php

+ 9
- 0
tests/LuDboDeserializeTest.php Переглянути файл

@@ -1,5 +1,6 @@
1 1
 <?php
2 2
 use Luticate\Utils\Dbo\LuDbo;
3
+use Luticate\Utils\Dbo\LuDboConstraintException;
3 4
 
4 5
 /**
5 6
  * Created by PhpStorm.
@@ -30,6 +31,13 @@ class TestDbo extends LuDbo {
30 31
     {
31 32
         $this->_test = $test;
32 33
     }
34
+    
35
+    public function ensure42()
36
+    {
37
+        if ($this->_test != "42") {
38
+            throw new LuDboConstraintException("Only 42 is allowed");
39
+        }
40
+    }
33 41
 
34 42
 }
35 43
 
@@ -42,6 +50,7 @@ class TestDbo2 extends LuDbo {
42 50
 
43 51
     /**
44 52
      * @var $_test3 TestDbo
53
+     * @nullable
45 54
      */
46 55
     private $_test3;
47 56
 

+ 219
- 0
tests/LuRouteTest.php Переглянути файл

@@ -0,0 +1,219 @@
1
+<?php
2
+use Luticate\Utils\Business\LuBusinessException;
3
+use Luticate\Utils\Controller\LuRoute;
4
+use Luticate\Utils\Dbo\LuDboConstraintException;
5
+use Luticate\Utils\Dbo\LuRouteDbo;
6
+use Luticate\Utils\Dbo\LuStringDbo;
7
+use Luticate\Utils\Middleware\LuAbstractMiddleware;
8
+
9
+/**
10
+ * Created by PhpStorm.
11
+ * User: robin
12
+ * Date: 6/7/16
13
+ * Time: 2:33 AM
14
+ */
15
+
16
+class BasicTestController
17
+{
18
+    public function testBasic()
19
+    {
20
+        return "Test. succeeded";
21
+    }
22
+    
23
+    public function testParameter($test)
24
+    {
25
+        return $test;
26
+    }
27
+
28
+    public function testTypedParameter(TestDbo $test)
29
+    {
30
+        return $test->getTest();
31
+    }
32
+
33
+    /**
34
+     * @param TestDbo $test
35
+     * @ensure42
36
+     * @return string
37
+     */
38
+    public function testTypedParameterDoc(TestDbo $test)
39
+    {
40
+        return $test->getTest();
41
+    }
42
+
43
+    /**
44
+     * @param TestDbo $test
45
+     * @nullable
46
+     * @return string
47
+     */
48
+    public function testNullable(TestDbo $test = null)
49
+    {
50
+        return is_null($test);
51
+    }
52
+
53
+    public function testBeforeMiddleware(LuStringDbo $_user)
54
+    {
55
+        return $_user->getString();
56
+    }
57
+
58
+    public function testAfterMiddleware()
59
+    {
60
+        return "A result";
61
+    }
62
+}
63
+
64
+class BeforeMiddleware extends LuAbstractMiddleware {
65
+
66
+    public function onBefore(LuRouteDbo $route, $parameters)
67
+    {
68
+        $parameters["_user"] = "\"my user\"";
69
+        return $parameters;
70
+    }
71
+
72
+    public function onAfter(LuRouteDbo $route, $parameters, $result)
73
+    {
74
+        return $result;
75
+    }
76
+}
77
+
78
+class AfterMiddleware extends LuAbstractMiddleware {
79
+
80
+    public function onBefore(LuRouteDbo $route, $parameters)
81
+    {
82
+        return $parameters;
83
+    }
84
+
85
+    public function onAfter(LuRouteDbo $route, $parameters, $result)
86
+    {
87
+        return "AfterMiddleware";
88
+    }
89
+}
90
+
91
+class AuthenticationMiddleware extends LuAbstractMiddleware {
92
+
93
+    public function onBefore(LuRouteDbo $route, $parameters)
94
+    {
95
+        $permissions = $parameters['_middleware']['permissions'] ?? [];
96
+        if ($permissions[0] != "ALLOWED") {
97
+            throw new LuBusinessException("Unauthorized", 401);
98
+        }
99
+        return $parameters;
100
+    }
101
+
102
+    public function onAfter(LuRouteDbo $route, $parameters, $result)
103
+    {
104
+        return $result;
105
+    }
106
+}
107
+
108
+class LuRouteTest extends \PHPUnit_Framework_TestCase
109
+{
110
+    public function testBasic()
111
+    {
112
+        $router = new LuRoute();
113
+        $router->addRoute("GET", "/test", '\BasicTestController', "testBasic");
114
+        $router->setup();
115
+        $res = $router->dispatch("GET", "/test", []);
116
+        $this->assertSame("Test. succeeded", $res);
117
+    }
118
+    
119
+    public function testParameter()
120
+    {
121
+        $router = new LuRoute();
122
+        $router->addRoute("GET", "/test", '\BasicTestController', "testParameter");
123
+        $router->setup();
124
+        $res = $router->dispatch("GET", "/test", ["test" => "A value"]);
125
+        $this->assertSame("A value", $res);
126
+    }
127
+
128
+    public function testTypedParameter()
129
+    {
130
+        $router = new LuRoute();
131
+        $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter");
132
+        $router->setup();
133
+        $res = $router->dispatch("GET", "/test", ["test" => '{"Test": "Some value"}']);
134
+        $this->assertSame("Some value", $res);
135
+    }
136
+
137
+    public function testTypedParameterDoc()
138
+    {
139
+        $router = new LuRoute();
140
+        $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
141
+        $router->setup();
142
+        $res = $router->dispatch("GET", "/test", ["test" => '{"Test": "42"}']);
143
+        $this->assertSame("42", $res);
144
+    }
145
+
146
+    public function testNullable()
147
+    {
148
+        $router = new LuRoute();
149
+        $router->addRoute("GET", "/test", '\BasicTestController', "testNullable");
150
+        $router->setup();
151
+        $res = $router->dispatch("GET", "/test", ["test" => 'null']);
152
+        $this->assertSame(true, $res);
153
+    }
154
+
155
+    public function testBeforeMiddleware()
156
+    {
157
+        $router = new LuRoute();
158
+        $router->addRoute("GET", "/test", '\BasicTestController', "testBeforeMiddleware", [], ['BeforeMiddleware']);
159
+        $router->setup();
160
+        $res = $router->dispatch("GET", "/test", []);
161
+        $this->assertSame("my user", $res);
162
+    }
163
+
164
+    public function testAfterMiddleware()
165
+    {
166
+        $router = new LuRoute();
167
+        $router->addRoute("GET", "/test", '\BasicTestController', "testAfterMiddleware", [], ['AfterMiddleware']);
168
+        $router->setup();
169
+        $res = $router->dispatch("GET", "/test", []);
170
+        $this->assertSame("AfterMiddleware", $res);
171
+    }
172
+
173
+    public function testAuthenticationMiddlewareAllowed()
174
+    {
175
+        $router = new LuRoute();
176
+        $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
177
+            ["permissions" => ["ALLOWED"]], ['AuthenticationMiddleware']);
178
+        $router->setup();
179
+        $res = $router->dispatch("GET", "/test", []);
180
+        $this->assertSame("Test. succeeded", $res);
181
+    }
182
+
183
+    public function testAuthenticationMiddlewareNotAllowed()
184
+    {
185
+        $this->expectException(LuBusinessException::class);
186
+        $router = new LuRoute();
187
+        $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
188
+            ["permissions" => ["NOT_ALLOWED"]], ['AuthenticationMiddleware']);
189
+        $router->setup();
190
+        $router->dispatch("GET", "/test", []);
191
+    }
192
+
193
+    public function testTypedParameterDocConstraint()
194
+    {
195
+        $this->expectException(LuBusinessException::class);
196
+        $router = new LuRoute();
197
+        $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
198
+        $router->setup();
199
+        $router->dispatch("GET", "/test", ["test" => '{"Test": "Test."}']);
200
+    }
201
+
202
+    public function testMethodNotAllowed()
203
+    {
204
+        $this->expectException(LuBusinessException::class);
205
+        $router = new LuRoute();
206
+        $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
207
+        $router->setup();
208
+        $router->dispatch("POST", "/test", ["test" => '{"Test": "Test."}']);
209
+    }
210
+
211
+    public function testRouteNotFound()
212
+    {
213
+        $this->expectException(LuBusinessException::class);
214
+        $router = new LuRoute();
215
+        $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
216
+        $router->setup();
217
+        $router->dispatch("GET", "/notfound", ["test" => '{"Test": "Test."}']);
218
+    }
219
+}

Завантаження…
Відмінити
Зберегти