简体   繁体   中英

How to get configs to the Config service in the testing context in Zend Framework 2?

I have a ZF2 application and want to write some integration tests for in. I'm using PHPUnit v6 (no zend-test ). PHPUnit is set up and the Bootstrap class implemented. Everything is/was working.

For normal requests the framework scans the config folders (usually defined in the config/application.config.php : ['module_listener_options']['config_glob_paths'] ) and merges all the configs for me. But now I'm in the testing context and need some configs from a custom config file (eg config/autoload/module/audit-logging.local.php ) -- and cannot find out, how to add/merge them to/with the other configs.

How to integrate configs from custom config files and make them available for the application, when running PHPUnit tests?

How about creating environment-specific configuration files, for example:

config
├── application.config.php
├── autoload
│   └── global.php
└── environment
    ├── development.php
    ├── production.php
    └── testing.php

and then adjusting config/application.config.php to this:

return [
    'modules' => $modules,
    'module_listener_options' => [
        'config_glob_paths' => [
            __DIR__ . '/autoload/{,*.}{global,local}.php',
            __DIR__ . '/environment/' . (\getenv('APPLICATION_ENVIRONMENT') ?: 'production') . '.php',
        ],
        'module_paths' => [
            __DIR__ . '/../module',
            __DIR__ . '/../vendor',
        ],
    ],
];

Now all you need to do is configure the APPLICATION_ENVIRONMENT environment variable, and set it to testing , for example. Then you can configure the application for specific environments with corresponding configuration files in config/environment .

<phpunit>
    <php>
        <env name="APPLICATION_ENVIRONMENT" value="testing" />
    </php>
    <testsuites>
        <testsuite name="Integration Tests">
            <directory>.</directory>
        </testsuite>
    </testsuites>
</phpunit>

Works fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM