Skip to content

Commit

Permalink
Added XGET request method. Also added new test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Jan 23, 2022
1 parent 3641a96 commit f7dfc53
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
6 changes: 3 additions & 3 deletions src/RouterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
165 changes: 165 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit f7dfc53

Please sign in to comment.