1

我正在使用 PHPUnit6.2.2和 zendframework/zend-test3.1.0为 Zend Framework 3 应用程序编写集成测试,并观察以下行为:

当我在测试类中定义一个常量时,例如

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

并在配置文件中使用它,例如

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

测试类被完全忽略。(用 . 检查assertTrue(false)。)当我用FooTest::MY_CONST一个值替换配置文件时,一切都会再次运行。

看起来像一个错误,但我无法找出它是 PHPUnit 中的错误还是Zend\Test. 以前有人观察过这种行为吗?想法,它可能是由什么引起的(为 PHPUnit 或创建错误票证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();
4

0 回答 0