Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ protected function checkRoutes(string $uri): bool
*/
private function replaceBackReferences(string $input, array $matches): string
{
$pattern = '/\$([1-' . count($matches) . '])/u';
$pattern = '/\$([1-9][0-9]*)/u';

return preg_replace_callback(
$pattern,
Expand Down
55 changes: 55 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,61 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void
$this->assertSame(['123/456'], $router->params());
}

/**
* @param list<string> $expectedParams
*/
#[DataProvider('provideMultipleSegmentsParamTrueWithManyPlaceholders')]
public function testMultipleSegmentsParamTrueWithManyPlaceholders(
string $route,
string $handler,
string $uri,
array $expectedParams,
): void {
$routingConfig = new Routing();
$routingConfig->multipleSegmentsOneParam = true;
Factories::injectMock('config', Routing::class, $routingConfig);

$this->createRouteCollection($routingConfig);
$this->collection->get($route, $handler);
$router = new Router($this->collection, $this->request);

$router->handle($uri);

$this->assertSame('\Catalog', $router->controllerName());
$this->assertSame('lookup', $router->methodName());
$this->assertSame($expectedParams, $router->params());
}

/**
* @return iterable<string, array{string, string, string, list<string>}>
*/
public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): iterable
{
$nineSegments = implode('/', array_fill(0, 9, '(:segment)'));
$tenSegments = implode('/', array_fill(0, 10, '(:segment)'));

yield 'nine placeholders (boundary)' => [
$nineSegments,
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9',
'a/b/c/d/e/f/g/h/i',
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],
];

yield 'ten placeholders' => [
$tenSegments,
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10',
'a/b/c/d/e/f/g/h/i/j',
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
];

yield 'out-of-range reference resolves to empty string' => [
$tenSegments,
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10/$11',
'a/b/c/d/e/f/g/h/i/j',
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ''],
];
}

public function testLogsRouteAttributeInstantiationFailureWithContext(): void
{
$collection = clone $this->collection;
Expand Down
Loading