Skip to content

Commit fb390c5

Browse files
committed
Rector improvements
1 parent f8df0c6 commit fb390c5

File tree

2 files changed

+223
-4
lines changed

2 files changed

+223
-4
lines changed

system/Router/Router.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
171171

172172
$this->translateURIDashes = $this->collection->shouldTranslateURIDashes();
173173

174-
if ($this->collection->shouldAutoRoute()) {
174+
/**
175+
* Gets the AutoRouter instance
176+
*/
177+
private function getAutoRouter(): AutoRouterInterface
178+
{
179+
if (!$this->autoRouter instanceof AutoRouterInterface) {
175180
$autoRoutesImproved = config(Feature::class)->autoRoutesImproved ?? false;
176181
if ($autoRoutesImproved) {
177182
assert($this->collection instanceof RouteCollection);
@@ -221,8 +226,6 @@ public function handle(?string $uri = null)
221226
// Restart filterInfo
222227
$this->filtersInfo = [];
223228

224-
<<<<<<< HEAD
225-
=======
226229
$useDefinedRoutes = $this->collection->shouldUseDefinedRoutes();
227230
$useAutoRoute = $this->collection->shouldAutoRoute();
228231

@@ -258,7 +261,6 @@ public function handle(?string $uri = null)
258261
}
259262

260263
// Original path: BOTH enabled (check defined routes first, then auto-route)
261-
>>>>>>> 8f675dc60a (Fixing tests after routing options PR was merged)
262264
// Checks defined routes
263265
if ($this->checkRoutes($uri)) {
264266
if ($this->collection->isFiltered($this->matchedRoute[0])) {
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Router;
15+
16+
use CodeIgniter\Exceptions\PageNotFoundException;
17+
use CodeIgniter\HTTP\IncomingRequest;
18+
use CodeIgniter\HTTP\Method;
19+
use CodeIgniter\Router\Controllers\Mycontroller;
20+
use CodeIgniter\Test\CIUnitTestCase;
21+
use Config\Feature;
22+
use Config\Modules;
23+
use Config\Routing;
24+
use PHPUnit\Framework\Attributes\Group;
25+
26+
/**
27+
* Integration tests for routing optimization features.
28+
* Tests the complete flow from config to execution with different routing modes.
29+
*
30+
* @internal
31+
*/
32+
#[Group('Others')]
33+
final class RoutingOptimizationTest extends CIUnitTestCase
34+
{
35+
private IncomingRequest $request;
36+
37+
protected function setUp(): void
38+
{
39+
parent::setUp();
40+
41+
$featureConfig = config(Feature::class);
42+
$featureConfig->autoRoutesImproved = true;
43+
44+
$this->request = service('request');
45+
$this->request->setMethod(Method::GET);
46+
}
47+
48+
private function createRouteCollection(Routing $routingConfig): RouteCollection
49+
{
50+
$moduleConfig = new Modules();
51+
$moduleConfig->enabled = false;
52+
53+
$routingConfig->defaultNamespace = 'CodeIgniter\\Router\\Controllers';
54+
55+
$collection = new RouteCollection(service('locator'), $moduleConfig, $routingConfig);
56+
$collection->setHTTPVerb(Method::GET);
57+
58+
return $collection;
59+
}
60+
61+
/**
62+
* Test auto-routing only mode (definedRoutes = false)
63+
* This should skip all route file loading and discovery
64+
*/
65+
public function testAutoRoutingOnlyMode(): void
66+
{
67+
$routingConfig = new Routing();
68+
$routingConfig->autoRoute = true;
69+
$routingConfig->definedRoutes = false;
70+
71+
$collection = $this->createRouteCollection($routingConfig);
72+
73+
// Add a defined route (should be ignored)
74+
$collection->get('ignored', 'Ignored::method');
75+
76+
// Verify no routes are returned
77+
$this->assertSame([], $collection->getRoutes());
78+
79+
// Create router and test auto-routing
80+
$router = new Router($collection, $this->request);
81+
$router->handle('mycontroller');
82+
83+
$this->assertSame('\\' . Mycontroller::class, $router->controllerName());
84+
$this->assertSame('getIndex', $router->methodName());
85+
}
86+
87+
/**
88+
* Test defined routes only mode (autoRoute = false)
89+
* This should skip AutoRouter instantiation entirely
90+
*/
91+
public function testDefinedRoutesOnlyMode(): void
92+
{
93+
$routingConfig = new Routing();
94+
$routingConfig->autoRoute = false;
95+
$routingConfig->definedRoutes = true;
96+
97+
$collection = $this->createRouteCollection($routingConfig);
98+
$collection->get('products', 'Products::list');
99+
100+
// Verify route is available
101+
$routes = $collection->getRoutes();
102+
$this->assertArrayHasKey('products', $routes);
103+
104+
// Create router and test defined routing
105+
$router = new Router($collection, $this->request);
106+
$router->handle('products');
107+
108+
$this->assertSame('\CodeIgniter\Router\Controllers\Products', $router->controllerName());
109+
$this->assertSame('list', $router->methodName());
110+
}
111+
112+
/**
113+
* Test that defined routes only mode throws when no route matches
114+
* (no fallback to auto-routing)
115+
*/
116+
public function testDefinedRoutesOnlyModeThrowsOnNoMatch(): void
117+
{
118+
$this->expectException(PageNotFoundException::class);
119+
$this->expectExceptionMessage("Can't find a route for 'GET: nonexistent'");
120+
121+
$routingConfig = new Routing();
122+
$routingConfig->autoRoute = false;
123+
$routingConfig->definedRoutes = true;
124+
125+
$collection = $this->createRouteCollection($routingConfig);
126+
$router = new Router($collection, $this->request);
127+
128+
// Should throw immediately without trying auto-routing
129+
$router->handle('nonexistent');
130+
}
131+
132+
/**
133+
* Test both modes enabled (traditional behavior)
134+
* Should check defined routes first, then fall back to auto-routing
135+
*/
136+
public function testBothModesEnabled(): void
137+
{
138+
$routingConfig = new Routing();
139+
$routingConfig->autoRoute = true;
140+
$routingConfig->definedRoutes = true;
141+
142+
$collection = $this->createRouteCollection($routingConfig);
143+
$collection->get('users', 'Users::index');
144+
145+
$router = new Router($collection, $this->request);
146+
147+
// Test defined route takes precedence
148+
$router->handle('users');
149+
$this->assertSame('\CodeIgniter\Router\Controllers\Users', $router->controllerName());
150+
$this->assertSame('index', $router->methodName());
151+
152+
// Test fallback to auto-routing
153+
$router->handle('mycontroller');
154+
$this->assertSame(Mycontroller::class, $router->controllerName());
155+
$this->assertSame('getIndex', $router->methodName());
156+
}
157+
158+
/**
159+
* Test that route file loading is skipped when definedRoutes = false
160+
*/
161+
public function testRouteFileLoadingSkipped(): void
162+
{
163+
$routingConfig = new Routing();
164+
$routingConfig->autoRoute = true;
165+
$routingConfig->definedRoutes = false;
166+
$routingConfig->routeFiles = [APPPATH . 'Config/Routes.php'];
167+
168+
$collection = $this->createRouteCollection($routingConfig);
169+
170+
// Call loadRoutes - should return early
171+
$collection->loadRoutes();
172+
173+
// Verify routes were not loaded
174+
$this->assertSame([], $collection->getRoutes());
175+
}
176+
177+
/**
178+
* Test that route discovery is skipped when definedRoutes = false
179+
*/
180+
public function testRouteDiscoverySkipped(): void
181+
{
182+
$routingConfig = new Routing();
183+
$routingConfig->autoRoute = true;
184+
$routingConfig->definedRoutes = false;
185+
186+
$moduleConfig = new Modules();
187+
$moduleConfig->enabled = true; // Enable discovery
188+
189+
$collection = new RouteCollection(service('locator'), $moduleConfig, $routingConfig);
190+
191+
// Verify discovery doesn't happen and routes remain empty
192+
$this->assertSame([], $collection->getRoutes());
193+
}
194+
195+
/**
196+
* Test configuration flags are properly stored
197+
*/
198+
public function testConfigurationFlags(): void
199+
{
200+
// Test defaults
201+
$defaultConfig = new Routing();
202+
$collection = $this->createRouteCollection($defaultConfig);
203+
204+
$this->assertFalse($collection->shouldAutoRoute());
205+
$this->assertTrue($collection->shouldUseDefinedRoutes());
206+
207+
// Test custom values
208+
$customConfig = new Routing();
209+
$customConfig->autoRoute = true;
210+
$customConfig->definedRoutes = false;
211+
212+
$collection = $this->createRouteCollection($customConfig);
213+
214+
$this->assertTrue($collection->shouldAutoRoute());
215+
$this->assertFalse($collection->shouldUseDefinedRoutes());
216+
}
217+
}

0 commit comments

Comments
 (0)