|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Tests\TestRequestHandler; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Jaxon; |
|
6
|
|
|
use Jaxon\Exception\RequestException; |
|
7
|
|
|
use Jaxon\Exception\SetupException; |
|
8
|
|
|
use Nyholm\Psr7Server\ServerRequestCreator; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class ClassTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @throws SetupException |
|
17
|
|
|
*/ |
|
18
|
|
|
public function setUp(): void |
|
19
|
|
|
{ |
|
20
|
|
|
jaxon()->setOption('core.response.send', false); |
|
21
|
|
|
jaxon()->register(Jaxon::CALLABLE_CLASS, 'Sample', __DIR__ . '/../src/sample.php'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @throws SetupException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function tearDown(): void |
|
28
|
|
|
{ |
|
29
|
|
|
jaxon()->reset(); |
|
30
|
|
|
parent::tearDown(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws RequestException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testGetRequestToJaxonClass() |
|
37
|
|
|
{ |
|
38
|
|
|
// The server request |
|
39
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
40
|
|
|
return $c->g(ServerRequestCreator::class) |
|
41
|
|
|
->fromGlobals() |
|
42
|
|
|
->withQueryParams([ |
|
43
|
|
|
'jxncall' => json_encode([ |
|
44
|
|
|
'type' => 'class', |
|
45
|
|
|
'name' => 'Sample', |
|
46
|
|
|
'method' => 'myMethod', |
|
47
|
|
|
'args' => [], |
|
48
|
|
|
]), |
|
49
|
|
|
]); |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
53
|
|
|
$this->assertFalse(jaxon()->di()->getCallableFunctionPlugin()->canProcessRequest(jaxon()->di()->getRequest())); |
|
54
|
|
|
$this->assertTrue(jaxon()->di()->getCallableClassPlugin()->canProcessRequest(jaxon()->di()->getRequest())); |
|
55
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
56
|
|
|
jaxon()->di()->getCallableClassPlugin()->processRequest(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @throws RequestException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testPostRequestToJaxonClass() |
|
63
|
|
|
{ |
|
64
|
|
|
// The server request |
|
65
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
66
|
|
|
return $c->g(ServerRequestCreator::class) |
|
67
|
|
|
->fromGlobals() |
|
68
|
|
|
->withParsedBody([ |
|
69
|
|
|
'jxncall' => json_encode([ |
|
70
|
|
|
'type' => 'class', |
|
71
|
|
|
'name' => 'Sample', |
|
72
|
|
|
'method' => 'myMethod', |
|
73
|
|
|
'args' => [], |
|
74
|
|
|
]), |
|
75
|
|
|
]); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
79
|
|
|
$this->assertFalse(jaxon()->di()->getCallableFunctionPlugin()->canProcessRequest(jaxon()->di()->getRequest())); |
|
80
|
|
|
$this->assertTrue(jaxon()->di()->getCallableClassPlugin()->canProcessRequest(jaxon()->di()->getRequest())); |
|
81
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
82
|
|
|
jaxon()->di()->getCallableClassPlugin()->processRequest(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws SetupException |
|
87
|
|
|
* @throws RequestException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testRequestToJaxonClass() |
|
90
|
|
|
{ |
|
91
|
|
|
// The server request |
|
92
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
93
|
|
|
return $c->g(ServerRequestCreator::class) |
|
94
|
|
|
->fromGlobals() |
|
95
|
|
|
->withParsedBody([ |
|
96
|
|
|
'jxncall' => json_encode([ |
|
97
|
|
|
'type' => 'class', |
|
98
|
|
|
'name' => 'Sample', |
|
99
|
|
|
'method' => 'myMethod', |
|
100
|
|
|
'args' => [], |
|
101
|
|
|
]), |
|
102
|
|
|
]); |
|
103
|
|
|
}); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
106
|
|
|
jaxon()->processRequest(); |
|
107
|
|
|
$this->assertNotNull(jaxon()->getResponse()); |
|
108
|
|
|
$this->assertEquals(1, jaxon()->getResponse()->getCommandCount()); |
|
109
|
|
|
$xCallableObject = jaxon()->di()->getCallableClassPlugin()->getCallable('Sample'); |
|
110
|
|
|
$this->assertEquals('Sample', $xCallableObject->getClassName()); |
|
111
|
|
|
|
|
112
|
|
|
$xTarget = jaxon()->di()->getCallableClassPlugin()->getTarget(); |
|
113
|
|
|
$this->assertNotNull($xTarget); |
|
114
|
|
|
$this->assertTrue($xTarget->isClass()); |
|
115
|
|
|
$this->assertFalse($xTarget->isFunction()); |
|
116
|
|
|
$this->assertEquals('Sample', $xTarget->getClassName()); |
|
117
|
|
|
$this->assertEquals('myMethod', $xTarget->getMethodName()); |
|
118
|
|
|
$this->assertEquals('', $xTarget->getFunctionName()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @throws SetupException |
|
123
|
|
|
* @throws RequestException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testRequestWithIncorrectClassName() |
|
126
|
|
|
{ |
|
127
|
|
|
// The server request |
|
128
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
129
|
|
|
return $c->g(ServerRequestCreator::class) |
|
130
|
|
|
->fromGlobals() |
|
131
|
|
|
->withParsedBody([ |
|
132
|
|
|
'jxncall' => json_encode([ |
|
133
|
|
|
'type' => 'class', |
|
134
|
|
|
'name' => 'Sam ple', |
|
135
|
|
|
'method' => 'myMethod', |
|
136
|
|
|
'args' => [], |
|
137
|
|
|
]), |
|
138
|
|
|
]); |
|
139
|
|
|
}); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
142
|
|
|
$this->expectException(RequestException::class); |
|
143
|
|
|
jaxon()->processRequest(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @throws SetupException |
|
148
|
|
|
* @throws RequestException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function testRequestWithUnknownClassName() |
|
151
|
|
|
{ |
|
152
|
|
|
// The server request |
|
153
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
154
|
|
|
return $c->g(ServerRequestCreator::class) |
|
155
|
|
|
->fromGlobals() |
|
156
|
|
|
->withParsedBody([ |
|
157
|
|
|
'jxncall' => json_encode([ |
|
158
|
|
|
'type' => 'class', |
|
159
|
|
|
'name' => 'NotRegistered', |
|
160
|
|
|
'method' => 'myMethod', |
|
161
|
|
|
'args' => [], |
|
162
|
|
|
]), |
|
163
|
|
|
]); |
|
164
|
|
|
}); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
167
|
|
|
$this->expectException(RequestException::class); |
|
168
|
|
|
jaxon()->processRequest(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @throws SetupException |
|
173
|
|
|
* @throws RequestException |
|
174
|
|
|
*/ |
|
175
|
|
|
public function testRequestWithUnknownMethodName() |
|
176
|
|
|
{ |
|
177
|
|
|
// The server request |
|
178
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
179
|
|
|
return $c->g(ServerRequestCreator::class) |
|
180
|
|
|
->fromGlobals() |
|
181
|
|
|
->withParsedBody([ |
|
182
|
|
|
'jxncall' => json_encode([ |
|
183
|
|
|
'type' => 'class', |
|
184
|
|
|
'name' => 'Sample', |
|
185
|
|
|
'method' => 'unknownMethod', |
|
186
|
|
|
'args' => [], |
|
187
|
|
|
]), |
|
188
|
|
|
]); |
|
189
|
|
|
}); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
192
|
|
|
$this->expectException(RequestException::class); |
|
193
|
|
|
jaxon()->processRequest(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @throws SetupException |
|
198
|
|
|
* @throws RequestException |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testRequestWithIncorrectMethodName() |
|
201
|
|
|
{ |
|
202
|
|
|
// The server request |
|
203
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
204
|
|
|
return $c->g(ServerRequestCreator::class) |
|
205
|
|
|
->fromGlobals() |
|
206
|
|
|
->withParsedBody([ |
|
207
|
|
|
'jxncall' => json_encode([ |
|
208
|
|
|
'type' => 'class', |
|
209
|
|
|
'name' => 'Sample', |
|
210
|
|
|
'method' => 'my Method', |
|
211
|
|
|
'args' => [], |
|
212
|
|
|
]), |
|
213
|
|
|
]); |
|
214
|
|
|
}); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
217
|
|
|
$this->expectException(RequestException::class); |
|
218
|
|
|
jaxon()->processRequest(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @throws SetupException |
|
223
|
|
|
* @throws RequestException |
|
224
|
|
|
*/ |
|
225
|
|
|
public function testRequestToExcludedClass() |
|
226
|
|
|
{ |
|
227
|
|
|
jaxon()->setAppOption('', true); |
|
228
|
|
|
jaxon()->register(Jaxon::CALLABLE_CLASS, 'Excluded', [ |
|
229
|
|
|
'include' => __DIR__ . '/../src/excluded.php', |
|
230
|
|
|
'functions' => [ |
|
231
|
|
|
'*' => [ |
|
232
|
|
|
'excluded' => true, |
|
233
|
|
|
], |
|
234
|
|
|
], |
|
235
|
|
|
]); |
|
236
|
|
|
// The server request |
|
237
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
238
|
|
|
return $c->g(ServerRequestCreator::class) |
|
239
|
|
|
->fromGlobals() |
|
240
|
|
|
->withParsedBody([ |
|
241
|
|
|
'jxncall' => json_encode([ |
|
242
|
|
|
'type' => 'class', |
|
243
|
|
|
'name' => 'Excluded', |
|
244
|
|
|
'method' => 'action', |
|
245
|
|
|
'args' => [], |
|
246
|
|
|
]), |
|
247
|
|
|
]); |
|
248
|
|
|
}); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
251
|
|
|
$this->expectException(RequestException::class); |
|
252
|
|
|
jaxon()->processRequest(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @throws SetupException |
|
257
|
|
|
* @throws RequestException |
|
258
|
|
|
*/ |
|
259
|
|
|
public function testRequestToExcludedMethod() |
|
260
|
|
|
{ |
|
261
|
|
|
jaxon()->setAppOption('', true); |
|
262
|
|
|
jaxon()->register(Jaxon::CALLABLE_CLASS, 'Excluded', [ |
|
263
|
|
|
'include' => __DIR__ . '/../src/excluded.php', |
|
264
|
|
|
'functions' => [ |
|
265
|
|
|
'action' => [ |
|
266
|
|
|
'excluded' => true, |
|
267
|
|
|
], |
|
268
|
|
|
], |
|
269
|
|
|
]); |
|
270
|
|
|
// The server request |
|
271
|
|
|
jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
272
|
|
|
return $c->g(ServerRequestCreator::class) |
|
273
|
|
|
->fromGlobals() |
|
274
|
|
|
->withParsedBody([ |
|
275
|
|
|
'jxncall' => json_encode([ |
|
276
|
|
|
'type' => 'class', |
|
277
|
|
|
'name' => 'Excluded', |
|
278
|
|
|
'method' => 'action', |
|
279
|
|
|
'args' => [], |
|
280
|
|
|
]), |
|
281
|
|
|
]); |
|
282
|
|
|
}); |
|
283
|
|
|
|
|
284
|
|
|
$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
285
|
|
|
$this->expectException(RequestException::class); |
|
286
|
|
|
jaxon()->processRequest(); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|