dousui3124 2017-08-14 21:22
浏览 82

当使用类常量时,忽略PHPUnit测试

I'm writing integration tests with PHPUnit 6.2.2 and zendframework/zend-test 3.1.0 for a Zend Framework 3 application and observing following behavior:

When I define a constant in a test class, e.g.

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class FooTest extends AbstractHttpControllerTestCase
{
    const MY_CONST = 123;
}

and use it in a configuration file, e.g.

// /config/autoload/test/mymodule.local.php
use Test\Integration\...\FooTest;
return [
    'module' => [
        'my' => [
            'whatever' => [
                'key' => FooTest::MY_CONST
            ]
        ]
    ]
];

the test class gets completely ignored. (Checked with assertTrue(false).) When I replace FooTest::MY_CONST in the config file by a value, everything works again.

Looks like a bug, but I could not find out, whether it's a bug in PHPUnit or in Zend\Test. Has anybody observed this behavior before? Ideas, what it might be caused by (to create a bug ticket for PHPUnit or Zend\Test)?


phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
         backupGlobals="false" backupStaticAttributes="false" bootstrap="test/Bootstrap.php"
         cacheTokens="false" colors="true"
>
    <php>
        <env name="RUNTIME_CONTEXT" value="testing"/>
    </php>
    <testsuites>
        <testsuite name="unit-app-only">
            <directory suffix="Test.php">test/Unit</directory>
        </testsuite>
        <testsuite name="integration-app-only">
            <directory suffix="Test.php">test/Integration</directory>
        </testsuite>
        <testsuite name="app-lib">
            <directory suffix="Test.php">vendor/my-namespace/my-common-lib/tests</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html" target="build/coverage"/>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
        <log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
    </logging>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">module/**/src</directory>
            <directory suffix=".php">vendor/my-namespace/**/src</directory>
            <exclude>
                <directory suffix=".php">vendor/my-namespace/**/tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

test/Bootstrap.php

namespace Test;

use MyNamespace\Test\AbstractControllerDbTest;
use MyNamespace\Test\AbstractControllerTest;
use MyNamespace\Test\AbstractDbTest;
use MyNamespace\Test\DatabaseInitializer;
use RuntimeException;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;

error_reporting(E_ALL | E_STRICT);
ini_set('memory_limit', '768M');
chdir(__DIR__);

/**
 * Sets up the MVC (application, service manager, autoloading) and the database.
 */
class Bootstrap
{

    /** @var ServiceManager */
    protected $serviceManager;

    protected $applicationConfigPath;

    public function __construct()
    {
        $this->applicationConfigPath = __DIR__ . '/../config/application.config.php';
    }

    /**
     * Sets up the
     */
    public function init()
    {
        // autoloading setup
        static::initAutoloader();
        // application configuration
        $applicationConfig = require_once $this->applicationConfigPath;
        // service manager setup
        $this->setUpServiceManager($applicationConfig);
        // application setup
        $this->bootstrapApplication($applicationConfig);
        // database setup
        $dbConfigs = $this->serviceManager->get('Config')['db'];
        self::setUpDatabase($dbConfigs);
    }

    public function chroot()
    {
        $rootPath = dirname(static::findParentPath('module'));
        chdir($rootPath);
    }

    protected function setUpServiceManager($config)
    {
        $serviceManagerConfig = isset($config['service_manager']) ? $config['service_manager'] : [];
        $serviceManagerConfigObject = new ServiceManagerConfig($serviceManagerConfig);
        $this->serviceManager = new ServiceManager();
        $serviceManagerConfigObject->configureServiceManager($this->serviceManager);
    }

    protected function bootstrapApplication($config)
    {
        $this->serviceManager->setService('ApplicationConfig', $config);
        $this->serviceManager->get('ModuleManager')->loadModules();
        $listenersFromAppConfig     = isset($configuration['listeners']) ? $configuration['listeners'] : [];
        $config                     = $this->serviceManager->get('config');
        $listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];
        $listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));
        $application = $this->serviceManager->get('Application');
        $application->bootstrap($listeners);
    }

    protected function setUpDatabase(array $dbConfigs)
    {
        $databaseInitializer = new DatabaseInitializer($dbConfigs);
        $databaseInitializer->setUp();
        AbstractDbTest::setDbConfigs($dbConfigs);
        AbstractControllerTest::setApplicationConfigPath($this->applicationConfigPath);
        AbstractControllerTest::setDbConfigs($dbConfigs);
    }

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

        if (file_exists($vendorPath.'/autoload.php')) {
            include $vendorPath.'/autoload.php';
        }

        if (! class_exists('Zend\Loader\AutoloaderFactory')) {
            throw new RuntimeException(
                'Unable to load ZF2. Run `php composer.phar install`'
            );
        }

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

    protected 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 = new Bootstrap();
$bootstrap->init();
$bootstrap->chroot();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向