Merge pull request #7652 from kenjis/fix-url-to-error-msg

fix: `url_to()` error message
This commit is contained in:
Abdul Malik Ikhsan 2023-07-04 19:03:04 +07:00 committed by GitHub
commit d7cda9308f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -1239,6 +1239,11 @@ class RouteCollection implements RouteCollectionInterface
// Build our resulting string, inserting the $params in
// the appropriate places.
foreach ($matches[0] as $index => $pattern) {
if (! isset($params[$index])) {
throw new InvalidArgumentException(
'Missing argument for "' . $pattern . '" in route "' . $from . '".'
);
}
if (! preg_match('#^' . $pattern . '$#u', $params[$index])) {
throw RouterException::forInvalidParameterType();
}

View File

@ -17,6 +17,7 @@ use CodeIgniter\HTTP\URI;
use CodeIgniter\Router\Exceptions\RouterException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use InvalidArgumentException;
/**
* @backupGlobals enabled
@ -900,4 +901,20 @@ final class MiscUrlTest extends CIUnitTestCase
url_to('path-to', 'string', 13, 'en')
);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/7651
*/
public function testUrlToMissingArgument()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing argument for "([a-zA-Z]+)" in route "([a-zA-Z]+)/login".');
$routes = Services::routes();
$routes->group('(:alpha)', static function ($routes) {
$routes->match(['get'], 'login', 'Common\LoginController::loginView', ['as' => 'loginURL']);
});
url_to('loginURL');
}
}