Skip to content

Commit 9889db6

Browse files
committed
More RouteCollection tests for overwriting. Closes #1692
1 parent 79809d7 commit 9889db6

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

tests/system/Router/RouteCollectionTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,4 +1094,109 @@ public function testOffsetParameters()
10941094
$this->assertEquals($expected, $routes->getRoutes());
10951095
}
10961096

1097+
//--------------------------------------------------------------------
1098+
// Tests for router overwritting issue
1099+
// @see https://github.com/codeigniter4/CodeIgniter4/issues/1692
1100+
1101+
public function testRouteOverwritingDifferentSubdomains()
1102+
{
1103+
$_SERVER['REQUEST_METHOD'] = 'GET';
1104+
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1105+
1106+
$routes = $this->getCollector();
1107+
$router = new Router($routes);
1108+
1109+
$routes->setDefaultNamespace('App\Controllers');
1110+
$routes->setDefaultController('Home');
1111+
$routes->setDefaultMethod('index');
1112+
1113+
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
1114+
$routes->get('/', 'Home::index', ['subdomain' => 'dev']);
1115+
1116+
$expects = '\App\Controllers\Site\CDoc';
1117+
1118+
$this->assertEquals($expects, $router->handle('/'));
1119+
}
1120+
1121+
public function testRouteOverwritingTwoRules()
1122+
{
1123+
$_SERVER['REQUEST_METHOD'] = 'GET';
1124+
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1125+
1126+
$routes = $this->getCollector();
1127+
$router = new Router($routes);
1128+
1129+
$routes->setDefaultNamespace('App\Controllers');
1130+
$routes->setDefaultController('Home');
1131+
$routes->setDefaultMethod('index');
1132+
1133+
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
1134+
$routes->get('/', 'Home::index');
1135+
1136+
// the second rule applies, so overwrites the first
1137+
$expects = '\App\Controllers\Home';
1138+
1139+
$this->assertEquals($expects, $router->handle('/'));
1140+
}
1141+
1142+
public function testRouteOverwritingTwoRulesLastApplies()
1143+
{
1144+
$_SERVER['REQUEST_METHOD'] = 'GET';
1145+
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1146+
1147+
$routes = $this->getCollector();
1148+
$router = new Router($routes);
1149+
1150+
$routes->setDefaultNamespace('App\Controllers');
1151+
$routes->setDefaultController('Home');
1152+
$routes->setDefaultMethod('index');
1153+
1154+
$routes->get('/', 'Home::index');
1155+
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
1156+
1157+
$expects = '\App\Controllers\Site\CDoc';
1158+
1159+
$this->assertEquals($expects, $router->handle('/'));
1160+
}
1161+
1162+
public function testRouteOverwritingMatchingSubdomain()
1163+
{
1164+
$_SERVER['REQUEST_METHOD'] = 'GET';
1165+
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1166+
1167+
$routes = $this->getCollector();
1168+
$router = new Router($routes);
1169+
1170+
$routes->setDefaultNamespace('App\Controllers');
1171+
$routes->setDefaultController('Home');
1172+
$routes->setDefaultMethod('index');
1173+
1174+
$routes->get('/', 'Home::index', ['as' => 'ddd']);
1175+
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
1176+
1177+
$expects = '\App\Controllers\Site\CDoc';
1178+
1179+
$this->assertEquals($expects, $router->handle('/'));
1180+
}
1181+
1182+
public function testRouteOverwritingMatchingHost()
1183+
{
1184+
$_SERVER['REQUEST_METHOD'] = 'GET';
1185+
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1186+
1187+
$routes = $this->getCollector();
1188+
$router = new Router($routes);
1189+
1190+
$routes->setDefaultNamespace('App\Controllers');
1191+
$routes->setDefaultController('Home');
1192+
$routes->setDefaultMethod('index');
1193+
1194+
$routes->get('/', 'Home::index', ['as' => 'ddd']);
1195+
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['hostname' => 'doc.domain.com', 'as' => 'doc_index']);
1196+
1197+
$expects = '\App\Controllers\Site\CDoc';
1198+
1199+
$this->assertEquals($expects, $router->handle('/'));
1200+
}
1201+
10971202
}

0 commit comments

Comments
 (0)