4

在我的 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模块测试文件夹开始时,我既不能使用来自 s 的值,global.php也不能使用来自 s 的值local(尽管包含配置文件——我已经检查过了)。

例如: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.php并有效。local.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);

但是错误仍然存​​在。

4

2 回答 2

2

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

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

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

于 2013-11-04T13:06:00.837 回答
0

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

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

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

于 2013-05-14T20:32:28.617 回答