-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(app): Allow turning off defined routes and/or auto routing #9749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec326ad
Fixing tests after routing options PR was merged
lonnieezell f8df0c6
PHPStand baseline
lonnieezell fb390c5
Rector improvements
lonnieezell 7e209dd
CS fixes
lonnieezell 269049a
Fix test
lonnieezell cbbe656
review comment fix
lonnieezell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Router; | ||
|
||
use CodeIgniter\Exceptions\PageNotFoundException; | ||
use CodeIgniter\HTTP\IncomingRequest; | ||
use CodeIgniter\HTTP\Method; | ||
use CodeIgniter\Router\Controllers\Mycontroller; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use Config\Feature; | ||
use Config\Modules; | ||
use Config\Routing; | ||
use PHPUnit\Framework\Attributes\Group; | ||
|
||
/** | ||
* Integration tests for routing optimization features. | ||
* Tests the complete flow from config to execution with different routing modes. | ||
* | ||
* @internal | ||
*/ | ||
#[Group('Others')] | ||
final class RoutingOptimizationTest extends CIUnitTestCase | ||
{ | ||
private IncomingRequest $request; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$featureConfig = config(Feature::class); | ||
$featureConfig->autoRoutesImproved = true; | ||
|
||
$this->request = service('request'); | ||
$this->request->setMethod(Method::GET); | ||
} | ||
|
||
private function createRouteCollection(Routing $routingConfig): RouteCollection | ||
{ | ||
$moduleConfig = new Modules(); | ||
$moduleConfig->enabled = false; | ||
|
||
$routingConfig->defaultNamespace = 'CodeIgniter\\Router\\Controllers'; | ||
|
||
$collection = new RouteCollection(service('locator'), $moduleConfig, $routingConfig); | ||
$collection->setHTTPVerb(Method::GET); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* Test auto-routing only mode (definedRoutes = false) | ||
* This should skip all route file loading and discovery | ||
*/ | ||
public function testAutoRoutingOnlyMode(): void | ||
{ | ||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = true; | ||
$routingConfig->definedRoutes = false; | ||
|
||
$collection = $this->createRouteCollection($routingConfig); | ||
|
||
// Add a defined route (should be ignored) | ||
$collection->get('ignored', 'Ignored::method'); | ||
|
||
// Verify no routes are returned | ||
$this->assertSame([], $collection->getRoutes()); | ||
|
||
// Create router and test auto-routing | ||
$router = new Router($collection, $this->request); | ||
$router->handle('mycontroller'); | ||
|
||
$this->assertSame('\\' . Mycontroller::class, $router->controllerName()); | ||
$this->assertSame('getIndex', $router->methodName()); | ||
} | ||
|
||
/** | ||
* Test defined routes only mode (autoRoute = false) | ||
* This should skip AutoRouter instantiation entirely | ||
*/ | ||
public function testDefinedRoutesOnlyMode(): void | ||
{ | ||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = false; | ||
$routingConfig->definedRoutes = true; | ||
|
||
$collection = $this->createRouteCollection($routingConfig); | ||
$collection->get('products', 'Products::list'); | ||
|
||
// Verify route is available | ||
$routes = $collection->getRoutes(); | ||
$this->assertArrayHasKey('products', $routes); | ||
|
||
// Create router and test defined routing | ||
$router = new Router($collection, $this->request); | ||
$router->handle('products'); | ||
|
||
$this->assertSame('\CodeIgniter\Router\Controllers\Products', $router->controllerName()); | ||
$this->assertSame('list', $router->methodName()); | ||
} | ||
|
||
/** | ||
* Test that defined routes only mode throws when no route matches | ||
* (no fallback to auto-routing) | ||
*/ | ||
public function testDefinedRoutesOnlyModeThrowsOnNoMatch(): void | ||
{ | ||
$this->expectException(PageNotFoundException::class); | ||
$this->expectExceptionMessage("Can't find a route for 'GET: nonexistent'"); | ||
|
||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = false; | ||
$routingConfig->definedRoutes = true; | ||
|
||
$collection = $this->createRouteCollection($routingConfig); | ||
$router = new Router($collection, $this->request); | ||
|
||
// Should throw immediately without trying auto-routing | ||
$router->handle('nonexistent'); | ||
} | ||
|
||
/** | ||
* Test both modes enabled (traditional behavior) | ||
* Should check defined routes first, then fall back to auto-routing | ||
*/ | ||
public function testBothModesEnabled(): void | ||
{ | ||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = true; | ||
$routingConfig->definedRoutes = true; | ||
|
||
$collection = $this->createRouteCollection($routingConfig); | ||
$collection->get('users', 'Users::index'); | ||
|
||
$router = new Router($collection, $this->request); | ||
|
||
// Test defined route takes precedence | ||
$router->handle('users'); | ||
$this->assertSame('\CodeIgniter\Router\Controllers\Users', $router->controllerName()); | ||
$this->assertSame('index', $router->methodName()); | ||
|
||
// Test fallback to auto-routing | ||
$router->handle('mycontroller'); | ||
$this->assertSame('\\' . Mycontroller::class, $router->controllerName()); | ||
$this->assertSame('getIndex', $router->methodName()); | ||
} | ||
|
||
/** | ||
* Test that route file loading is skipped when definedRoutes = false | ||
*/ | ||
public function testRouteFileLoadingSkipped(): void | ||
{ | ||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = true; | ||
$routingConfig->definedRoutes = false; | ||
$routingConfig->routeFiles = [APPPATH . 'Config/Routes.php']; | ||
|
||
$collection = $this->createRouteCollection($routingConfig); | ||
|
||
// Call loadRoutes - should return early | ||
$collection->loadRoutes(); | ||
|
||
// Verify routes were not loaded | ||
$this->assertSame([], $collection->getRoutes()); | ||
} | ||
|
||
/** | ||
* Test that route discovery is skipped when definedRoutes = false | ||
*/ | ||
public function testRouteDiscoverySkipped(): void | ||
{ | ||
$routingConfig = new Routing(); | ||
$routingConfig->autoRoute = true; | ||
$routingConfig->definedRoutes = false; | ||
|
||
$moduleConfig = new Modules(); | ||
$moduleConfig->enabled = true; // Enable discovery | ||
|
||
$collection = new RouteCollection(service('locator'), $moduleConfig, $routingConfig); | ||
|
||
// Verify discovery doesn't happen and routes remain empty | ||
$this->assertSame([], $collection->getRoutes()); | ||
} | ||
|
||
/** | ||
* Test configuration flags are properly stored | ||
*/ | ||
public function testConfigurationFlags(): void | ||
{ | ||
// Test defaults | ||
$defaultConfig = new Routing(); | ||
$collection = $this->createRouteCollection($defaultConfig); | ||
|
||
$this->assertFalse($collection->shouldAutoRoute()); | ||
$this->assertTrue($collection->shouldUseDefinedRoutes()); | ||
|
||
// Test custom values | ||
$customConfig = new Routing(); | ||
$customConfig->autoRoute = true; | ||
$customConfig->definedRoutes = false; | ||
|
||
$collection = $this->createRouteCollection($customConfig); | ||
|
||
$this->assertTrue($collection->shouldAutoRoute()); | ||
$this->assertFalse($collection->shouldUseDefinedRoutes()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# total 2767 errors | ||
# total 2808 errors | ||
|
||
includes: | ||
- argument.type.neon | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.