Drupal PHPUnit测试中使用Url::fromRoute报路由不存在的解决办法
解决Drupal测试中Route "commerce_payment.checkout.return"不存在的问题
问题重现
测试类代码:
class ReturnHandlerTest extends OrderKernelTestBase { protected static $modules = [ 'commerce_payment', 'commerce_payment_example', ]; protected function setUp(): void { parent::setUp(); } }
测试中调用的代码:
Url::fromRoute('commerce_payment.checkout.return', $arguments) ->setOptions(['absolute' => true]) ->toString()
抛出的异常信息:
Symfony\Component\Routing\Exception\RouteNotFoundException: Route "commerce_payment.checkout.return" does not exist.
解决方法
1. 补充缺失的依赖模块
commerce_payment.checkout.return路由属于commerce_checkout模块,你的测试类当前未启用该模块,导致路由无法被识别。只需在测试类的static $modules数组中添加commerce_checkout即可:
修改后的测试类代码:
class ReturnHandlerTest extends OrderKernelTestBase { protected static $modules = [ 'commerce_payment', 'commerce_payment_example', 'commerce_checkout', // 新增依赖模块 ]; protected function setUp(): void { parent::setUp(); } }
2. 额外验证点
- 确认
OrderKernelTestBase是否默认启用了commerce_order模块(通常该基类会包含此依赖,若仍有异常可手动添加到模块列表) - 若问题持续,可在本地Drupal环境执行
drush route-list | grep commerce_payment.checkout.return,确认路由所属模块,确保测试用例中已启用对应模块
内容的提问来源于stack exchange,提问作者sk DYLAN
相关产品推荐
相关产品推荐

