Skip to content

Commit

Permalink
Merge pull request #8981 from kenjis/change-auto-routing-improved-config
Browse files Browse the repository at this point in the history
config: change default config for Auto Routing Improved
  • Loading branch information
kenjis authored Jun 25, 2024
2 parents ab71e4f + f0db26f commit 4722157
Show file tree
Hide file tree
Showing 21 changed files with 590 additions and 430 deletions.
4 changes: 2 additions & 2 deletions app/Config/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class Feature extends BaseConfig
{
/**
* Use improved new auto routing instead of the default legacy version.
* Use improved new auto routing instead of the legacy version.
*/
public bool $autoRoutesImproved = false;
public bool $autoRoutesImproved = true;

/**
* Use filter execution order in 4.4 or before.
Expand Down
2 changes: 1 addition & 1 deletion app/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@ class Routing extends BaseRouting
*
* Default: false
*/
public bool $translateUriToCamelCase = false;
public bool $translateUriToCamelCase = true;
}
3 changes: 3 additions & 0 deletions tests/system/Commands/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Feature;
use Config\Services;
use PHPUnit\Framework\Attributes\Group;

Expand Down Expand Up @@ -213,6 +214,8 @@ public function testRoutesCommandRouteLegacy(): void
$routes = $this->getCleanRoutes();
$routes->loadRoutes();

$featureConfig = config(Feature::class);
$featureConfig->autoRoutesImproved = false;
$routes->setAutoRoute(true);

command('routes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public function testRead(): void

public function testReadTranslateURIDashes(): void
{
$config = config(Routing::class);
$config->translateURIDashes = true;
$config = config(Routing::class);
$config->translateURIDashes = true;
$config->translateUriToCamelCase = false;
Factories::injectMock('config', Routing::class, $config);

$reader = $this->createControllerMethodReader(
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Router/AutoRouterImprovedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,16 @@ public function testAutoRouteFindsControllerWithSubSubfolder(): void
$this->assertSame([], $params);
}

private function disableTranslateUriToCamelCase(): void
{
$routingConfig = config(Routing::class);
$routingConfig->translateUriToCamelCase = false;
}

public function testAutoRouteFindsDashedSubfolder(): void
{
$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
Expand All @@ -222,6 +230,8 @@ public function testAutoRouteFindsDashedSubfolder(): void

public function testAutoRouteFindsDashedController(): void
{
$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
Expand All @@ -235,6 +245,8 @@ public function testAutoRouteFindsDashedController(): void

public function testAutoRouteFindsDashedMethod(): void
{
$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
Expand All @@ -248,6 +260,8 @@ public function testAutoRouteFindsDashedMethod(): void

public function testAutoRouteFindsDefaultDashFolder(): void
{
$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
Expand Down Expand Up @@ -438,6 +452,8 @@ public function testRejectsURIWithUnderscoreController(): void
'AutoRouterImproved prohibits access to the URI containing underscores ("dash_controller")'
);

$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

$router->getRoute('dash-folder/dash_controller/dash-method', Method::GET);
Expand All @@ -450,6 +466,8 @@ public function testRejectsURIWithUnderscoreMethod(): void
'AutoRouterImproved prohibits access to the URI containing underscores ("dash_method")'
);

$this->disableTranslateUriToCamelCase();

$router = $this->createNewAutoRouter();

$router->getRoute('dash-folder/dash-controller/dash_method', Method::GET);
Expand Down
14 changes: 6 additions & 8 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Method;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Feature;
use Config\Modules;
use Config\Routing;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -1768,12 +1769,12 @@ public static function provideRouteDefaultNamespace(): iterable
];
}

/**
* @param mixed $namespace
*/
#[DataProvider('provideRouteDefaultNamespace')]
public function testAutoRoutesControllerNameReturnsFQCN($namespace): void
public function testAutoRoutesControllerNameReturnsFQCN(string $namespace): void
{
$featureConfig = config(Feature::class);
$featureConfig->autoRoutesImproved = false;

$routes = $this->getCollector();
$routes->setAutoRoute(true);
$routes->setDefaultNamespace($namespace);
Expand All @@ -1788,11 +1789,8 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void
$this->assertSame('\\' . Product::class, $router->controllerName());
}

/**
* @param mixed $namespace
*/
#[DataProvider('provideRouteDefaultNamespace')]
public function testRoutesControllerNameReturnsFQCN($namespace): void
public function testRoutesControllerNameReturnsFQCN(string $namespace): void
{
Services::request()->setMethod(Method::GET);
$routes = $this->getCollector();
Expand Down
8 changes: 8 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use CodeIgniter\Router\Exceptions\RouterException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\Feature;
use Config\Modules;
use Config\Routing;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -42,12 +43,19 @@ protected function setUp(): void
{
parent::setUp();

$this->disableAutoRoutesImproved();
$this->createRouteCollection();

$this->request = Services::request();
$this->request->setMethod(Method::GET);
}

private function disableAutoRoutesImproved(): void
{
$featureConfig = config(Feature::class);
$featureConfig->autoRoutesImproved = false;
}

private function createRouteCollection(?Routing $routingConfig = null): void
{
$moduleConfig = new Modules();
Expand Down
16 changes: 10 additions & 6 deletions tests/system/Test/FeatureTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use CodeIgniter\HTTP\Response;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use Config\App;
use Config\Feature;
use Config\Routing;
use Config\Services;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -359,17 +360,19 @@ public static function provideOpenCliRoutesFromHttpGot404(): iterable
];
}

/**
* @param mixed $from
* @param mixed $to
* @param mixed $httpGet
*/
private function disableAutoRoutesImproved(): void
{
$featureConfig = config(Feature::class);
$featureConfig->autoRoutesImproved = false;
}

#[DataProvider('provideOpenCliRoutesFromHttpGot404')]
public function testOpenCliRoutesFromHttpGot404($from, $to, $httpGet): void
public function testOpenCliRoutesFromHttpGot404(string $from, string $to, string $httpGet): void
{
$this->expectException(PageNotFoundException::class);
$this->expectExceptionMessage('Cannot access CLI Route: ');

$this->disableAutoRoutesImproved();
$collection = Services::routes();
$collection->setAutoRoute(true);
$collection->setDefaultNamespace('Tests\Support\Controllers');
Expand Down Expand Up @@ -641,6 +644,7 @@ public function testSetupRequestBodyWithBody(): void

public function testAutoRoutingLegacy(): void
{
$this->disableAutoRoutesImproved();
$config = config(Routing::class);
$config->autoRoute = true;
Factories::injectMock('config', Routing::class, $config);
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Others
- If a controller is found that corresponds to a URI segment and that controller
does not have a method defined for the URI segment, the default method will
now be executed. This addition allows for more flexible handling of URIs in
auto routing. See :ref:`controller-default-method-fallback` for details.
auto routing. See :ref:`auto-routing-improved-default-method-fallback` for details.
- **Filters:** Now you can use Filter Arguments with :ref:`$filters property <filters-filters-filter-arguments>`.
- **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales.
- **Table:** Added ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with headings. See :ref:`table-sync-rows-with-headings` for details.
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Routing

- **AutoRouting Improved:** The ``$translateUriToCamelCase`` option has been added
that allows using CamelCase controller and method names. See
:ref:`controller-translate-uri-to-camelcase`.
:ref:`translate-uri-to-camelcase`.
- **Others:**
- Added option ``$multipleSegmentsOneParam``. When this option is
enabled, a placeholder that matches multiple segments, such as ``(:any)``, will
Expand Down
Loading

0 comments on commit 4722157

Please sign in to comment.