diff --git a/src/Router.php b/src/Router.php index 353de0a..96a4625 100644 --- a/src/Router.php +++ b/src/Router.php @@ -28,6 +28,7 @@ * @method $this head(string $route, string|array|Closure $callback, array $options = []) * @method $this options(string $route, string|array|Closure $callback, array $options = []) * @method $this ajax(string $route, string|array|Closure $callback, array $options = []) + * @method $this xget(string $route, string|array|Closure $callback, array $options = []) * @method $this xpost(string $route, string|array|Closure $callback, array $options = []) * @method $this xput(string $route, string|array|Closure $callback, array $options = []) * @method $this xdelete(string $route, string|array|Closure $callback, array $options = []) diff --git a/src/RouterRequest.php b/src/RouterRequest.php index d28c9d7..03de508 100644 --- a/src/RouterRequest.php +++ b/src/RouterRequest.php @@ -8,7 +8,7 @@ class RouterRequest { /** @var string $validMethods Valid methods for Router */ - protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH'; + protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH'; /** @var Request $request */ private $request; @@ -87,10 +87,10 @@ public function getMethod(): string $method = $this->request->getMethod(); $formMethod = $this->request->request->get('_method'); if (!empty($formMethod)) { - $method = strtoupper($formMethod); + $method = $formMethod; } - return $method; + return strtoupper($method); } /** diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 59a25b7..ceb52fe 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -82,4 +82,169 @@ public function testRequestMethods() // Cleanup ob_end_clean(); } + + public function testDynamicRequestUri() + { + $this->router->get('/test/:id', function (int $id) { + return "result: {$id}"; + }); + + $this->router->get('/test/:string', function (string $username) { + return "result: {$username}"; + }); + + $this->router->get('/test/:uuid', function (string $uuid) { + return "result: ce3a3f47-b950-4e34-97ee-fa5f127d4564"; + }); + + $this->router->get('/test/:date', function (string $date) { + return "result: 1938-11-10"; + }); + + $this->request->server->set('REQUEST_METHOD', 'GET'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test/10'); + $this->router->run(); + $this->assertEquals('result: 10', ob_get_contents()); + + ob_clean(); + $this->request->server->set('REQUEST_URI', '/test/izniburak'); + $this->router->run(); + $this->assertEquals('result: izniburak', ob_get_contents()); + + ob_clean(); + $this->request->server->set('REQUEST_URI', '/test/ce3a3f47-b950-4e34-97ee-fa5f127d4564'); + $this->router->run(); + $this->assertEquals('result: ce3a3f47-b950-4e34-97ee-fa5f127d4564', ob_get_contents()); + + ob_clean(); + $this->request->server->set('REQUEST_URI', '/test/1938-11-10'); + $this->router->run(); + $this->assertEquals('result: 1938-11-10', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testPostRequestMethod() + { + $this->router->post('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'POST'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testPutRequestMethod() + { + $this->router->put('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'PUT'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testDeleteRequestMethod() + { + $this->router->delete('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'DELETE'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testPatchRequestMethod() + { + $this->router->patch('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'PATCH'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testOptionsRequestMethod() + { + $this->router->options('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'OPTIONS'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testXGetRequestMethod() + { + $this->router->xget('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'XGET'); + $this->request->server->set('X-Requested-With', 'XMLHttpRequest'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } + + public function testXPostRequestMethod() + { + $this->router->xpost('/test', function () { + return "success"; + }); + + $this->request->server->set('REQUEST_METHOD', 'XPOST'); + $this->request->server->set('X-Requested-With', 'XMLHttpRequest'); + + ob_start(); + $this->request->server->set('REQUEST_URI', '/test'); + $this->router->run(); + $this->assertEquals('success', ob_get_contents()); + + // Cleanup + ob_end_clean(); + } }