【问题标题】:Unit testing CakePHP 3 plugin requires a config/routes.php folder to test with Router::url()单元测试 CakePHP 3 插件需要一个 config/routes.php 文件夹来使用 Router::url() 进行测试
【发布时间】:2017-09-17 03:14:50
【问题描述】:

我正在为 CakePHP 3 插件编写一些测试,我的一些操作使用了Router::url 调用。当我运行 phpunit 时,出现以下错误:include([project dir]\\config\routes.php): failed to open stream: No such file or directory.

我想知道的是,这个文件是否真的需要单元测试才能工作。如果我在该文件夹上创建文件,则测试正常。我已经尝试添加

DispatcherFactory::add('Asset'); DispatcherFactory::add('Routing'); DispatcherFactory::add('ControllerFactory');

到我的tests/bootstrap.php 文件,但它根本没有改变。

由于这是一个独立的插件,我会觉得有一个 config 文件夹和一个 routes.php 文件只是为了测试有点奇怪。有什么解决方法吗?

【问题讨论】:

    标签: php unit-testing cakephp routes


    【解决方案1】:

    路由器要求routes.php 文件存在于应用程序级别,因此您应该配置一个可以放置此类文件的测试应用程序环境。

    在您的tests/bootstrap.php 文件中,定义您的测试环境所需的常量和配置。如果它只用于路由器,那么如果您相应地定义 CONFIG 常量就足够了,该常量在 \Cake\Routing\Router::_loadRoutes() 中使用,例如

    define('CONFIG', dirname(__DIR__) . DS . 'tests' . DS . 'TestApp' . DS . 'config' . DS);
    

    这会将配置目录设置为tests/TestApp/config/,您可以在其中放置routes.php 文件。

    一般来说,我建议设置所有常量,并且至少设置基本的应用程序配置,这是我的一个插件中的一个示例:

    use Cake\Core\Configure;
    use Cake\Core\Plugin;
    
    if (!defined('DS')) {
        define('DS', DIRECTORY_SEPARATOR);
    }
    define('ROOT', dirname(__DIR__));
    define('APP_DIR', 'src');
    define('APP_ROOT', ROOT . DS . 'tests' . DS . 'TestApp' . DS);
    define('APP', APP_ROOT . APP_DIR . DS);
    define('CONFIG', APP_ROOT . DS . 'config' . DS);
    define('WWW_ROOT', APP . DS . 'webroot' . DS);
    define('TESTS', ROOT . DS . 'tests' . DS);
    define('TMP', APP_ROOT . DS . 'tmp' . DS);
    define('LOGS', APP_ROOT . DS . 'logs' . DS);
    define('CACHE', TMP . 'cache' . DS);
    define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
    define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
    define('CAKE', CORE_PATH . 'src' . DS);
    
    require_once ROOT . DS . 'vendor' . DS . 'autoload.php';
    require_once CORE_PATH . 'config' . DS . 'bootstrap.php';
    
    $config = [
        'debug' => true,
    
        'App' => [
            'namespace' => 'App',
            'encoding' => 'UTF-8',
            'defaultLocale' => 'en_US',
            'base' => false,
            'baseUrl' => false,
            'dir' => 'src',
            'webroot' => 'webroot',
            'wwwRoot' => WWW_ROOT,
            'fullBaseUrl' => 'http://localhost',
            'imageBaseUrl' => 'img/',
            'cssBaseUrl' => 'css/',
            'jsBaseUrl' => 'js/',
            'paths' => [
                'plugins' => [APP_ROOT . 'plugins' . DS],
                'templates' => [APP . 'Template' . DS],
                'locales' => [APP . 'Locale' . DS],
            ],
        ]
    ];
    Configure::write($config);
    
    date_default_timezone_set('UTC');
    mb_internal_encoding(Configure::read('App.encoding'));
    ini_set('intl.default_locale', Configure::read('App.defaultLocale'));
    
    Plugin::load('MyPlugin', ['path' => ROOT]);
    

    【讨论】:

    • 抱歉回复晚了,感谢您的详细解答!我设置了CONFIG 常量,但使用的是项目根目录而不是tests 目录。示例配置也非常有用。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2023-03-12
    • 2014-12-25
    相关资源
    最近更新 更多