Skip to content

Commit c87169d

Browse files
authored
Merge pull request #7543 from kenjis/fix-feature-testing-with-autoroute
fix: [Auto Routing Improved] feature testing may not find controller/method
2 parents e31579e + f22139b commit c87169d

File tree

10 files changed

+131
-37
lines changed

10 files changed

+131
-37
lines changed

rector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
2727
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
2828
use Rector\Config\RectorConfig;
29+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
2930
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
3031
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
3132
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
@@ -88,6 +89,11 @@
8889
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
8990
],
9091

92+
RemoveUnusedConstructorParamRector::class => [
93+
// @TODO remove if deprecated $httpVerb is removed
94+
__DIR__ . '/system/Router/AutoRouterImproved.php',
95+
],
96+
9197
// call on purpose for nothing happen check
9298
RemoveEmptyMethodCallRector::class => [
9399
__DIR__ . '/tests',

system/Router/AutoRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(
8080
*
8181
* @return array [directory_name, controller_name, controller_method, params]
8282
*/
83-
public function getRoute(string $uri): array
83+
public function getRoute(string $uri, string $httpVerb): array
8484
{
8585
$segments = explode('/', $uri);
8686

system/Router/AutoRouterImproved.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ final class AutoRouterImproved implements AutoRouterInterface
5858
*/
5959
private bool $translateURIDashes;
6060

61-
/**
62-
* HTTP verb for the request.
63-
*/
64-
private string $httpVerb;
65-
6661
/**
6762
* The namespace for controllers.
6863
*/
@@ -74,15 +69,17 @@ final class AutoRouterImproved implements AutoRouterInterface
7469
private string $defaultController;
7570

7671
/**
77-
* The name of the default method
72+
* The name of the default method without HTTP verb prefix.
7873
*/
7974
private string $defaultMethod;
8075

8176
/**
8277
* @param class-string[] $protectedControllers
8378
* @param string $defaultController Short classname
79+
*
80+
* @deprecated $httpVerb is deprecated. No longer used.
8481
*/
85-
public function __construct(
82+
public function __construct(// @phpstan-ignore-line
8683
array $protectedControllers,
8784
string $namespace,
8885
string $defaultController,
@@ -93,22 +90,23 @@ public function __construct(
9390
$this->protectedControllers = $protectedControllers;
9491
$this->namespace = rtrim($namespace, '\\') . '\\';
9592
$this->translateURIDashes = $translateURIDashes;
96-
$this->httpVerb = $httpVerb;
9793
$this->defaultController = $defaultController;
98-
$this->defaultMethod = $httpVerb . ucfirst($defaultMethod);
94+
$this->defaultMethod = $defaultMethod;
9995

10096
// Set the default values
10197
$this->controller = $this->defaultController;
102-
$this->method = $this->defaultMethod;
10398
}
10499

105100
/**
106101
* Finds controller, method and params from the URI.
107102
*
108103
* @return array [directory_name, controller_name, controller_method, params]
109104
*/
110-
public function getRoute(string $uri): array
105+
public function getRoute(string $uri, string $httpVerb): array
111106
{
107+
$defaultMethod = strtolower($httpVerb) . ucfirst($this->defaultMethod);
108+
$this->method = $defaultMethod;
109+
112110
$segments = explode('/', $uri);
113111

114112
// WARNING: Directories get shifted out of the segments array.
@@ -144,10 +142,10 @@ public function getRoute(string $uri): array
144142
$methodSegment = $this->translateURIDashes(array_shift($nonDirSegments));
145143

146144
// Prefix HTTP verb
147-
$this->method = $this->httpVerb . ucfirst($methodSegment);
145+
$this->method = $httpVerb . ucfirst($methodSegment);
148146

149147
// Prevent access to default method path
150-
if (strtolower($this->method) === strtolower($this->defaultMethod)) {
148+
if (strtolower($this->method) === strtolower($defaultMethod)) {
151149
throw new PageNotFoundException(
152150
'Cannot access the default method "' . $this->method . '" with the method name URI path.'
153151
);

system/Router/AutoRouterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface AutoRouterInterface
2121
*
2222
* @return array [directory_name, controller_name, controller_method, params]
2323
*/
24-
public function getRoute(string $uri): array;
24+
public function getRoute(string $uri, string $httpVerb): array;
2525
}

system/Router/RouteCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class RouteCollection implements RouteCollectionInterface
150150
/**
151151
* The current method that the script is being called by.
152152
*
153-
* @var string
153+
* @var string HTTP verb (lower case) like `get`,`post` or `*`
154154
*/
155155
protected $HTTPVerb = '*';
156156

@@ -550,11 +550,13 @@ public function getHTTPVerb(): string
550550
* Sets the current HTTP verb.
551551
* Used primarily for testing.
552552
*
553+
* @param string $verb HTTP verb
554+
*
553555
* @return $this
554556
*/
555557
public function setHTTPVerb(string $verb)
556558
{
557-
$this->HTTPVerb = $verb;
559+
$this->HTTPVerb = strtolower($verb);
558560

559561
return $this;
560562
}

system/Router/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
126126
$this->controller = $this->collection->getDefaultController();
127127
$this->method = $this->collection->getDefaultMethod();
128128

129-
$this->collection->setHTTPVerb(strtolower($request->getMethod() ?? $_SERVER['REQUEST_METHOD']));
129+
$this->collection->setHTTPVerb($request->getMethod() ?? $_SERVER['REQUEST_METHOD']);
130130

131131
$this->translateURIDashes = $this->collection->shouldTranslateURIDashes();
132132

@@ -504,7 +504,7 @@ protected function checkRoutes(string $uri): bool
504504
public function autoRoute(string $uri)
505505
{
506506
[$this->directory, $this->controller, $this->method, $this->params]
507-
= $this->autoRouter->getRoute($uri);
507+
= $this->autoRouter->getRoute($uri, $this->collection->getHTTPVerb());
508508
}
509509

510510
/**

tests/system/Router/AutoRouterImprovedTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodGet()
5757
$router = $this->createNewAutoRouter();
5858

5959
[$directory, $controller, $method, $params]
60-
= $router->getRoute('/');
60+
= $router->getRoute('/', 'get');
6161

6262
$this->assertNull($directory);
6363
$this->assertSame('\\' . Index::class, $controller);
@@ -72,7 +72,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodPost()
7272
$router = $this->createNewAutoRouter('post');
7373

7474
[$directory, $controller, $method, $params]
75-
= $router->getRoute('/');
75+
= $router->getRoute('/', 'post');
7676

7777
$this->assertNull($directory);
7878
$this->assertSame('\\' . Index::class, $controller);
@@ -85,7 +85,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod()
8585
$router = $this->createNewAutoRouter();
8686

8787
[$directory, $controller, $method, $params]
88-
= $router->getRoute('mycontroller/somemethod');
88+
= $router->getRoute('mycontroller/somemethod', 'get');
8989

9090
$this->assertNull($directory);
9191
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -98,7 +98,7 @@ public function testFindsControllerAndMethodAndParam()
9898
$router = $this->createNewAutoRouter();
9999

100100
[$directory, $controller, $method, $params]
101-
= $router->getRoute('mycontroller/somemethod/a');
101+
= $router->getRoute('mycontroller/somemethod/a', 'get');
102102

103103
$this->assertNull($directory);
104104
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -115,15 +115,15 @@ public function testUriParamCountIsGreaterThanMethodParams()
115115

116116
$router = $this->createNewAutoRouter();
117117

118-
$router->getRoute('mycontroller/somemethod/a/b');
118+
$router->getRoute('mycontroller/somemethod/a/b', 'get');
119119
}
120120

121121
public function testAutoRouteFindsControllerWithFile()
122122
{
123123
$router = $this->createNewAutoRouter();
124124

125125
[$directory, $controller, $method, $params]
126-
= $router->getRoute('mycontroller');
126+
= $router->getRoute('mycontroller', 'get');
127127

128128
$this->assertNull($directory);
129129
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -136,7 +136,7 @@ public function testAutoRouteFindsControllerWithSubfolder()
136136
$router = $this->createNewAutoRouter();
137137

138138
[$directory, $controller, $method, $params]
139-
= $router->getRoute('subfolder/mycontroller/somemethod');
139+
= $router->getRoute('subfolder/mycontroller/somemethod', 'get');
140140

141141
$this->assertSame('Subfolder/', $directory);
142142
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Mycontroller::class, $controller);
@@ -149,7 +149,7 @@ public function testAutoRouteFindsDashedSubfolder()
149149
$router = $this->createNewAutoRouter();
150150

151151
[$directory, $controller, $method, $params]
152-
= $router->getRoute('dash-folder/mycontroller/somemethod');
152+
= $router->getRoute('dash-folder/mycontroller/somemethod', 'get');
153153

154154
$this->assertSame('Dash_folder/', $directory);
155155
$this->assertSame(
@@ -165,7 +165,7 @@ public function testAutoRouteFindsDashedController()
165165
$router = $this->createNewAutoRouter();
166166

167167
[$directory, $controller, $method, $params]
168-
= $router->getRoute('dash-folder/dash-controller/somemethod');
168+
= $router->getRoute('dash-folder/dash-controller/somemethod', 'get');
169169

170170
$this->assertSame('Dash_folder/', $directory);
171171
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -178,7 +178,7 @@ public function testAutoRouteFindsDashedMethod()
178178
$router = $this->createNewAutoRouter();
179179

180180
[$directory, $controller, $method, $params]
181-
= $router->getRoute('dash-folder/dash-controller/dash-method');
181+
= $router->getRoute('dash-folder/dash-controller/dash-method', 'get');
182182

183183
$this->assertSame('Dash_folder/', $directory);
184184
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -191,7 +191,7 @@ public function testAutoRouteFindsDefaultDashFolder()
191191
$router = $this->createNewAutoRouter();
192192

193193
[$directory, $controller, $method, $params]
194-
= $router->getRoute('dash-folder');
194+
= $router->getRoute('dash-folder', 'get');
195195

196196
$this->assertSame('Dash_folder/', $directory);
197197
$this->assertSame('\\' . Home::class, $controller);
@@ -205,7 +205,7 @@ public function testAutoRouteRejectsSingleDot()
205205

206206
$router = $this->createNewAutoRouter();
207207

208-
$router->getRoute('.');
208+
$router->getRoute('.', 'get');
209209
}
210210

211211
public function testAutoRouteRejectsDoubleDot()
@@ -214,7 +214,7 @@ public function testAutoRouteRejectsDoubleDot()
214214

215215
$router = $this->createNewAutoRouter();
216216

217-
$router->getRoute('..');
217+
$router->getRoute('..', 'get');
218218
}
219219

220220
public function testAutoRouteRejectsMidDot()
@@ -223,7 +223,7 @@ public function testAutoRouteRejectsMidDot()
223223

224224
$router = $this->createNewAutoRouter();
225225

226-
$router->getRoute('foo.bar');
226+
$router->getRoute('foo.bar', 'get');
227227
}
228228

229229
public function testRejectsDefaultControllerPath()
@@ -232,7 +232,7 @@ public function testRejectsDefaultControllerPath()
232232

233233
$router = $this->createNewAutoRouter();
234234

235-
$router->getRoute('home');
235+
$router->getRoute('home', 'get');
236236
}
237237

238238
public function testRejectsDefaultControllerAndDefaultMethodPath()
@@ -241,7 +241,7 @@ public function testRejectsDefaultControllerAndDefaultMethodPath()
241241

242242
$router = $this->createNewAutoRouter();
243243

244-
$router->getRoute('home/index');
244+
$router->getRoute('home/index', 'get');
245245
}
246246

247247
public function testRejectsDefaultMethodPath()
@@ -250,7 +250,7 @@ public function testRejectsDefaultMethodPath()
250250

251251
$router = $this->createNewAutoRouter();
252252

253-
$router->getRoute('mycontroller/index');
253+
$router->getRoute('mycontroller/index', 'get');
254254
}
255255

256256
public function testRejectsControllerWithRemapMethod()
@@ -262,6 +262,6 @@ public function testRejectsControllerWithRemapMethod()
262262

263263
$router = $this->createNewAutoRouter();
264264

265-
$router->getRoute('remap/test');
265+
$router->getRoute('remap/test', 'get');
266266
}
267267
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Test;
13+
14+
use CodeIgniter\Events\Events;
15+
use Config\Feature;
16+
use Config\Services;
17+
18+
/**
19+
* @group Others
20+
*
21+
* @internal
22+
*/
23+
final class FeatureTestAutoRoutingImprovedTest extends CIUnitTestCase
24+
{
25+
use FeatureTestTrait;
26+
27+
public static function setUpBeforeClass(): void
28+
{
29+
parent::setUpBeforeClass();
30+
31+
Events::simulate(true);
32+
33+
self::initializeRouter();
34+
}
35+
36+
public static function tearDownAfterClass(): void
37+
{
38+
parent::tearDownAfterClass();
39+
40+
Events::simulate(false);
41+
42+
Services::reset();
43+
}
44+
45+
private static function initializeRouter(): void
46+
{
47+
$routes = Services::routes();
48+
$routes->resetRoutes();
49+
$routes->loadRoutes();
50+
51+
$routes->setAutoRoute(true);
52+
config(Feature::class)->autoRoutesImproved = true;
53+
54+
$namespace = 'Tests\Support\Controllers';
55+
$routes->setDefaultNamespace($namespace);
56+
57+
$router = Services::router($routes);
58+
59+
Services::injectMock('router', $router);
60+
}
61+
62+
public function testCallGet()
63+
{
64+
$response = $this->get('newautorouting');
65+
66+
$response->assertSee('Hello');
67+
}
68+
69+
public function testCallPost()
70+
{
71+
$response = $this->post('newautorouting/save/1/a/b');
72+
73+
$response->assertSee('Saved');
74+
}
75+
}

0 commit comments

Comments
 (0)