Skip to content

Commit f7dfc53

Browse files
committed
Added XGET request method. Also added new test cases.
1 parent 3641a96 commit f7dfc53

File tree

3 files changed

+169
-3
lines changed

3 files changed

+169
-3
lines changed

src/Router.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @method $this head(string $route, string|array|Closure $callback, array $options = [])
2929
* @method $this options(string $route, string|array|Closure $callback, array $options = [])
3030
* @method $this ajax(string $route, string|array|Closure $callback, array $options = [])
31+
* @method $this xget(string $route, string|array|Closure $callback, array $options = [])
3132
* @method $this xpost(string $route, string|array|Closure $callback, array $options = [])
3233
* @method $this xput(string $route, string|array|Closure $callback, array $options = [])
3334
* @method $this xdelete(string $route, string|array|Closure $callback, array $options = [])

src/RouterRequest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class RouterRequest
99
{
1010
/** @var string $validMethods Valid methods for Router */
11-
protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH';
11+
protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH';
1212

1313
/** @var Request $request */
1414
private $request;
@@ -87,10 +87,10 @@ public function getMethod(): string
8787
$method = $this->request->getMethod();
8888
$formMethod = $this->request->request->get('_method');
8989
if (!empty($formMethod)) {
90-
$method = strtoupper($formMethod);
90+
$method = $formMethod;
9191
}
9292

93-
return $method;
93+
return strtoupper($method);
9494
}
9595

9696
/**

tests/RouterTest.php

+165
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,169 @@ public function testRequestMethods()
8282
// Cleanup
8383
ob_end_clean();
8484
}
85+
86+
public function testDynamicRequestUri()
87+
{
88+
$this->router->get('/test/:id', function (int $id) {
89+
return "result: {$id}";
90+
});
91+
92+
$this->router->get('/test/:string', function (string $username) {
93+
return "result: {$username}";
94+
});
95+
96+
$this->router->get('/test/:uuid', function (string $uuid) {
97+
return "result: ce3a3f47-b950-4e34-97ee-fa5f127d4564";
98+
});
99+
100+
$this->router->get('/test/:date', function (string $date) {
101+
return "result: 1938-11-10";
102+
});
103+
104+
$this->request->server->set('REQUEST_METHOD', 'GET');
105+
106+
ob_start();
107+
$this->request->server->set('REQUEST_URI', '/test/10');
108+
$this->router->run();
109+
$this->assertEquals('result: 10', ob_get_contents());
110+
111+
ob_clean();
112+
$this->request->server->set('REQUEST_URI', '/test/izniburak');
113+
$this->router->run();
114+
$this->assertEquals('result: izniburak', ob_get_contents());
115+
116+
ob_clean();
117+
$this->request->server->set('REQUEST_URI', '/test/ce3a3f47-b950-4e34-97ee-fa5f127d4564');
118+
$this->router->run();
119+
$this->assertEquals('result: ce3a3f47-b950-4e34-97ee-fa5f127d4564', ob_get_contents());
120+
121+
ob_clean();
122+
$this->request->server->set('REQUEST_URI', '/test/1938-11-10');
123+
$this->router->run();
124+
$this->assertEquals('result: 1938-11-10', ob_get_contents());
125+
126+
// Cleanup
127+
ob_end_clean();
128+
}
129+
130+
public function testPostRequestMethod()
131+
{
132+
$this->router->post('/test', function () {
133+
return "success";
134+
});
135+
136+
$this->request->server->set('REQUEST_METHOD', 'POST');
137+
138+
ob_start();
139+
$this->request->server->set('REQUEST_URI', '/test');
140+
$this->router->run();
141+
$this->assertEquals('success', ob_get_contents());
142+
143+
// Cleanup
144+
ob_end_clean();
145+
}
146+
147+
public function testPutRequestMethod()
148+
{
149+
$this->router->put('/test', function () {
150+
return "success";
151+
});
152+
153+
$this->request->server->set('REQUEST_METHOD', 'PUT');
154+
155+
ob_start();
156+
$this->request->server->set('REQUEST_URI', '/test');
157+
$this->router->run();
158+
$this->assertEquals('success', ob_get_contents());
159+
160+
// Cleanup
161+
ob_end_clean();
162+
}
163+
164+
public function testDeleteRequestMethod()
165+
{
166+
$this->router->delete('/test', function () {
167+
return "success";
168+
});
169+
170+
$this->request->server->set('REQUEST_METHOD', 'DELETE');
171+
172+
ob_start();
173+
$this->request->server->set('REQUEST_URI', '/test');
174+
$this->router->run();
175+
$this->assertEquals('success', ob_get_contents());
176+
177+
// Cleanup
178+
ob_end_clean();
179+
}
180+
181+
public function testPatchRequestMethod()
182+
{
183+
$this->router->patch('/test', function () {
184+
return "success";
185+
});
186+
187+
$this->request->server->set('REQUEST_METHOD', 'PATCH');
188+
189+
ob_start();
190+
$this->request->server->set('REQUEST_URI', '/test');
191+
$this->router->run();
192+
$this->assertEquals('success', ob_get_contents());
193+
194+
// Cleanup
195+
ob_end_clean();
196+
}
197+
198+
public function testOptionsRequestMethod()
199+
{
200+
$this->router->options('/test', function () {
201+
return "success";
202+
});
203+
204+
$this->request->server->set('REQUEST_METHOD', 'OPTIONS');
205+
206+
ob_start();
207+
$this->request->server->set('REQUEST_URI', '/test');
208+
$this->router->run();
209+
$this->assertEquals('success', ob_get_contents());
210+
211+
// Cleanup
212+
ob_end_clean();
213+
}
214+
215+
public function testXGetRequestMethod()
216+
{
217+
$this->router->xget('/test', function () {
218+
return "success";
219+
});
220+
221+
$this->request->server->set('REQUEST_METHOD', 'XGET');
222+
$this->request->server->set('X-Requested-With', 'XMLHttpRequest');
223+
224+
ob_start();
225+
$this->request->server->set('REQUEST_URI', '/test');
226+
$this->router->run();
227+
$this->assertEquals('success', ob_get_contents());
228+
229+
// Cleanup
230+
ob_end_clean();
231+
}
232+
233+
public function testXPostRequestMethod()
234+
{
235+
$this->router->xpost('/test', function () {
236+
return "success";
237+
});
238+
239+
$this->request->server->set('REQUEST_METHOD', 'XPOST');
240+
$this->request->server->set('X-Requested-With', 'XMLHttpRequest');
241+
242+
ob_start();
243+
$this->request->server->set('REQUEST_URI', '/test');
244+
$this->router->run();
245+
$this->assertEquals('success', ob_get_contents());
246+
247+
// Cleanup
248+
ob_end_clean();
249+
}
85250
}

0 commit comments

Comments
 (0)