forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingOptimizationTest.php
More file actions
217 lines (172 loc) · 7.04 KB
/
RoutingOptimizationTest.php
File metadata and controls
217 lines (172 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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());
}
}