Skip to content

Commit 4b98ddf

Browse files
committed
test: use HTTP\Method constants
1 parent 2c44922 commit 4b98ddf

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

tests/system/CodeIgniterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Config\Services;
1515
use CodeIgniter\Exceptions\ConfigException;
1616
use CodeIgniter\Exceptions\PageNotFoundException;
17+
use CodeIgniter\HTTP\Method;
1718
use CodeIgniter\HTTP\Response;
1819
use CodeIgniter\Router\Exceptions\RedirectException;
1920
use CodeIgniter\Router\RouteCollection;
@@ -721,7 +722,7 @@ public function testSpoofRequestMethodCanUsePUT(): void
721722
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
722723
$_SERVER['REQUEST_METHOD'] = 'POST';
723724

724-
$_POST['_method'] = 'PUT';
725+
$_POST['_method'] = Method::PUT;
725726

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

736-
$this->assertSame('PUT', Services::incomingrequest()->getMethod());
737+
$this->assertSame(Method::PUT, Services::incomingrequest()->getMethod());
737738
}
738739

739740
public function testSpoofRequestMethodCannotUseGET(): void

tests/system/Router/RouteCollectionTest.php

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\Config\Services;
1616
use CodeIgniter\controller;
1717
use CodeIgniter\Exceptions\PageNotFoundException;
18+
use CodeIgniter\HTTP\Method;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use Config\Modules;
2021
use Config\Routing;
@@ -144,7 +145,7 @@ public function testAddIgnoresDefaultNamespaceWhenExists(): void
144145

145146
public function testAddWorksWithCurrentHTTPMethods(): void
146147
{
147-
Services::request()->setMethod('GET');
148+
Services::request()->setMethod(Method::GET);
148149

149150
$routes = $this->getCollector();
150151

@@ -176,7 +177,7 @@ public function testAddWithLeadingSlash(): void
176177

177178
public function testMatchIgnoresInvalidHTTPMethods(): void
178179
{
179-
Services::request()->setMethod('GET');
180+
Services::request()->setMethod(Method::GET);
180181

181182
$routes = $this->getCollector();
182183

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

190191
public function testAddWorksWithArrayOFHTTPMethods(): void
191192
{
192-
Services::request()->setMethod('POST');
193+
Services::request()->setMethod(Method::POST);
193194

194195
$routes = $this->getCollector();
195196

@@ -696,7 +697,7 @@ public function testPresenterScaffoldsCorrectly(): void
696697

697698
public function testResourcesWithCustomController(): void
698699
{
699-
Services::request()->setMethod('GET');
700+
Services::request()->setMethod(Method::GET);
700701
$routes = $this->getCollector();
701702

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

714715
public function testResourcesWithCustomPlaceholder(): void
715716
{
716-
Services::request()->setMethod('GET');
717+
Services::request()->setMethod(Method::GET);
717718
$routes = $this->getCollector();
718719

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

731732
public function testResourcesWithDefaultPlaceholder(): void
732733
{
733-
Services::request()->setMethod('GET');
734+
Services::request()->setMethod(Method::GET);
734735
$routes = $this->getCollector();
735736

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

749750
public function testResourcesWithBogusDefaultPlaceholder(): void
750751
{
751-
Services::request()->setMethod('GET');
752+
Services::request()->setMethod(Method::GET);
752753
$routes = $this->getCollector();
753754

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

767768
public function testResourcesWithOnly(): void
768769
{
769-
Services::request()->setMethod('GET');
770+
Services::request()->setMethod(Method::GET);
770771
$routes = $this->getCollector();
771772

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

781782
public function testResourcesWithExcept(): void
782783
{
783-
Services::request()->setMethod('GET');
784+
Services::request()->setMethod(Method::GET);
784785
$routes = $this->getCollector();
785786

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

812813
public function testMatchSupportsMultipleMethods(): void
813814
{
814-
Services::request()->setMethod('GET');
815+
Services::request()->setMethod(Method::GET);
815816
$routes = $this->getCollector();
816817

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

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

822-
Services::request()->setMethod('POST');
823+
Services::request()->setMethod(Method::POST);
823824
$routes = $this->getCollector();
824825
$routes->match(['GET', 'POST'], 'here', 'there');
825826
$this->assertSame($expected, $routes->getRoutes());
826827
}
827828

828829
public function testGet(): void
829830
{
830-
Services::request()->setMethod('GET');
831+
Services::request()->setMethod(Method::GET);
831832
$routes = $this->getCollector();
832833

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

952-
Services::request()->setMethod('GET');
953+
Services::request()->setMethod(Method::GET);
953954
$routes = $this->getCollector();
954955

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

14351436
public function testRouteGroupWithFilterSimple(): void
14361437
{
1437-
Services::request()->setMethod('GET');
1438+
Services::request()->setMethod(Method::GET);
14381439
$routes = $this->getCollector();
14391440

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

14541455
public function testRouteGroupWithFilterWithParams(): void
14551456
{
1456-
Services::request()->setMethod('GET');
1457+
Services::request()->setMethod(Method::GET);
14571458
$routes = $this->getCollector();
14581459

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

14721473
public function test404OverrideNot(): void
14731474
{
1474-
Services::request()->setMethod('GET');
1475+
Services::request()->setMethod(Method::GET);
14751476
$routes = $this->getCollector();
14761477

14771478
$this->assertNull($routes->get404Override());
14781479
}
14791480

14801481
public function test404OverrideString(): void
14811482
{
1482-
Services::request()->setMethod('GET');
1483+
Services::request()->setMethod(Method::GET);
14831484
$routes = $this->getCollector();
14841485

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

14891490
public function test404OverrideCallable(): void
14901491
{
1491-
Services::request()->setMethod('GET');
1492+
Services::request()->setMethod(Method::GET);
14921493
$routes = $this->getCollector();
14931494

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

15001501
public function testOffsetParameters(): void
15011502
{
1502-
Services::request()->setMethod('GET');
1503+
Services::request()->setMethod(Method::GET);
15031504
$routes = $this->getCollector();
15041505

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

15201521
$routes = $this->getCollector();
15211522

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

15321533
$routes = $this->getCollector();
15331534

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

15441545
$routes = $this->getCollector();
15451546

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

15561557
$routes = $this->getCollector();
15571558

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

15681569
$routes = $this->getCollector();
15691570

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

15801581
$routes = $this->getCollector();
15811582

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

15921593
$routes = $this->getCollector();
15931594

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

16041605
$routes = $this->getCollector();
16051606

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

16161617
$routes = $this->getCollector();
16171618

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

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

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

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

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

17151716
$routes = $this->getCollector();
17161717
$router = new Router($routes, Services::request());
@@ -1734,7 +1735,7 @@ public function testRouteOverwritingMatchingHost(): void
17341735
*/
17351736
public function testRouteDefaultNameSpace(): void
17361737
{
1737-
Services::request()->setMethod('GET');
1738+
Services::request()->setMethod(Method::GET);
17381739
$routes = $this->getCollector();
17391740
$router = new Router($routes, Services::request());
17401741

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

17491750
public function testZeroAsURIPath(): void
17501751
{
1751-
Services::request()->setMethod('GET');
1752+
Services::request()->setMethod(Method::GET);
17521753
$routes = $this->getCollector();
17531754
$router = new Router($routes, Services::request());
17541755

@@ -1796,7 +1797,7 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void
17961797
*/
17971798
public function testRoutesControllerNameReturnsFQCN($namespace): void
17981799
{
1799-
Services::request()->setMethod('GET');
1800+
Services::request()->setMethod(Method::GET);
18001801
$routes = $this->getCollector();
18011802
$routes->setAutoRoute(false);
18021803
$routes->setDefaultNamespace($namespace);

tests/system/Router/RouterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\Exceptions\PageNotFoundException;
1616
use CodeIgniter\HTTP\Exceptions\RedirectException;
1717
use CodeIgniter\HTTP\IncomingRequest;
18+
use CodeIgniter\HTTP\Method;
1819
use CodeIgniter\Router\Exceptions\RouterException;
1920
use CodeIgniter\Test\CIUnitTestCase;
2021
use Config\Modules;
@@ -65,7 +66,7 @@ protected function setUp(): void
6566
$this->collection->map($routes);
6667

6768
$this->request = Services::request();
68-
$this->request->setMethod('GET');
69+
$this->request->setMethod(Method::GET);
6970
}
7071

7172
public function testEmptyURIMatchesRoot(): void

0 commit comments

Comments
 (0)