【问题标题】:How to provide namespace of Kernel to Codeception for a Symfony4 project如何为 Symfony4 项目的 Codeception 提供内核的命名空间
【发布时间】:2017-09-04 08:38:36
【问题描述】:

我正在使用 Coception 测试 Symfony4 的新目录结构。

安装并配置了 Codeception。因为 bundle 策略的结束,我知道会发生错误,这里是错误:

PHP 致命错误:未捕获的错误:...\my_project\vendor\codeception\codeception\src\Codeception\Module\Symfony.php:146 中找不到类“内核”

Codeception 找不到 Kernel 类,因为它的新命名空间是 App 而不是空的命名空间。

内核现在位于 src 目录并在 App 命名空间中声明。

新的内核声明

src/Kernel.php

namespace App; // <-- the namespace

[...]

class Kernel extends BaseKernel[...]

以前,Kernel 被命名为 AppKernel 并且没有命名空间:

旧内核声明

app/AppKernel.php

<?php

//no namespace

[...]

class AppKernel extends Kernel

我编辑了 Symfony.php 文件的第 265 行以更改返回 Kernel 的这一行:

$class = $file-&gt;getBasename('.php');

通过这一行返回App\Kernel:

$class = 'App\\'.$file-&gt;getBasename('.php');

此修改强制新结构的新命名空间。

我的功能测试运行良好,但我想避免修改供应商文件。

如何在没有此hack 的情况下强制使用 App 命名空间?似乎没有参数可以做到这一点,但我可以将代码接收封装在应用程序命名空间中,还是找到一个 hack 将它放在我的文件中,而不是在供应商文件中?

【问题讨论】:

  • class_alias('App\Kernel', 'AppKernel'); 还不够吗?
  • 好主意,但它不起作用。我试过了:我也试过了:class_alias('App\Kernel', 'Kernel'); 但我处理了这个错误:Cannot declare class App\Kernel, because the name is already in use
  • 对不起!有用!我把它放在两个bootstrap.php 文件中(在tests\functional 和tests 目录中)如果我只把它放在一个文件中,它当然会更好
  • 如果你想写你的答案,我会验证它。

标签: php symfony codeception symfony-3.3 symfony-flex


【解决方案1】:

Codeception https://github.com/Codeception/Codeception/issues/4695 中存在一个已知问题。基本上 Codeception 只允许某些 Symfony 内核名称:

# codeception/src/Codeception/Module/Symfony.php:287

$possibleKernelClasses = [
    'AppKernel', // Symfony Standard
    'App\Kernel', // Symfony Flex
];

目前的解决方法是使用@Federkun 指出的class_alias('MyNamespace\Kernel', 'App\Kernel');

【讨论】:

    【解决方案2】:

    您可以为 Kernel 类创建一个别名:

       class_alias('App\Kernel', 'Kernel');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2014-02-04
      相关资源
      最近更新 更多