Skip to content

Commit

Permalink
test: use HTTP\Method constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 19, 2023
1 parent 2c44922 commit 4b98ddf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
5 changes: 3 additions & 2 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
Expand Down Expand Up @@ -721,7 +722,7 @@ public function testSpoofRequestMethodCanUsePUT(): void
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';

$_POST['_method'] = 'PUT';
$_POST['_method'] = Method::PUT;

$routes = \Config\Services::routes();
$routes->setDefaultNamespace('App\Controllers');
Expand All @@ -733,7 +734,7 @@ public function testSpoofRequestMethodCanUsePUT(): void
$this->codeigniter->run();
ob_get_clean();

$this->assertSame('PUT', Services::incomingrequest()->getMethod());
$this->assertSame(Method::PUT, Services::incomingrequest()->getMethod());
}

public function testSpoofRequestMethodCannotUseGET(): void
Expand Down
73 changes: 37 additions & 36 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Config\Services;
use CodeIgniter\controller;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Method;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Modules;
use Config\Routing;
Expand Down Expand Up @@ -144,7 +145,7 @@ public function testAddIgnoresDefaultNamespaceWhenExists(): void

public function testAddWorksWithCurrentHTTPMethods(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand Down Expand Up @@ -176,7 +177,7 @@ public function testAddWithLeadingSlash(): void

public function testMatchIgnoresInvalidHTTPMethods(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -189,7 +190,7 @@ public function testMatchIgnoresInvalidHTTPMethods(): void

public function testAddWorksWithArrayOFHTTPMethods(): void
{
Services::request()->setMethod('POST');
Services::request()->setMethod(Method::POST);

$routes = $this->getCollector();

Expand Down Expand Up @@ -696,7 +697,7 @@ public function testPresenterScaffoldsCorrectly(): void

public function testResourcesWithCustomController(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->resource('photos', ['controller' => '<script>gallery']);
Expand All @@ -713,7 +714,7 @@ public function testResourcesWithCustomController(): void

public function testResourcesWithCustomPlaceholder(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->resource('photos', ['placeholder' => ':num']);
Expand All @@ -730,7 +731,7 @@ public function testResourcesWithCustomPlaceholder(): void

public function testResourcesWithDefaultPlaceholder(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->setDefaultConstraint('num');
Expand All @@ -748,7 +749,7 @@ public function testResourcesWithDefaultPlaceholder(): void

public function testResourcesWithBogusDefaultPlaceholder(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->setDefaultConstraint(':num');
Expand All @@ -766,7 +767,7 @@ public function testResourcesWithBogusDefaultPlaceholder(): void

public function testResourcesWithOnly(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->resource('photos', ['only' => 'index']);
Expand All @@ -780,7 +781,7 @@ public function testResourcesWithOnly(): void

public function testResourcesWithExcept(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->resource('photos', ['except' => 'edit,new']);
Expand Down Expand Up @@ -811,23 +812,23 @@ public function testResourcesWithWebsafe(): void

public function testMatchSupportsMultipleMethods(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$expected = ['here' => '\there'];

$routes->match(['GET', 'POST'], 'here', 'there');
$this->assertSame($expected, $routes->getRoutes());

Services::request()->setMethod('POST');
Services::request()->setMethod(Method::POST);
$routes = $this->getCollector();
$routes->match(['GET', 'POST'], 'here', 'there');
$this->assertSame($expected, $routes->getRoutes());
}

public function testGet(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$expected = ['here' => '\there'];
Expand Down Expand Up @@ -949,7 +950,7 @@ public function testEnvironmentRestricts(): void
{
// ENVIRONMENT should be 'testing'

Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$expected = ['here' => '\there'];
Expand Down Expand Up @@ -1434,7 +1435,7 @@ static function (): void {},

public function testRouteGroupWithFilterSimple(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->group(
Expand All @@ -1453,7 +1454,7 @@ static function ($routes): void {

public function testRouteGroupWithFilterWithParams(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->group(
Expand All @@ -1471,15 +1472,15 @@ static function ($routes): void {

public function test404OverrideNot(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$this->assertNull($routes->get404Override());
}

public function test404OverrideString(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->set404Override('Explode');
Expand All @@ -1488,7 +1489,7 @@ public function test404OverrideString(): void

public function test404OverrideCallable(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->set404Override(static function (): void {
Expand All @@ -1499,7 +1500,7 @@ public function test404OverrideCallable(): void

public function testOffsetParameters(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();

$routes->get('users/(:num)', 'users/show/$1', ['offset' => 1]);
Expand All @@ -1515,7 +1516,7 @@ public function testOffsetParameters(): void
public function testRouteToWithSubdomainMatch(): void
{
$_SERVER['HTTP_HOST'] = 'doc.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1527,7 +1528,7 @@ public function testRouteToWithSubdomainMatch(): void
public function testRouteToWithSubdomainMismatch(): void
{
$_SERVER['HTTP_HOST'] = 'dev.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1539,7 +1540,7 @@ public function testRouteToWithSubdomainMismatch(): void
public function testRouteToWithSubdomainNot(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1551,7 +1552,7 @@ public function testRouteToWithSubdomainNot(): void
public function testRouteToWithGenericSubdomainMatch(): void
{
$_SERVER['HTTP_HOST'] = 'doc.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1563,7 +1564,7 @@ public function testRouteToWithGenericSubdomainMatch(): void
public function testRouteToWithGenericSubdomainMismatch(): void
{
$_SERVER['HTTP_HOST'] = 'dev.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1575,7 +1576,7 @@ public function testRouteToWithGenericSubdomainMismatch(): void
public function testRouteToWithGenericSubdomainNot(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1587,7 +1588,7 @@ public function testRouteToWithGenericSubdomainNot(): void
public function testRouteToWithoutSubdomainMatch(): void
{
$_SERVER['HTTP_HOST'] = 'doc.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1599,7 +1600,7 @@ public function testRouteToWithoutSubdomainMatch(): void
public function testRouteToWithoutSubdomainMismatch(): void
{
$_SERVER['HTTP_HOST'] = 'dev.example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1611,7 +1612,7 @@ public function testRouteToWithoutSubdomainMismatch(): void
public function testRouteToWithoutSubdomainNot(): void
{
$_SERVER['HTTP_HOST'] = 'example.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();

Expand All @@ -1628,7 +1629,7 @@ public function testRouteToWithoutSubdomainNot(): void
public function testRouteOverwritingDifferentSubdomains(): void
{
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());
Expand All @@ -1649,7 +1650,7 @@ public function testRouteOverwritingDifferentSubdomains(): void
public function testRouteOverwritingTwoRules(): void
{
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());
Expand All @@ -1670,7 +1671,7 @@ public function testRouteOverwritingTwoRules(): void
public function testRouteOverwritingTwoRulesLastApplies(): void
{
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());
Expand All @@ -1690,7 +1691,7 @@ public function testRouteOverwritingTwoRulesLastApplies(): void
public function testRouteOverwritingMatchingSubdomain(): void
{
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());
Expand All @@ -1710,7 +1711,7 @@ public function testRouteOverwritingMatchingSubdomain(): void
public function testRouteOverwritingMatchingHost(): void
{
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());
Expand All @@ -1734,7 +1735,7 @@ public function testRouteOverwritingMatchingHost(): void
*/
public function testRouteDefaultNameSpace(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();
$router = new Router($routes, Services::request());

Expand All @@ -1748,7 +1749,7 @@ public function testRouteDefaultNameSpace(): void

public function testZeroAsURIPath(): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();
$router = new Router($routes, Services::request());

Expand Down Expand Up @@ -1796,7 +1797,7 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void
*/
public function testRoutesControllerNameReturnsFQCN($namespace): void
{
Services::request()->setMethod('GET');
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();
$routes->setAutoRoute(false);
$routes->setDefaultNamespace($namespace);
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\Router\Exceptions\RouterException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Modules;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function setUp(): void
$this->collection->map($routes);

$this->request = Services::request();
$this->request->setMethod('GET');
$this->request->setMethod(Method::GET);
}

public function testEmptyURIMatchesRoot(): void
Expand Down

0 comments on commit 4b98ddf

Please sign in to comment.