【问题标题】:Does PHPUnit ignore the config/autoload/... files in Zend Framework 2?PHPUnit 是否忽略 Zend Framework 2 中的 config/autoload/... 文件?
【发布时间】:2013-04-30 12:19:09
【问题描述】:

在我的 ZF2 应用程序中,我遵循处理配置的“标准”方法。有:

  • /config/application.config.php -- 默认应用范围设置

  • /config/autoload/global.php -- 默认应用范围设置

  • /config/autoload/local.php -- 环境特定的应用程序范围设置

  • /config/autoload/MODULENAME.local.php -- 环境特定模块特定设置

  • /module/MODULENAME/config/module.config.php -- 默认模块特定设置

现在,当我在模块测试文件夹中启动 PHPUnit 时,我既不能使用来自 global.php 的值,也不能使用来自 locals 的值(尽管包含配置文件 - 我已经检查过)。

例如:PHPUnit 调用我的自定义视图助手ContentForEnvironment,其中包含以下代码:$currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];

/path/to/project/module/Application/test# phpunit
PHPUnit 3.7.13 by Sebastian Bergmann.

Configuration read from /path/to/project/module/Application/test/phpunit.xml

E

Time: 0 seconds, Memory: 8.75Mb

There was 1 error:

1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Undefined index: environment

/path/to/project/vendor/MyNamespace/library/MyNamespace/View/Helper/ContentForEnvironment.php:32
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:400
/path/to/project/module/Application/view/layout/layout.phtml:25
/path/to/project/module/Application/view/layout/layout.phtml:25
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:507
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/View.php:205
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:126
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:136
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:332
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:285
/path/to/project/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:255
/path/to/project/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php:46

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

environment 选项在global.phplocal.php 中设置,并且在我在浏览器中运行应用程序时有效。

当我在默认模块特定配置文件 (module.config.php) 中设置选项时,可以读取设置值。

这里出了什么问题?


编辑

/module/Application/test/Bootstrap.php

<?php
namespace ApplicationTest;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
    protected static $serviceManager;
    protected static $config;
    protected static $bootstrap;

    public static function init()
    {
        // Load the user-defined test configuration file, if it exists; otherwise, load
        if (is_readable(__DIR__ . '/phpunit.config.php')) {
            $testConfig = include __DIR__ . '/phpunit.config.php';
        } else {
            $testConfig = include __DIR__ . '/phpunit.config.php.dist';
        }

        $zf2ModulePaths = array();

        if (isset($testConfig['module_listener_options']['module_paths'])) {
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
            foreach ($modulePaths as $modulePath) {
                if (($path = static::findParentPath($modulePath)) ) {
                    $zf2ModulePaths[] = $path;
                }
            }
        }

        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $baseConfig = array(
            'module_listener_options' => array(
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
        );

        $config = ArrayUtils::merge($baseConfig, $testConfig);

        $serviceManager = new ServiceManager(new ServiceManagerConfig());
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();

        static::$serviceManager = $serviceManager;
        static::$config = $config;
    }

    public static function getServiceManager()
    {
        return static::$serviceManager;
    }

    public static function getConfig()
    {
        return static::$config;
    }

    protected static function initAutoloader()
    {
        $vendorPath = static::findParentPath('vendor');

        if (is_readable($vendorPath . '/autoload.php')) {
            $loader = include $vendorPath . '/autoload.php';
        } else {
            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

            if (!$zf2Path) {
                throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
            }

            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';

        }

        AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true,
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
                ),
            ),
        ));
    }

    protected static function findParentPath($path)
    {
        $dir = __DIR__;
        $previousDir = '.';
        while (!is_dir($dir . '/' . $path)) {
            $dir = dirname($dir);
            if ($previousDir === $dir) return false;
            $previousDir = $dir;
        }
        return $dir . '/' . $path;
    }
}

Bootstrap::init();

/module/Application/test/phpunit.config.php

<?php
return array(
    'modules' => array(
        'Application',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            '../../../config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            'module',
            'vendor',
        ),
    ),
);

/module/Application/test/phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="Application Unit Tests">
            <directory>./ApplicationTest</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory>/path/to/project/module/Application/src/Application/</directory>
            <!--
            <exclude>
                <directory suffix=".phtml">/path/to/project/module/Application/</directory>
                <directory suffix=".php">/path/to/project/module/Application/test/</directory>
            </exclude>
            -->
        </whitelist>
    </filter>
</phpunit>

编辑

更改了 /module/Application/test/Bootstrap.php

// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
    'module_listener_options' => array(
        'module_paths' => $zf2ModulePaths,
    ),
    'modules' => array(
        'Application',
    )
);

// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/phpunit.config.php')) {
    $testConfig = include __DIR__ . '/phpunit.config.php';
} else {
    $testConfig = include __DIR__ . '/phpunit.config.php.dist';
}

$config = ArrayUtils::merge($baseConfig, $testConfig);

但错误仍然存​​在。

【问题讨论】:

  • 这与您的测试引导配置有关。请提供。
  • 感谢您的评论。我刚刚添加了引导程序和配置。

标签: unit-testing phpunit zend-framework2 configuration-files


【解决方案1】:

我遇到了同样的问题,通过测试控制器中的一个简单的行 setUp 方法解决了。

$serviceManager = Bootstrap::getServiceManager();    
$this->setApplicationConfig(Bootstrap::getConfig());

我的测试控制器扩展了 AbstractHttpControllerTestCase,我使用了 zend 网站上的引导程序。

【讨论】:

    【解决方案2】:

    我不确定这是否正确,但请尝试一下。只需在您的 PHPUnit 的 Bootstrap 中注释掉这一行并尝试运行您的测试。

    $config = ArrayUtils::merge($baseConfig, $testConfig);
    

    基本上我的猜测是您将 $testConfig 分解为 $baseConfig 并再次将其与 $testConfig 合并。这违背了它的目的。请试试这个,让我知道。稍后我将尝试运行您的配置。

    【讨论】:

    • 感谢您的提示。我试过了(我在答案中的最后一次编辑),但没有帮助。我瘦了,我得从头再设置一次测试环境。
    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2012-11-18
    • 2012-10-10
    • 2017-06-27
    • 2019-07-27
    • 2013-03-26
    • 2015-07-24
    • 2014-06-04
    相关资源
    最近更新 更多